2013-11-02 53 views
0

我似乎无法用填充 boxlot通过使用颜色brewer的连续值,并且我知道它必须只是一个简单的语法交换,因为我可以根据连续值获取框的轮廓以进行调整。这里是我正在使用的数据:ggplot boxplot + fill + color brewer spectrum

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

我想打一个镶板箱线图,以中位数“值”命令,用颜色的深浅填写每个框“值”增加(我知道这是多余的,但我承担的例子的缘故)

(订货数据):

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

如果我创建面板箱线图,我可以调整轮廓的颜色:

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

但我想做我对轮廓所做的事情,而是用填充每个框的(所以每个框随着“值”的增加而变暗)。我想,这可能是把“scale_colour_brewer()”为geom_boxplot美学参数中参数的问题,即

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

,但似乎并没有这样的伎俩。我知道这是“scale_colour_brewer”参数的定位问题;我只是不知道它在哪里!

(这里有一个类似的问题,但它不是相当正是我要找的,因为箱子的颜色不根据频谱/梯度一些连续值增加,它看起来像这些值基本上是因素:Add color to boxplot - "Continuous value supplied to discrete scale" error,并在汽车包装ggplot网站的示例: http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html似乎不工作时,我将“填充”设置为“值”...我得到的错误: 错误的单位(tic_pos。 “mm”):'x'和'单位'的长度必须大于0) )

回答

0

如果您需要设置箱型填充而不是color=animals使用fill=animal秒并以相同的方式替换scale_color_brewer()scale_fill_brewer()

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

enter image description here