我无法打开使用ggplot创建的pdf。当我尝试在Adobe中打开文件时,我收到一条错误消息,指出我无法打开该文件,因为它仍然在另一个应用程序中打开。我我使用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()
答案 0 :(得分:1)
你的ggplot代码存在一个小问题。
对于这一行:
scale_x_continuous(breaks=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")