ggplot2 中具有多种填充颜色的 geom_bar

Vin*_*der 0 r ggplot2 geom-bar

我有这样的数据集:

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))
Run Code Online (Sandbox Code Playgroud)

我想得到这个结果ggplot2

在此处输入图片说明

但我只有这个

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

在此处输入图片说明

你有什么解决办法吗?

Sve*_*ein 5

如果您希望所有条形都具有不同的颜色,则必须使用interactionoftypenom

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

在此处输入图片说明