我有数据框,想在同一轴上绘制多个维度。
df <- data.frame(Year = sample(2017:2018, 100, replace = T),
Months = sample(month.name, 100, replace = T),
catergory = sample(letters[1:4], 100, replace = T),
count = sample(1:1000, 100, replace = T),
Product = sample(c("Apple", "Orange", "Grapes", "Banana"),
100, replace = T)
)
我想将year
&映射month
到 x 轴、count
y 轴并catergory
填充。
我需要带有年月计数的输出,但不知道如何表示产品。堆叠条形图的最佳建议是什么?
我在不使用facet_grid
或facet_wrap
但变得非常困难或我的数据不支持的情况下进行了尝试。所以使用facet_wrap
我得到了解决方案。
df$Months<-factor(df$Months,levels = month.name) ##Reordering the Months
df$Year<-factor(df$Year) ##changing to factorial
ggplot(df,aes(x=Months,y=count,fill=category))+ geom_bar(stat = "identity",position = "stack")+
facet_grid(Year~Product)+coord_flip()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句