【问题标题】:R-Studio ggplot: my face_wrap plots are cut off, how can I make the plots wider?RStudio ggplot:我的 facet_wrap 图被切断了,我怎样才能使图更宽?
【发布时间】:2020-11-06 11:49:15
【问题描述】:

我正在尝试使用 ggplot 和 face_wrap 创建 6 个折线图。然而,这些图总是局促和切断,因此您无法阅读 x 轴标签,而且图表似乎也被切断了。如何使它们变宽并调整 x 轴的文本以便我可以看到所有内容?

这是我目前的代码:

MPO8_marchapril_2020 <- read.csv("~/TU Berlin/3. Semester/MuB/Laermdaten Schoenefeld/MPO8_marchapril_2020.csv", sep=";", stringsAsFactors=TRUE, dec=",")
str(MPO8_marchapril_2020)
library(tidyr)
datagather2=gather(data = MPO8_marchapril_2020, key = "type of measurements", value = "sound pressure levels", -Datum, -Messstelle)
View(datagather2)
library(ggplot2)
ggplot(datagather2, aes(x=Datum,y=`sound pressure levels`, color=`type of measurements`, group=1))+
  geom_line()+
  labs(title="blabla - 2020", x="", y="")+
  theme(plot.title=element_text(size=30, face="plain"), 
        axis.text.x=element_text(size=15, angle=90), 
        axis.text.y=element_text(size=15),
        axis.title.x=element_text(size=25),
        axis.title.y=element_text(size=25))+
  facet_wrap(~`type of measurements`)

非常感谢您的帮助!!

【问题讨论】:

  • 如果您以交互方式执行此操作,则只需增大绘图窗口/窗格即可。如果您以编程方式执行此操作(例如,@akaDrHouse 建议的pdf(...)),那么只需使画布的宽度/高度变大。否则,只需插入 \n 和/或使用 paste(strwrap(...), collapse="\n") 强制多行构面标签。

标签: r ggplot2 facet-wrap


【解决方案1】:

一种方法是打印为 pdf 并指定宽度/高度选项。然后,您可以使用不同的维度来匹配您的情节。

ggplot(datagather2, aes(x=Datum,y=`sound pressure levels`, color=`type of measurements`, group=1))+
  geom_line()+
  labs(title="blabla - 2020", x="", y="")+
  theme(plot.title=element_text(size=30, face="plain"), 
        axis.text.x=element_text(size=15, angle=90), 
        axis.text.y=element_text(size=15),
        axis.title.x=element_text(size=25),
        axis.title.y=element_text(size=25))+
  facet_wrap(~`type of measurements`) -> plotforpdf

pdf(file = "path.myplotname.pdf", height = 7, width = 12) #default is inches
print(plotforpdf)
dev.off() # close the pdf printer device

【讨论】:

    最近更新 更多