【问题标题】:change color in grouped barplot ggplot in R [duplicate]在R中更改分组条形图ggplot的颜色[重复]
【发布时间】:2019-03-19 08:28:37
【问题描述】:

我想更改以下分组条形图的颜色,使每个物种都具有相同的颜色,而不管条件如何。

# create a dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , 
rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))
data=data.frame(specie,condition,value)

# Grouped
ggplot(data, aes(fill=condition, y=value, x=specie)) + 
  geom_bar(position="dodge", stat="identity")

当前输出:

想要的输出:

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    您可以将条件设​​置为一个组,并将填充保持为实物:

    # Grouped
    ggplot(data, aes(group = condition, fill=specie, y=value, x=specie)) + 
      geom_bar(position="dodge", stat="identity")
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多