【问题标题】:Rstudio error when exporting ggplot2 graph导出ggplot2图形时出现Rstudio错误
【发布时间】:2016-01-23 16:17:45
【问题描述】:

我正在尝试使用 ggplot2 导出一个简单的条形图。一切正常,但是当打印该图时,它会显示以下错误:

In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), ... : Zeichensatzfamilie in der Windows Zeichensatzdatenbank nicht gefunden

用英文会说:

In grid.Call(L_text, as.graphicsAnnot(x$label), ... : font family not found in Windows font database

当我尝试使用绘图顶部的导出按钮将文件导出为 pdf 时,它永远不会完成保存绘图,我必须使用任务管理器关闭 Rstudio。

我已经读过我应该安装 extrafont 包,导入和加载字体,我也这样做了。这是我使用的代码:

    library(ggplot2)
library(extrafont)
font_import()
y
loadfonts()

Data <- Liposarcoma.ARSA_2 #txt. file with my data
newData <- Data[-c(1,2),] #delete Header


errors <- as.numeric(newData$V10)


samplev <- data.frame(samples=(newData$V3), Target=(as.numeric(newData$V8)))
samplev         #create dataframe for Target/ref


dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = samplev$Target + errors,
              ymin = samplev$Target - errors)
p <- ggplot(data = samplev, aes(x = samples, y = Target))

p + geom_bar(stat = "identity", position = dodge, fill="darkgrey") +
  geom_errorbar(limits, position = dodge, width = 0.25) +
  theme(axis.text.x=element_text(samplev$samples), axis.ticks.x=element_blank(),
        axis.title.x=element_blank(), axis.text=element_text(size=12, colour="black"),
        plot.title=element_text(size=18, face="bold")
        ) +
        ylab("Target/Reference") +
        ggtitle("ARSA: Liposarcoma cell lines") +
        geom_text(data=samplev, aes(x=samples, y=Target+0.15, label=Target, vjust=0))

提前谢谢你:)

【问题讨论】:

  • 如果你不使用 RStudio 运行它,你会得到同样的错误吗?
  • 我们无法重现您的错误,我们无权访问您的数据集。你似乎没有在你的情节中要求不同的字体
  • 有一个函数名为pdfFonts。它可能用于更改该图形设备的默认字体,尽管(还不是 RStudio 用户)我想 RStudio 可能有自己的控制图形设备的机制?
  • 您是否尝试过使用pdf() 设备或ggsave() 而不是RStudio 按钮?或者设置一个你知道你有的字体?
  • @Dason :是的,我在不使用 RStudio 的情况下也会遇到同样的错误

标签: r pdf plot ggplot2


【解决方案1】:

我解决了这个问题。问题是我认为我会指定我的 x-labels 使用:

axis.text.x=element_text(samplev$samples)

但是 element_text 中的第一个元素实现了字体,而不是数据!这就是它找不到字体的原因,因为 samplev$samples 中的数据不是字体名称。代码现在看起来像这样并且运行良好:

pdf(file = "Liposarcoma ARSA.pdf")
p <- ggplot(data = samplev, aes(x = samples, y = Target))

p + geom_bar(stat = "identity", position = dodge, fill="#3399FF") +
  geom_errorbar(limits, position = dodge, width = 0.25) +
  theme(axis.text.x=element_text(angle = 45, hjust=1), axis.ticks.x=element_blank(),
        axis.text.y=element_text(size = 12), axis.title.y = element_text(size=15),
        axis.title.x=element_blank(), axis.text=element_text(size=12, colour="black"),
        plot.title=element_text(size=18, face="bold")
        ) +
        ylab("Target/Reference") +
        ggtitle("ARSA: Liposarcoma cell lines") +
        geom_text(data=samplev, aes(x=samples, y=Target+0.1, label=Target, vjust=0))
dev.off()

【讨论】:

    猜你喜欢
    • 2019-03-02
    • 2023-03-08
    • 2013-12-07
    • 1970-01-01
    • 2022-11-11
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多