【问题标题】:R / ggplot2: Stuck with defining colorsR / ggplot2:坚持定义颜色
【发布时间】:2016-05-29 08:07:51
【问题描述】:

我是 R 新手。我的目标是创建一个带有回归线和置信椭圆的漂亮 xy 散点图。我已经设法自学了如何使用 ggplot2 创建 xy 图、回归线和圆,但现在我被颜色困住了。

在我的随机数示例中,我有 4 个数据组,我想更改颜色。我设法使用 colorbrewer 更改了它们,但我不喜欢它们的任何预设并想定义我自己的。

这是我目前使用 colorbrewer 的代码:

library(ggplot2)
data = read.csv("test.csv")
data$type <- factor(data$type, levels=c("bbb","ccc","aaa","ddd"))
g = aes(x=alpha, y=beta, color=type, group=type)
p_test <- ggplot(subset(data, id %in% c("a1", "b1"))) +
    geom_point(g) +
    stat_ellipse(g) +
    geom_smooth(g, method=lm, se=FALSE) +
    scale_color_brewer(palette="Set3") +
    expand_limits(x=c(1,11), y=c(1,11)) +
    xlab(expression(paste(italic(blah[p]), " in q ", r^-1))) +
    ylab(expression(paste(lambda, " in K ", l^-2*M^-3))) +
    ggtitle("blahblah") +
    theme(legend.justification=c(0,1), legend.position=c(0,1), legend.title=element_blank())

数据表是这样的:

> data

  id alpha beta type
1  a1   1.2  1.1  aaa
2  a1   6.6  3.4  bbb
3  a1   3.7  6.4  ccc
4  a1   2.7  6.4  aaa
5  a1   5.3  6.3  bbb
6  a1   4.5  8.1  ccc
7  a1  10.5  9.0  bbb
8  a1   9.9  5.3  aaa
9  a1   2.8  5.3  bbb
10 a1   5.4  1.6  ccc
11 a1   3.4 10.7  aaa
12 a1   8.0  4.0  bbb
13 a1   6.1  6.5  ccc
14 a1   8.8  9.0  bbb
15 a1   8.1  1.1  aaa
16 a1   4.2  3.8  bbb
17 a1   9.1  1.2  ccc
18 a1   4.4  2.3  aaa
19 a1   8.8  7.8  bbb
20 a1  11.0  2.5  ccc
21 a1   7.0  9.4  bbb
22 b1   9.5  7.8  ddd
23 b1   7.6  4.5  ddd
24 b1  10.8  7.5  ddd
25 b1   5.6  4.5  ddd
26 b1   8.9 11.0  ddd
27 c1   8.1  7.0  fff
28 c1   7.5  9.3  fff
29 c1   3.1  6.0  ggg

然后我尝试更改颜色。这就是我的问题开始的地方:

1.) 首先我想尝试彩虹渐变。所以我换了

scale_color_brewer(palette="Set3") +

scale_colour_gradientn(colours=rainbow(4)) +

这给了我一个错误“离散值提供给连续规模”。 这是什么意思?

2.) 接下来,我尝试使用

定义固定颜色
scale_fill_manual(values=c("red", "blue", "green", "orange")) +

这返回了一个绘图,但没有使用定义的颜色。它们看起来像默认值? 为什么?

3.) 最后,我尝试定义自己的渐变。我希望它的范围从黄色到红色。所以我用了

scale_colour_gradientn(colours=c("yellow", "red")) +

这又导致了一个错误:离散值提供给连续规模。

然后我放弃了。

有人可以告诉我我做错了什么吗?为什么 colorbrewer 可以工作,而其他的却不行?

【问题讨论】:

  • 你想要#2,但它应该是scale_colour_manual,而不是scale_fill_manual,因为你试图设置颜色,而不是填充。渐变不起作用,因为您传递的是分类数据 (type),因此没有定义渐变的数字。
  • 无关:您只需要在ggplot 中设置一次映射 (aes),其余的都将继承,除非您用新的映射覆盖。

标签: r colors ggplot2 gradient


【解决方案1】:

Alistaire 已经回答了这个问题。由于美学color 映射到factor 变量,因此 ggplot 期望色标在本质上是离散的(如上面的#2)。

所以试试这个:

library(ggplot2)
data = read.csv("test.csv")
data$type <- factor(data$type, levels=c("bbb","ccc","aaa","ddd"))
g = aes(x=alpha, y=beta, color=type, group=type)
p_test <- ggplot(subset(data, id %in% c("a1", "b1"))) +
  geom_point(g) +
  stat_ellipse(g) +
  geom_smooth(g, method=lm, se=FALSE) +
  expand_limits(x=c(1,11), y=c(1,11)) +
  xlab(expression(paste(italic(blah[p]), " in q ", r^-1))) +
  ylab(expression(paste(lambda, " in K ", l^-2*M^-3))) +
  ggtitle("blahblah") +
  theme(legend.justification=c(0,1), legend.position=c(0,1), legend.title=element_blank())


# These work fine
p_test + scale_color_manual(values = rainbow(4))
p_test + scale_color_manual(values = c("red", "blue", "green", "orange"))
p_test + scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set3"))
p_test + scale_color_discrete(h = c(0,360), c = 100, l = 50)

# This wont work
p_test + scale_color_gradient(low = "blue", high = "red")

错误Error: Discrete value supplied to continuous scale 表示色标本质上是连续的,但变量本质上是离散的 (factor)。

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多