2015-12-27 143 views
-1

爲了獲得更好的可視化效果,我需要將構面的條形文本移動到每個構面的頂部。R:ggplot2:將strip.text從「y」軸更改爲多面圖中的「x」軸?

現在他們在右側,但我需要他們頂部。

enter image description here

這是我的代碼:

tvs.rangos <- ggplot(tvs , aes(rangos)) 
tvs.rangos + geom_histogram(aes(fill = marca), width = .7) + labs(title = "Marcas de TV por rangos de precios", 
                   x = "rango de precios", y = "cantidad de tvs") + 
     facet_grid(ecommerce ~ .) + 
     scale_fill_brewer(palette="Set3") + 
     theme(axis.text.x = element_text(colour="grey10",size=16,hjust=.5,vjust=.5,face="bold"), 
       axis.text.y = element_text(colour="grey10",size=18,,hjust=1,vjust=0,face="plain"), 
       axis.title.x = element_text(colour="grey40",size=12,angle=0,hjust=.5,vjust=0,face="plain"), 
       axis.title.y = element_text(colour="grey40",size=12,angle=90,hjust=.5,vjust=.5,face="plain"), 
       plot.title = element_text(size = 16,vjust=2), 
       legend.title = element_text(colour="grey40", size=16, face="bold"), 
       legend.text = element_text(colour="grey10", size=16, face="bold"), 
       strip.text.y = element_text(size = 20, angle = 0)) 

我發現這個話題:

ggplot2: Using gtable to move strip labels to top of panel for facet_grid

不過,我正在尋找一個simplier方法,如果有的話。

+0

你能提供的數據(你的對象tvs.rangos),這樣更容易爲人們幫助一個簡單的例子嗎? – tagoma

+0

也許你可以嘗試使用'coord_flip'和'scale_x_reverse'就像他們在這裏所做的:http://stackoverflow.com/questions/29504883/ggplot2-how-to-swap-y-axis-and-right-facet-strip-以及如何訂購的方面值 – tagoma

回答

1

如果您正在尋找替代方案,爲什麼不簡單使用facet_wrap()。文本將在每個方面的頂部。我沒有訪問您的數據集,但這裏是

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + facet_wrap(~Species, nrow=3) 

enter image description here

+0

謝謝。你能否解釋爲什麼我的「facet_grid」方法錯了?何時使用「facet_wrap」和何時使用「facet_grid」?謝謝。 –

+0

你沒有錯,但你只是尋找不同的東西:-)一個在頂部,另一個在一邊!並感謝您接受答案! – MLavoie