【问题标题】:Can't view ggplot generated pdf in Adobe despite using dev.off() comand尽管使用 dev.off() 命令,但无法在 Adob​​e 中查看 ggplot 生成 pdf
【发布时间】:2015-01-23 18:30:43
【问题描述】:

我无法打开使用 ggplot 创建的 pdf。当我尝试在 Adob​​e 中打开文件时,我收到一条错误消息,指出我无法打开该文件,因为它仍在另一个应用程序中打开。我 am 使用 dev.off() 所以我不知道为什么我无法打开 pdf...

这是我的图形参数和变量:

stepheights=c(1.3,  2.6,  3.9,  5.2,  6.6,  7.9,  9.2, 10.5, 11.8, 13.1, 14.4,   15.7, 17.1, 18.4, 19.7, 21.0, 22.3, 23.6, 24.9, 26.2, 27.6, 28.9, 30.2, 31.5, 32.8, 34.1)

  Qs=c(2.0381420, NaN, 2.9921695, NaN, 6.9614420, 109.6719419, 80.7758644, NaN, 7.9477900, 18.3467407, 46.6796869, 117.7829841, 122.2436537, 6.8123483, 26.6823085, 74.5536880, 0.0421594, 92.1911593, 42.8689561, 18.5230607, 23.0842770, 132.5477074, 50.2996339, 3.1371337, NaN,NaN)

这两个变量都在数据框 VelocityData 中

 #create our graphing parameters using ggplot
downstream_QS<-ggplot(data=VelocityData, aes(x=stepheights, y=Qs))+geom_point() +
  stat_smooth(method="lm",formula=y~log(x), fill="red") +
  scale_x_continuous(breaks=x) +
  labs(x="Distance Downstream (cm)", y="Sediment Transport") +
  ggtitle("Sediment Transport Downstream of a Backward-Facing Step as Calculated from Near-bed Shear Stress") + 
  theme(plot.title=element_text(family="Palatino", face="bold", size=24), 
        axis.title.y=element_text(family="Palatino", face="bold", 
                                  size=14), axis.title.x=element_text(family="Palatino", face="bold", size=14),
        axis.text.y=element_text(family="Palatino", face="bold", size=10),
        axis.text.x=element_text(family="Palatino", face="bold", size=10))

pdf(paste('C:/Users/kcpotter/Desktop/Flume/BS Experiments/Step/ADV Qs Profile.pdf'), width=8, height=4)
print(downstream_QS)
dev.off()

【问题讨论】:

  • 您可以删除文件并重试吗?这听起来不像是与 R 相关的错误。
  • 删除文件然后使用 ggsave() 解决了问题。

标签: r pdf ggplot2


【解决方案1】:

您的 ggplot 代码有一个小问题。

对于线路:

scale_x_continuous(breaks=x) +

您需要使用 x 变量 stepheights

然后它应该可以工作了。

为了省钱,我更喜欢ggsave。具有两个保存选项的完整工作代码是:

library(ggplot2)

# Create dataframe
 stepheights=c(1.3,  2.6,  3.9,  5.2,  6.6,  7.9,  9.2, 10.5, 11.8, 13.1, 14.4,   15.7, 17.1, 18.4, 19.7, 21.0, 22.3, 23.6, 24.9, 26.2, 27.6, 28.9, 30.2, 31.5, 32.8, 34.1)
 Qs=c(2.0381420, NaN, 2.9921695, NaN, 6.9614420, 109.6719419, 80.7758644, NaN, 7.9477900, 18.3467407, 46.6796869, 117.7829841, 122.2436537, 6.8123483, 26.6823085, 74.5536880, 0.0421594, 92.1911593, 42.8689561, 18.5230607, 23.0842770, 132.5477074, 50.2996339, 3.1371337, NaN,NaN)
 VelocityData <- data.frame(stepheights = stepheights, Qs = Qs)

#create our graphing parameters using ggplot
 downstream_QS<-ggplot(data=VelocityData, aes(x=stepheights, y=Qs)) + geom_point() +
  stat_smooth(method="lm", formula=y ~ log(x), fill="red") +
  scale_x_continuous(breaks=stepheights) +
  labs(x="Distance Downstream (cm)", y="Sediment Transport") +
  ggtitle("Sediment Transport Downstream of a Backward-Facing Step as Calculated from Near-bed Shear Stress") + 
  theme(plot.title=element_text(family="Palatino", face="bold", size=24), 
        axis.title.y=element_text(family="Palatino", face="bold", 
                              size=14),      axis.title.x=element_text(family="Palatino", face="bold", size=14),
    axis.text.y=element_text(family="Palatino", face="bold", size=10),
    axis.text.x=element_text(family="Palatino", face="bold", size=10))

 # Original method of saving
 pdf(paste('ADV Qs Profile.pdf'), width=8, height=4)
 print(downstream_QS)
 dev.off()

# or you can use ggsave - I've changed the sizes to get the titles to fit
 ggsave('ADV Qs Profile2.pdf',downstream_QS, width=18, height=14, units="in")

【讨论】:

    猜你喜欢
    • 2013-11-29
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2018-02-06
    相关资源
    最近更新 更多