2017-01-03 73 views
0

我想更改ggplot2中刻面图中的组标签。此代码ggplot2中的定制x轴刻度值facet_wrap

data = tidyr::gather(data = iris, variable, value, -Species) 

ggplot(data, aes(y = value, x = variable, fill = Species)) + 
    geom_boxplot(position = "dodge", notch = TRUE) + 
    ylab("median(x)") + 
    facet_wrap(~variable, scales = "free", ncol = 2) 

产生 enter image description here

我想x轴刻度标记标签来表示每个种名代替相应的子情节正由分组的变量(其也被重复最新已经在分区标题中)。有关我如何实现这一目标的任何想法?我一直在搜索网络,但没有找到任何有价值的信息。

最好的问候, 安迪

更新

亨里克给出了正确的答案,我不得不改变适当aes功能。谢谢!

+4

替换'用X = variable''X =物种在你的'aes'? – Henrik

+0

谢谢,看起来好像我不得不重述ggplots语法! –

+0

在'facet_wrap'中也有一个名为'labeller'的参数...不知道它是否可以使用但值得一看 – Sotos

回答

0

你可以试试这个(不小)来实现类似的东西:

library(gridExtra) 
grid.arrange(ggplot(iris, aes(Species, Sepal.Length, fill = Species))+geom_boxplot(position = "dodge", notch = TRUE) + xlab('') + guides(fill=FALSE), 
      ggplot(iris, aes(Species, Sepal.Width, fill = Species))+geom_boxplot(position = "dodge", notch = TRUE) + xlab('') + guides(fill=FALSE), 
      ggplot(iris, aes(Species, Petal.Length, fill = Species))+geom_boxplot(position = "dodge", notch = TRUE) + xlab('') + guides(fill=FALSE), 
      ggplot(iris, aes(Species, Petal.Width, fill = Species))+geom_boxplot(position = "dodge", notch = TRUE) + xlab('') + guides(fill=FALSE)) 

enter image description here 如果你想有一个共同的图例参考这个:Add a common Legend for combined ggplots

+0

对于我的目的'grid.arrange'不是一个选项,但是无论如何,我会记住的! –