更改ggplot2中两个geom_point()的颜色 [英] change color for two geom_point() in ggplot2

查看:4391
本文介绍了更改ggplot2中两个geom_point()的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例数据集:

  library(ggplot2)
df = read.table(text =
编号年值1价值2价值3
1 2000 1 2000 2001
1 2001 2不适用
1 2002 2 2000不适用b $ b 1 2003 2不适用2003
2 2000 1 2001 2003
2 2002 2 NA 2000
2 2003 3 2002 NA
3 2002 2 2001 NA
,sep =,header = TRUE)
df $ value1 < - as.factor(df $ value1)

我知道如何更改因子变量的颜色有三个级别:

  p < -  ggplot(df,aes(y = id))
p < - p + scale_colour_manual(name =,values = c(yellow,orange,red))
p < - p + geom_point(aes(x = year,color = value1) 4)
p


我也可以更改两个数字变量的颜色:

 <$ c (value =grep,value2=grey,value =,value = c(value3=gray (黑色)))
p <-p + geom_point(aes(x = value3,color ='value3'),size = 3)
p <-p + geom_point(aes(x = value2, color ='value2'),size = 5)
p

但我不知道在同一图表中更改两者的颜色?
是否也适用于scale_color_manual?

  p < -  last_plot()+ geom_point(aes(x = year ,color = value1))
p


解决方案

这是你在找什么?

  ggplot(df,aes(y = id))+ 
geom_point(aes (x = year,color = value1),size = 4)+
geom_point(aes(x = value3,color ='value3'),size = 3)+
geom_point(aes(x = value2 ,color ='value2'),size = 5)+
scale_colour_manual(name =,
values = c(1=yellow,2=orange,3 =red,
value3=gray,value2=black))



基本上,只需将所有可能的颜色标签放在一个列表中即可。


Sample dataset:

library(ggplot2)    
df = read.table(text = 
              "id      year    value1  value2 value3
            1           2000    1   2000      2001
            1           2001    2   NA        NA
            1           2002    2   2000      NA
            1           2003    2   NA         2003
            2           2000    1   2001     2003
            2           2002    2   NA       2000
            2           2003    3   2002     NA
            3           2002    2   2001     NA
        ", sep = "", header = TRUE)
df$value1 <- as.factor(df$value1)

I know how to change color for a factor variable with three levels:

p <- ggplot(df, aes(y=id))
p <- p + scale_colour_manual(name="",  values =c("yellow", "orange", "red"))
p <- p + geom_point(aes(x=year, color=value1), size=4)
p

I can also change the color for two numeric variables:

p <- ggplot(df, aes(y=id))
p <- p + scale_colour_manual(name="",  values =c("value3"="grey", "value2"="black"))
p <- p + geom_point(aes(x=value3, colour ='value3'), size=3)
p <- p + geom_point(aes(x=value2, colour ='value2'), size=5)
p

But I do not know how to change the color for both in the same graph? Does it also work with scale_color_manual?

p <- last_plot() + geom_point(aes(x=year, color=value1))
p

解决方案

Is this what you are looking for?

ggplot(df, aes(y=id)) +
  geom_point(aes(x=year, color=value1), size=4) +
  geom_point(aes(x=value3, colour ='value3'), size=3) +
  geom_point(aes(x=value2, colour ='value2'), size=5) +
  scale_colour_manual(name="",  
                      values = c("1"="yellow", "2"="orange", "3"="red",
                                 "value3"="grey", "value2"="black"))

Basically, just putting all possible colour labels in a single list.

这篇关于更改ggplot2中两个geom_point()的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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