将颜色添加到盒图 - “提供给离散尺度的连续值”错误 [英] Add color to boxplot - "Continuous value supplied to discrete scale" error

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

问题描述

这可能是一个很容易的解决方案,我的问题,但我不能在网上找到满意的答案。



使用以下命令,我可以创建以下boxplot图并用单个数据点覆盖:

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

有几个我想添加/更改的东西:



1。使用从左到右的6种不同颜色更改每个框图的线条颜色和/或填充(取决于年龄 p#

  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))

但会导致连续值提供给离散缩放错误。


$ b b

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



<$ pre> 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))

但也会导致错误:



3。将图例中的文字更改为0个月,1个月 3个月,6个月,9个月,12个月

解决方案

。由于你没有,这里有一些:

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

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

  ggplot (data = MYdata,aes(x = Age,y = Richness))+ 
geom_boxplot(aes(fill = factor(Age)))+
geom_point +
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个月,1个月,3个月,
6个月,9个月 ),
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个月,
6个月,9个月,12个月),
值= c(#E69F00,#56B4E9,#009E73 $ b#F0E442,#0072B2,#D55E00))



使用此配色方案,落在箱线图内是不可见的(因为它们与箱线图的填充颜色相同)。

  ggplot(data = MYdata,aes(x = Age,y = Richness))+ 
geom_boxplot(aes(color = 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个月,1个月,3个月,
6个月,9个月,12个月 $ b values = c(#E69F00,#56B4E9,#009E73,
#F0E442,#0072B2,#D55E00))



最后,考虑你是否真的需要对每个年龄不同的颜色,因为它们已经由x轴很好地定义。


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. 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")

I tried

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

but it results in a "Continuous value supplied to discrete scale" error.

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")

I tried:

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")) 

but it also results in an error:

Continuous value supplied to discrete scale

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))

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"))

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

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

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