ggplot箱图+填充+啤酒酿造光谱 [英] ggplot boxplot + fill + color brewer spectrum

查看:51
本文介绍了ggplot箱图+填充+啤酒酿造光谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用color brewer通过连续值来填充箱形图,而且我知道它必须只是语法的简单交换,因为我可以得到轮廓可以根据连续值进行调整的框.这是我正在使用的数据:

I can't seem to be able to fill a boxplot by a continuous value using color brewer, and I know it must just be a simple swap of syntax somewhere, since I can get the outlines of the boxes to adjust based on continuous values. Here's the data I'm working with:

data <- data.frame(
    value = sample(1:50),
    animals = sample(c("cat","dog","zebra"), 50, replace = TRUE),
    region = sample(c("forest","desert","tundra"), 50, replace = TRUE)
)

我想制作一个面板式箱形图,按中值值"排序,每个框的颜色填充深度随值"增加(我知道这是多余的,但出于示例的考虑,请耐心等待)

I want to make a paneled boxplot, ordered by median "value", with the depth of color fill for each box increasing with "value" (I know this is redundant, but bear with me for the sake of the example)

(订购数据):

orderindex <- order(as.numeric(by(data$value, data$animals, median)))
data$animals <- ordered(data$animals, levels=levels(data$animals)[orderindex])

如果使用面板创建箱形图,则可以调整轮廓的颜色:

If I create the boxplot with panels, I can adjust the color of the outlines:

library(ggplot2)
first <- qplot(animals, value, data = data, colour=animals)
second <- first + geom_boxplot() + facet_grid(~region)
third <- second + scale_colour_brewer()
print(third)

但是我想对轮廓进行处理,而是使用每个框的 fill (这样,随着值"的增加,每个框都会变暗).我认为这可能是将"scale_colour_brewer()"参数放入geom_boxplot的美学参数之内的问题,即

But I want to do what I did to the outlines, but instead with the fill of each box (so each box gets darker as "value" increases). I thought that it might be a matter of putting the "scale_colour_brewer()" argument within the aesthetic argument for geom_boxplot, ie

second <- first + geom_boxplot(aes(scale_colour_brewer())) + facet_grid(~region)

但这似乎并不能解决问题.我知道这是"scale_colour_brewer"参数的定位问题;我只是不知道它会去哪里!

but that doesn't seem to do the trick. I know it's a matter of positioning for this "scale_colour_brewer" argument; I just don't know where it goes!

(这里有一个类似的问题,但不是我想要的相当,因为盒子的颜色不会沿着带有连续值的光谱/梯度增加;它看起来像这些值基本上是因素:添加颜色转换为箱形图-连续值提供给离散刻度"错误,并在ggplot站点上使用汽车包装的示例: http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html当我将"fill"设置为"value"时似乎不起作用...我得到了错误:单位错误(tic_pos.c,"mm"):"x"和"units"的长度必须大于0))

(there is a similar question here but it's not quite what I'm looking for, since the colors of the box don't increase along a spectrum/gradient with some continuous value; it looks like these values are basically factors: Add color to boxplot - "Continuous value supplied to discrete scale" error, and the example at the ggplot site with the cars package: http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html doesn't seem to work when I set "fill" to "value" ... I get the error: Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0) )

推荐答案

如果需要为盒图设置填充,请使用 fill = animal 代替 color = animals s和用 scale_fill_brewer()替换 scale_color_brewer()的方法相同.

If you need to set fill for the boxplots then instead of color=animals use fill=animals and the same way replace scale_color_brewer() with scale_fill_brewer().

qplot(animals, value, data = data, fill=animals)+ 
  geom_boxplot() + facet_grid(~region) + scale_fill_brewer()

这篇关于ggplot箱图+填充+啤酒酿造光谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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