2017-01-03 248 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'不是一個選項,但是無論如何,我會記住的! –