增加 ggplot2 中的绘图大小(宽度)

Increase plot size (width) in ggplot2

下面是我想包含在论文中的情节。问题是我的图的宽度太小了(这使得 x 轴根本不可读)

这是 ggplot2 代码 myCode.r :

require("ggplot2")

all <- read.csv(file="benchmark/bench.query.csv", head=TRUE, sep=";")

w <- subset(all, query %in% c("sort.q1", "sort.q2", "sort.q3", "sort.q4", "sort.q5"))

w$rtime <- as.numeric(sub(",", ".", w$rtime, fixed=TRUE))

p <- ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore))
p <- p + scale_shape_manual(values = 0:length(unique(w$triplestore)))
p <- p + geom_point(size=4)
p <- p + geom_line(size=1,aes(group=triplestore))
p <- p + labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))")
p <- p + scale_fill_continuous(guide = guide_legend(title = NULL))
p <- p + facet_grid(trace~type)
p <- p + theme_bw()

ggsave(file="bench_query_sort.pdf")

print (p)

我四处寻找如何放大情节,但我一无所获。

知道我的代码中要 add/delete/modify 什么吗?

可能最简单的方法是使用图形设备(png、jpeg、bmp、tiff)。您可以按如下方式设置图片的确切宽度和高度:

png(filename="bench_query_sort.png", width=600, height=600)

ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore)) + 
  scale_shape_manual(values = 0:length(unique(w$triplestore))) + 
  geom_point(size=4) + 
  geom_line(size=1,aes(group=triplestore)) + 
  labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))") + 
  scale_fill_continuous(guide = guide_legend(title = NULL)) + 
  facet_grid(trace~type) + 
  theme_bw()    

dev.off()

widthheight 以像素为单位。这在准备要在 Internet 上发布的图像时特别有用。有关更多信息,请参阅 ?png.

的帮助页面

或者,您也可以使用 ggsave 来获得您想要的确切尺寸。您可以使用以下方式设置尺寸:

ggsave(file="bench_query_sort.pdf", width=4, height=4, dpi=300)

widthheight的单位是英寸,dpi可以设置图片的质量。

在 Jupyter notebook 中,我发现以下内容很有帮助:

# Make plots wider 
options(repr.plot.width=15, repr.plot.height=8)

如果您使用 RMD(R Markdown),这将是定义宽度和高度的最简单方法。

```{r fig.align="center", echo = FALSE,fig.width = 14}
<write the code for your plot here>
```

注意: options() 对我不起作用所以我使用了这个方法