2016-11-08 80 views
8

我想繪製對方使用facet_grid的頂部一些barplots:旋轉開關小標籤在GGPLOT2 facet_grid

library(ggplot2) 

df <- group_by(mpg, manufacturer) %>% 
    summarise(cty = mean(cty), hwy = mean(hwy)) %>% 
    ungroup() 

df <- melt(df, id.vars = "manufacturer") 

ggplot() + 
    geom_bar(data =df, aes(x = variable, y = value), stat = "identity") + 
    facet_grid(manufacturer ~ ., switch = "y") 

我用的ggplot2::facet_grid()switch說法讓小標籤在y顯示而不是在每個面的頂部。問題是小平面標籤垂直繪製,因此裁剪。有沒有什麼方法可以水平繪製面 - 標籤?到目前爲止,我發現的所有問題都與僅旋轉x軸標籤有關,而不是刻面標籤。

回答

18

你只需要添加主題(),並指定strip.text.y角度

library(ggplot2) 
df <- group_by(mpg, manufacturer) %>% 
    summarise(cty = mean(cty), hwy = mean(hwy)) %>% 
    ungroup() 

df <- melt(df, id.vars = "manufacturer") 

ggplot() + 
    geom_bar(data =df, aes(x = variable, y = value), stat = "identity") + 
    facet_grid(manufacturer ~ ., switch = "y")+ 
theme(strip.text.y = element_text(angle = 180)) 
+1

真棒。謝謝! – roming