为箱线图添加颜色 - “提供给离散比例的连续值"错误 [英] Add color to boxplot - "Continuous value supplied to discrete scale" error

查看:28
本文介绍了为箱线图添加颜色 - “提供给离散比例的连续值"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可能有一个非常简单的解决方案,但我在网上找不到令人满意的答案.

There is probably a very easy solution to my problem but I couldn't find a satisfying answer online.

使用以下命令,我能够创建以下箱线图并将其与各个数据点叠加:

Using the following command I was able to create the following boxplot graph and overlay it with the individual data points:

ggplot(data = MYdata, aes(x = Age, y = Richness)) + 
  geom_boxplot(aes(group=Age)) + 
  geom_point(aes(color = Age))

我想添加/更改几件事情:

There are several things I would like to add/change:

1. 使用从左到右的 6 种不同颜色更改每个箱线图的线条颜色和/或填充(取决于年龄"):

1. Change the line color and/or fill of each boxplot (depending on "Age") using 6 different colors from left to right:

c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00")

我试过了

ggplot(data = MYdata, aes(Age, Richness)) + 
  geom_boxplot(aes(group=Age)) + 
  scale_colour_manual(values = c("#E69F00", "#56B4E9", "#009E73", 
                                 "#F0E442", "#0072B2", "#D55E00")) 

但它会导致 提供给离散尺度的连续值" 错误.

2. 使用从左到右的 6 种不同颜色更改每个数据点的颜色(取决于年龄"):

2. Change the color of each data point (depending on "Age") using 6 different colors from left to right:

c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00")

我试过:

ggplot(data = MYdata, aes(Age, Richness)) + 
  geom_boxplot(aes(group=Age)) + 
  geom_point(aes(color = Age)) + 
  scale_colour_manual(values = c("#E69F00", "#56B4E9", "#009E73", 
                                 "#F0E442", "#0072B2", "#D55E00")) 

但它也会导致错误:

提供给离散尺度的连续值

Continuous value supplied to discrete scale

3.将图例中的文字改为0个月"、1个月"、3个月"、6个月"、9个月"、12个月"

3. Change the text in the legend to "0 month", "1 month", "3 months", "6 months", "9 months", "12 months"

推荐答案

首先,提供示例数据会有所帮助.既然你没有,这里有一些:

First, providing sample data would help. Since you didn't, here is some:

MYdata <- data.frame(Age = rep(c(0,1,3,6,9,12), each=20),
                    Richness = rnorm(120, 10000, 2500))

第 1 部分和第 2 部分源于相同的问题.Age 是一个连续变量,但您试图以离散比例使用它(通过指定特定年龄值的颜色).通常,比例尺将变量映射到视觉;对于连续年龄,每个可能的年龄值都有相应的颜色,而不仅仅是数据中碰巧出现的颜色.但是,您可以同时将年龄视为某些美学的分类变量(因素).对于问题的第三部分,在比例描述中,您可以定义与比例中的特定中断相对应的特定标签.将所有这些放在一起(并添加一些东西,使 x 轴标记得更像示例中的内容):

Parts 1 and 2 stem from the same problem. Age is a continuous variable, but you are trying to use it in a discrete scale (by specifying the color for specific values of age). In general, a scale maps the variable to the visual; for a continuous age, there is a corresponding color for every possible value of age, not just the ones that happen to appear in your data. However, you can simultaneously treat age as a categorical variable (factor) for some of the aesthetics. For the third part of your question, within the scale description, you can define specific labels corresponding to specific breaks in the scale. Putting this all together (and adding something to give you the x axis labelled more like what you have in the example):

ggplot(data = MYdata, aes(x = Age, y = Richness)) + 
  geom_boxplot(aes(fill=factor(Age))) + 
  geom_point(aes(color = factor(Age))) +
  scale_x_continuous(breaks = c(0, 1, 3, 6, 9, 12)) +
  scale_colour_manual(breaks = c("0", "1", "3", "6", "9", "12"),
                      labels = c("0 month", "1 month", "3 months",
                                 "6 months", "9 months", "12 months"),
                      values = c("#E69F00", "#56B4E9", "#009E73", 
                                 "#F0E442", "#0072B2", "#D55E00")) +
  scale_fill_manual(breaks = c("0", "1", "3", "6", "9", "12"),
                      labels = c("0 month", "1 month", "3 months",
                                 "6 months", "9 months", "12 months"),
                      values = c("#E69F00", "#56B4E9", "#009E73", 
                                 "#F0E442", "#0072B2", "#D55E00"))

使用此配色方案,位于箱线图内的点不可见(因为它们与箱线图的填充颜色相同).也许将箱线图留空并用颜色绘制线条会更好.

With this color scheme, the points that fall inside the boxplot are not visible (since they are the same color as the boxplot's fill). Perhaps leaving the boxplot hollow and drawing its lines in the color would be better.

ggplot(data = MYdata, aes(x = Age, y = Richness)) + 
  geom_boxplot(aes(colour=factor(Age)), fill=NA) + 
  geom_point(aes(color = factor(Age))) +
  scale_x_continuous(breaks = c(0, 1, 3, 6, 9, 12)) +
  scale_colour_manual(breaks = c("0", "1", "3", "6", "9", "12"),
                      labels = c("0 month", "1 month", "3 months",
                                 "6 months", "9 months", "12 months"),
                      values = c("#E69F00", "#56B4E9", "#009E73", 
                                 "#F0E442", "#0072B2", "#D55E00"))

最后,请考虑您是否真的需要为每个年龄涂上不同的颜色,因为 x 轴已经很好地定义了它们.

Finally, consider if you really need to color each age differently, since they are well defined by the x-axis already.

这篇关于为箱线图添加颜色 - “提供给离散比例的连续值"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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