)和dev.off()将图表写入文件。代码就像这样
allWeeks = data.frame(weekInYear = 1:53,
user2013 = sample(1:100, 53),
user2014 = sample(1:100, 53),
user2015 = sample(1:100, 53),
job2013 = sample(1:100, 53),
job2014 = sample(1:100, 53),
job2015 = sample(1:100, 53))
allWeeks[30:53, c('user2015', 'job2015')] = NA
melted = melt(allWeeks[ , c('weekInYear', 'user2015', 'job2015')], id = 'weekInYear')
pic1 = 'output/allWeek2015.png'
png(width = 600, height = 300, file = pic1)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
geom_line()
dev.off()
melted = melt(allWeeks[ , c('weekInYear', 'user2013', 'user2014', 'user2015')], id = 'weekInYear')
pic2 = 'output/allWeekUser.png'
png(width = 600, height = 300, file = pic2)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
geom_line()
dev.off()
melted = melt(allWeeks[ , c('weekInYear', 'job2013', 'job2014', 'job2015')], id = 'weekInYear')
pic3 = 'output/allWeekJob.png'
png(width = 600, height = 300, file = pic3)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
geom_line()
dev.off()
当我选择所有代码并单击运行时,它运行良好,并在绘制数据中显示有关NA的一些警告消息。
当我点击source
时,没有消息,图片为空白。任何帮助都值得赞赏...
更新:
替换为ggsave(pic1, width = 3, height = 1.5)
,现在没有空白问题。但ggsave()
似乎使用了不合理的比例和字体大小...查看图片:LEFT已保存png()
和dev.off()
与RIGHT一起保存ggsave()
。我错过了一些参数吗?任何帮助都值得赞赏