更改ggplot中点的颜色填充和形状 [英] Change color fill and shape of points in ggplot

查看:4340
本文介绍了更改ggplot中点的颜色填充和形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change shape and color fill of points in ggplot, this is my code that worked partially, color legend is still black.

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)


推荐答案

您需要告诉ggplot使用shape = 21作为图例,以便它使用注意填充规范的一点,即

You need to tell ggplot to use shape=21 for the legend, so that it uses a point that pays attention to the fill specification, i.e.

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

完整示例:

Full example:

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))

这篇关于更改ggplot中点的颜色填充和形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆