在ggplot中更改颜色填充和点的形状

时间:2016-12-26 22:41:13

标签: r ggplot2

我想在ggplot中更改点的形状和颜色填充,这是我的部分工作的代码,颜色图例仍然是黑色。

ID<-rep(c("KO", "WT"), each=4)
O<-rep(c("HP", "NN"), each=2, times=2)
Methionine<-rep(c("- Methionine", "+ Methionine"), each=2, times=2)
mean.1<-sample(50:100, 8)
se.1<-runif(8, min=1, max=3)
mydata<-data.frame(ID, O, Methionine, mean.1, se.1)

ggplot(mydata, aes(x=O, y=mean.1, ymax=max(mean.1), shape=Methionine, fill=ID)) + 
  geom_errorbar(aes(ymin=mean.1-se.1, ymax=mean.1+se.1), size=1,colour="black", width=.15) +
  geom_point( size = 10) +
  scale_shape_manual(labels=c("+ Methionine", "- Methionine"),
                     values = c(21,22))+
  scale_fill_manual(name=bquote(bold(paste(alpha, " "))), values = cols)

1 个答案:

答案 0 :(得分:6)

你需要告诉ggplot使用shape = 21作为图例,这样它就会使用一个注意填充规范的点,即

scale_fill_manual(name=bquote(bold(paste(alpha, " "))),
                     values = cols,
                     guide=guide_legend(override.aes=list(shape=21))

完整示例:

cols <- c("red","blue")
set.seed(101)
library(ggplot2)
mydata <- data.frame(ID=rep(c("KO", "WT"), each=4),
     O=rep(c("HP", "NN"), each=2, times=2),
  Methionine=rep(c("- Methionine", "+ Methionine"), each=2, times=2), 
      mean.1=sample(50:100, 8),
  se.1=runif(8, min=1, max=3))

gg0 <- ggplot(mydata,
   aes(x=O, y=mean.1, ymax=max(mean.1), shape=Methionine, fill=ID)) + 
   geom_errorbar(aes(ymin=mean.1-se.1, ymax=mean.1+se.1),
            size=1,colour="black", width=.15) +
   geom_point( size = 10)+
   scale_shape_manual(labels=c("+ Methionine", "- Methionine"),
                 values = c(21,22))

  gg0 + 
   scale_fill_manual(name=bquote(bold(paste(alpha, " "))),
                     values = cols,
                     guide=guide_legend(override.aes=list(shape=21))

enter image description here

相关问题