【问题标题】:5-dimensional stacked barplot using ggplot2使用 ggplot2 的 5 维堆叠条形图
【发布时间】:2019-01-09 13:39:40
【问题描述】:

我有数据框,想在同一轴上绘制多个维度。

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 进行填充。

我需要年月计数的输出,但不知道如何表示产品。对于堆积条形图有什么最好的建议吗?

【问题讨论】:

  • 请发布您一直在处理的代码并在 Paint/Word/PPT 中绘制预期的输出,以便人们可以提供帮助
  • 肯定会尝试绘制预期的@Tung
  • 您能否详细说明为什么您不想使用facet_XXX 方法?

标签: r ggplot2


【解决方案1】:

我尝试不使用facet_gridfacet_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()

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多