R或ggplot2中的默认绘图颜色是什么? [英] What are the default plotting colors in R or ggplot2?

查看:2072
本文介绍了R或ggplot2中的默认绘图颜色是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制ggplot2,并且想添加一些颜色与点相同的线。任何人都可以告诉我在R中绘制时默认的颜色代码是什么?例如,以下6种颜色的代码是什么:

  df<  -  structure(list(type = structure(1 :6,.Label = c(a,b,c,
d,e,f),class =factor),value = 1:6) ,.Names = c(type,
value),class =data.frame,row.names = c(NA,-6L))

library(ggplot2 )
ggplot(df,aes(x = value,y = value,color = type))+ geom_point(shape = 21,size = 4)

谢谢!

解决方案

你可以使用函数 ggplot_build(),然后查看这个对象的数据部分(在 color 是代码中) 。

  p < -  ggplot(df,aes(x = value,y = value,color = type))+ 
geom_point(shape = 21,size = 4)

ggplot_build(p)$ data
#[[1]]
#color xy面板组
# 1#F8766D 1 1 1 1
#2#B79F00 2 2 1 2
#3#00BA38 3 3 1 3
#4#00BFC4 4 4 1 4
#5# 619CFF 5 5 1 5
#6#F564E3 6 6 1 6


I'm plotting in ggplot2 and want to add some lines that are colored the same as the points. Can anyone tell me what the default color codes are when plotting in R? For example, what are the codes for the following 6 colors:

df <- structure(list(type = structure(1:6, .Label = c("a", "b", "c", 
"d", "e", "f"), class = "factor"), value = 1:6), .Names = c("type", 
"value"), class = "data.frame", row.names = c(NA, -6L))

library(ggplot2)
ggplot(df, aes(x=value, y=value, color=type)) + geom_point(shape=21, size=4)

Thanks!

解决方案

To see what colors are used to make your plot you can use function ggplot_build() and then look at data part of this object (in column colour are codes).

p <- ggplot(df, aes(x = value, y = value, color = type)) +
    geom_point(shape = 21, size = 4)

ggplot_build(p)$data
# [[1]]
#    colour x y PANEL group
# 1 #F8766D 1 1     1     1
# 2 #B79F00 2 2     1     2
# 3 #00BA38 3 3     1     3
# 4 #00BFC4 4 4     1     4
# 5 #619CFF 5 5     1     5
# 6 #F564E3 6 6     1     6

这篇关于R或ggplot2中的默认绘图颜色是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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