简体   繁体   English

R的问题:使用ggplot2更改多个箱形图的标题

[英]Troubles with R: Changing headings of multiple boxplots using ggplot2

I've run into a problem, that I can't seem to solve, and after having searched the internet for 24h I decided to ask in here. 我遇到了一个似乎无法解决的问题,在互联网上搜索了24小时后,我决定在这里询问。

I have a 4 boxplot figure, that I made with the ggplot2. 我有一个用ggplot2制作的4箱线图。 Each boxplot is named after the data variable. 每个箱线图均以数据变量命名。 But to make it easier for the reader to understand, I would like to change the title of each single boxplot (without changing the name in the data set). 但是为了使读者更容易理解,我想更改每个箱形图的标题(而不更改数据集中的名称)。 Is this possible? 这可能吗? I've tried using "ggtitle", "rename", "main=" and several other suggestions, I found on the internet. 我尝试使用“ ggtitle”,“ rename”,“ main =“和其他一些建议,我在互联网上发现了这些建议。

Here is the coding I've done: 这是我完成的编码:

d11 <- droplevels(subset(d, time=="Baseline" | time=="Pre-ECT" | time=="1st Period Post-ECT" | time=="2nd Period Post-ECT"))

ggplot(melt(d11, id.vars= c("subj", "time", "time.num", "intervention","order", "age", "sex", "diagn", "diagn2"), measure.vars = c("HR", "BPsys", "BPdia", "CO")), aes(x=time, y = value, fill = intervention)) + 
  geom_boxplot()  +
  theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5)) + 
  facet_wrap(~variable, scales= "free")

4箱图

I would like the headings in the boxplots to be "Heart rate", "Systolic blood pressure", "Diastolic blood pressure", "Cardiac output" instead. 我希望方框图中的标题改为“心率”,“收缩压”,“舒张压”,“心输出量”。

Any suggestions to a function, that might work? 对某个功能有什么建议可能可行?

You need to use the labeller argument in facet_wrap . 您需要使用labeller的参数facet_wrap The easiest way to use it is with as_labeller and a named vector. 使用它的最简单方法是使用as_labeller和一个命名向量。

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point() +
  facet_wrap(~Species, 
    labeller = as_labeller(c("setosa" = "I. setosa", "versicolor" = "I. versicolor",  "virginica" = "I. virginica")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM