0

我有一个如下所示的数据集:

a<-data.frame(Strain=rep(LETTERS[1:3], each=2),
              Mean=rnorm(6,0.5,0.1),
              Treat=rep(letters[1:2],6))

而且我想使用ggplot将它绘制在条形图中,但是对于对应于“C”的应变具有不同的填充颜色。我不知道我是否有正确的方法,我只是使用该特定代码添加另一层,但是说填充的长度不正确。

b <- ggplot(data=a, aes(x=factor(Strain), y=Mean, fill=factor(Treat))) + 
     geom_bar(position='dodge', stat='identity') +
     geom_bar(data=subset(a,a$Strain=="C"), fill=c('red','blue'),
              position='dodge', stat='identity')

很抱歉这个基本问题,但到目前为止我还没有解决这个问题。谢谢你的帮助!

4

1 回答 1

2

像这样?

a$treat2 <- as.character(a$Treat)
a$treat2[a$Strain=="C"] <- paste("C",a$treat2[a$Strain=="C"],sep=".")

#function to create ggplot default colours
ggcolours <- function(n) force(hcl(h=seq(15, 375-360/n, length=n)%%360, c=100, l=65))

b <- ggplot(data=a, aes(x=Strain, y=Mean, group=Treat,fill=treat2)) + 
  geom_bar(position='dodge', stat='identity') +
  #use scale_fill_manual to specify the colours
  scale_fill_manual(values=c(ggcolours(2),"#FF0000","#0000FF"))

print(b)

在此处输入图像描述

于 2013-07-01T11:39:29.210 回答