【发布时间】:2012-11-01 11:06:52
【问题描述】:
我正在使用 ggplot2 创建一个简单的阶梯图。如果我将文件类型从 PNG 切换为 PDF,则绘图不会显示标签、刻度、标题或图例。我做错了什么?
数据:
plotData <- structure(list(iteration = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), time = c(0L, 10L,
20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 0L, 10L, 20L, 30L,
40L, 50L, 60L, 70L), routes = c(6L, 6L, 5L, 3L, 3L, 3L, 3L, 3L,
2L, 1L, 0L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 0L)), .Names = c("iteration",
"time", "routes"), class = "data.frame", row.names = c(NA, -19L
))
代码:
library(ggplot2)
x_axis_breaks <- seq(10, 100, by = 10)
png(file="plot.png",width=1280, height=1280)
## pdf(file="plot.pdf",width=6,height=6)
plot <- ggplot(plotData) + geom_step(data=plotData, size = 5,
mapping=aes(x=time,
y=routes, group=iteration, colour=factor(iteration)), direction="vh")
plot <- plot + scale_x_discrete(breaks=x_axis_breaks, name="time") +
scale_y_discrete(name="#routes");
plot <- plot + opts(axis.text.x=theme_text(size=36,face="bold"),
axis.text.y=theme_text(size=36,face="bold")) +
scale_colour_hue(name="iteration")
plot <- plot + opts(legend.title=theme_text(size=36,face="bold"),
legend.text=theme_text(size=36,face="bold"))
plot <- plot + opts(axis.title.x=theme_text(size=36,face="bold"),
axis.title.y=theme_text(size=36,face="bold"))
plot <- plot + opts(title="network lifetime",
plot.title=theme_text(size=36, face="bold"))
print(plot)
dev.off()
如果我从“png...”切换到“pdf”,就会出现问题。数据本身绘制得很好。也许我只是错过了一些关于在 ggplot2 中生成 PDF 绘图的信息?
【问题讨论】:
-
我看到你在这里有
dev.off()(虽然它没有进入你的代码块),但是这里的症状听起来很可疑,就像忘记打电话给dev.off()... -
清理了你的代码,但没有改变它的功能。在带有 R-devel 的 Ubuntu 10.04 上为我工作。你能给出
sessionInfo()的结果吗? -
在 OSX 和 R 2.15.1 上也适用于我。你可以试试
ggsave("plot.png")。 -
@DrewSteen 抱歉,我在发布答案后立即看到了您的评论。 :-/