【问题标题】:Weird character strings when generating pdf生成pdf时奇怪的字符串
【发布时间】:2019-08-13 12:45:08
【问题描述】:

我使用 ggplot2 和 geom_bar 生成条形图。

然后,当使用ggexport(或pdf)将其导出为pdf时,字符串的每个字符都堆叠在一起;图表在 Rstudio 预览窗口中看起来不错。

在之前的图表中,我已经使用了带有 geom_lines 的 ggexport,它做得很好。

有人可以帮我吗?提前谢谢你。

  #define font 
  font="Arial"
  #define general atributs of graphs
  axisTitle=element_text(size=10, family = font, face = "bold")
  axisText=element_text(size=10, family = font)

  theme1=  theme(
    axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, size = 10, family = font),
    axis.text.y = axisText,
    axis.title.x = element_blank(),    
    axis.title.y = axisTitle,
    axis.line.x = element_line(size=0.1),
    axis.line.y = element_line(size=0.1),
    axis.ticks.x = element_line(size=0.1),
    axis.ticks.y = element_line(size=0.1),
    plot.margin = unit(c(0.5,0.5,0.5,0), "cm"))#top, right, bottom,left

  graf2<-ggplot(data = subset(mBdata, subset = (Measurements == "1_1_x_4" | Measurements == "1_2_x_4") ), aes(x=reorder(VarUnit, -averages), y=averages)) +
    geom_bar(aes(fill = Measurements),stat="identity", position="dodge", color="black")+
    coord_cartesian(ylim = c(1,1e7))+
    scale_y_log10(expand = expand_scale(mult = 0, add = 0))+
    ylab("Averages over cycle") +    
    scale_fill_manual(values=c(color1,color2), labels=c("B30","B00"),name=NULL)+
    theme(legend.position=c(0.10, 0.90),
          legend.direction = "horizontal",
          legend.text = element_text(size=8, family = font))+
    annotate("text", label = "cycle1", x= "work [kWh]", y = 1e7*0.5, color = color1, size = 4, family = font, hjust = 0, vjust=0)+
    theme1

fname="test"

ggexport(graf2,filename = paste(fname,"pdf",sep = "."),
           width = 7.27, 
           height = 9.5/2)

这里是使用 dput(head(mBdata)) 的数据:

structure(list(VarUnit = c("VIA_CO [ppm]", "FID_HC [ppm]", "CLD700_NOx [ppm]", 
"CPC_PN [#/cm3]", "FTIR_CO [ppm]", "FTIR_NOX [ppm]"), Measurements = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("1_1_x_4", "2_1_x_4", "1_2_x_4", 
"2_2_x_4", "1_1_x_5", "2_1_x_5", "1_2_x_5", "2_2_x_5"), class = "factor"), 
    averages = c(0.0851193781232649, 2.01203081621322, 217.02611049417, 
    640967.527383398, 0.737018707801222, 225.867359490006)), row.names = c(NA, 
6L), class = "data.frame")

【问题讨论】:

  • 此代码不可重现。缺少数据以及其他对象。所以很难猜测这里发生了什么。另外:你试过ggsave吗?
  • 帮助我们通过发布数据结构来帮助您。复制并粘贴dput(mBdata) 的结果。如果它真的很大,请dput(head(mBdata))
  • 感谢您的反馈。添加了信息。 ggsave 产生的结果与 ggexport 相同
  • 似乎是警察错误。您是否尝试过在不更换警察的情况下也这样做?
  • @Ben G 在不改变警察的情况下,它可以工作;更换警察并使用 Cair_pdf(来自 grDevices)也可以。就我的理解水平而言,仍然有点模糊,但无论如何谢谢!

标签: r pdf ggplot2


【解决方案1】:

无需更换警察,它就可以工作;更换警察并改用 Cair_pdf(来自 grDevices)也可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-01-20
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多