【问题标题】:ggplot2 using scale_colour_manual access default colors?ggplot2 使用 scale_colour_manual 访问默认颜色?
【发布时间】:2012-09-02 02:28:51
【问题描述】:

我正在使用scale_colour_manual 指定我需要的可能颜色。但是,如果我选择red,我会得到令人眼花缭乱的红色,而不是如果我首先不使用scale_colour_manual 会出现的较温和的ggplot2 默认红色。访问 ggplot2 默认调色板的标签或常量是什么?

 ggplot(data=df, aes(x=n, y=rt, group=kernel, shape=kernel, colour=kernel)) + 
     geom_point(fill="white", size=3) + geom_line() + xlab("n") + ylab("Runtime (s)") + 
     opts(title=title,plot.title=theme_text(size=font.size)) + 
     scale_colour_manual(values=c("grey30", "red")) + 
     opts(legend.position = c(x_shift,0.87),legend.background=theme_rect(fill = "transparent",colour=NA))

请注意,使用“red30”是行不通的,它只适用于灰色,原因我不知道。

【问题讨论】:

标签: r colors ggplot2


【解决方案1】:

red30 不起作用,因为没有只有 30% 的 颜色 的概念,尽管有 30% 的灰色阴影的概念(即 30%黑白之间。)

如果您想要scale_color_manual 的不刺眼的红色,您需要在RGB 中指定颜色,即。通过使用三个字节来表示十六进制的红色绿色和蓝色。像这样的东西应该可以工作

ggplot(data=df, aes(x=n, y=rt, group=kernel, shape=kernel, colour=kernel)) + 
 geom_point(fill="white", size=3) + geom_line() + xlab("n") + ylab("Runtime (s)") + 
 opts(title=title,plot.title=theme_text(size=font.size)) + 
 scale_colour_manual(values=c("grey30", "#EF8A62")) + 
 opts(legend.position = c(x_shift,0.87),legend.background=theme_rect(fill =  "transparent",colour=NA))

如果您不知道如何使用这种颜色编码,请参阅this。我不知道如何以编程方式提取 ggplot 使用的颜色,但您始终可以将使用标准颜色生成的图加载到某些图像编辑器(例如 Gimp)中,然后找出确切的颜色代码是什么。你可以在colorbrewer 上找到好的配色方案,但请注意ggplot2 已经有一个功能:scale_brewer

【讨论】:

  • 要找到默认的 ggplot 颜色,请参阅我上面评论中的链接。
  • 'rgb' 允许透明度值。
【解决方案2】:

ggplot2 中使用的默认“粉红色”和“粉蓝色”分别具有十六进制代码#f8766d#00b0f6,RGB 代码分别为248,118,1090,176,246。使用scale_colour_manual 指定这些:

scale_colour_manual(values=c("#f8766d", "#00b0f6"))

【讨论】:

    猜你喜欢
    • 2018-09-13
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多