【问题标题】:geom_bar with multiple fill colour in ggplot2ggplot2中具有多种填充颜色的geom_bar
【发布时间】:2014-03-02 13:14:49
【问题描述】:

我有这种数据集:

comb<-data.frame(nom=c("A","B","C","A","B","C"),type=c(rep("1",3),rep("2",3)),val=c(1,3,2,3,2,2))

我想用ggplot2 得到这个结果:

但我只有这个

ggplot()+    geom_bar(data=comb,aes(x=nom, y=val,fill=type),stat='identity',position='dodge')

你有什么解决办法吗?

【问题讨论】:

    标签: r ggplot2 geom-bar


    【解决方案1】:

    如果你想让所有的条有不同的颜色,你必须使用typenominteraction

    library(ggplot2)
    ggplot() +  
      geom_bar(data = comb,aes(x = nom, y = val, fill = interaction(type, nom)),
               stat = 'identity', position = 'dodge')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多