尽管使用 dev.off() 命令,但无法在 Adob​​e 中查看 ggplot 生成 pdf

Can't view ggplot generated pdf in Adobe despite using dev.off() comand

我无法打开使用 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()

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

对于行:

scale_x_continuous(breaks=x) +

您需要使用 stepheights

的 x 变量

然后它应该可以工作了。

为了节省我更喜欢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")