本文介绍了ggplot2:多种颜色比例或系统地在不同图层上移动颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! library() ggplot2) library(RColorBrewer) cols = brewer.pal(9,'Set1') $ bn = 10000 dat = data.frame(值= rnorm(n,1:4),group = factor(1:4)) ggplot(dat,aes(x = group,y = value,color = group,group = group) )+ geom_point(position = position_jitter(width = 0.3),alpha = 0.1)+ scale_color_manual(values = cols)+ geom_boxplot(fill = 0,outlier.size = 0) 然而,我不喜欢它在点变得太密集时我的方块图完全消失。我知道我可以调整 alpha ,这在某些情况下是正确的,但是当我的组的密度不同时(例如,如果我减少时最轻的组会完全消失 alpha 足够使最黑暗的组不会遮挡箱形图)。我想要做的是系统地改变箱形图的颜色 - 也许有点黑 - 这样即使背景指向最大值时,它们也会显示出来。例如: plot(1:9,rep(1,9),pch = 19,cex = 2,col = cols)= cols_dk = rgb2hsv(col2rgb(brewer.pal(9,'Set1'))) - c(0,0,0.2) cols_dk = hsv(cols_dk [1,],cols_dk [2 ,],cols_dk [3,]) points(1:9,rep(1.2,9),pch = 19,cex = 2,col = cols_dk) 到目前为止,我还没找到一种伪造 scale_color 为 geom_boxplot layer(如果有办法的话,这看起来是最简单的路线)。我也没有能够找到一个简单的语法来系统地调整颜色,就像你可以很容易地抵消连续审美的方式一样,例如 aes(x = x + 1) p> 我能够得到的最接近的结果是完全重复了这个因素的水平... ggplot(dat,aes(x = group,y = value,color = group,group = group))+ geom_point(position = position_jitter(width = 0.3),alpha = 0.1)+ scale_color_manual(values = c(cols [1:4],cols_dk [1:4]))+ geom_boxplot(aes(color = factor(as.numeric(group)+4) ),fill = 0,outlier.size = 0) 然后我必须处理那个丑陋的传说。现在,你可以定义你自己的版本的GeomBoxplot $ c>(调用它,比如 GeomPlotDark ),这与原来的不同之处在于,它在绘制它们之前首先使颜色变暗。 使用proto,您可以通过创建一个从 c> GeomBoxplot ,并且仅在它的 draw 函数中有所不同。大多数 draw 函数的定义都来自 GeomBoxplot 来源;我注释了我改变过的几行代码,如#** ... ** : require(ggplot2) GeomBoxplotDark< - proto(ggplot2 ::: GeomBoxplot, draw< - function(。,data,...,outlier .colour =black,outlier.shape = 16,outlier.size = 2){默认值 cols_dk< - 添加了行** ** cols_dk data.frame(x = x,xmin = xmin,xmax = xmax, color = cols_dk,#** EDITED,通过cols_dk ** size = size, linetype = 1,group = 1,alpha = 1, fill = alpha(fill,alpha), stringsAsFactors = FALSE )})#* * CLOSING}ADDED ** defaults2 if(!is.null(data $ outliers)&& (数据$ outliers [[1]]> = 1)){ outliers_grob GeomPoint $ draw(data.frame(y = outliers [[1 ]],x = x [rep(1,length(outliers [[1]]))], color = I(outlier.colour),shape = outlier.shape,alpha = 1, size = outlier.size,fill = NA),... ))} else { outliers_grob< - NULL } with(data,ggname(。$ my_name(),grobTree( outliers_grob,)GeomPath $ draw(data.frame(y = c(upper,ymax),defaults2),... ), GeomPath $ draw(data.frame(y = c(lower,ymin),defaults2),...), GeomRect $ draw(data.frame(ymax = upper,ymin = lower ,...), GeomRect $ draw(data.frame(ymax = middle,ymin = middle,defaults),...))))} $ / code> 然后创建一个 geom_boxplot_dark()被用户调用,并且适当地将调用包装为 GeomBoxplotDark $ new(): geom_boxplot_dark outlier.colour =black,outlier.shape = 16,outlier.size = 2, ...) GeomBoxplotDark $ new(mapping = mapping,data = data,stat = stat, position = position,outlier.colour = outlier.colour,outlier.shape = outlier.shape, outlier.size = outlier.size, ...) 最后,使用与原始调用几乎相同的代码尝试它,调用 geom_boxplot_dark()来调用 geom_boxplot(): library(ggplot2) library(RColorBrewer) cols = brewer.pal(9,'Set1') n = 10000 dat = data.frame(value = rnorm(n,1:4),group = factor(1:4)) ggplot(dat,aes(x = group,y = value,color = group,group = group))+ geom_point(position = position_jitter(width = 0.3),alpha = 0.1)+ sca le_color_manual(values = cols)+ geom_boxplot_dark(fill = 0,outlier.size = 0) b $ b When I make box plots, I like to also show the raw data in the background, like this:library(ggplot2)library(RColorBrewer)cols = brewer.pal(9, 'Set1')n=10000dat = data.frame(value=rnorm(n, 1:4), group=factor(1:4))ggplot(dat, aes(x=group, y=value, color=group, group=group)) + geom_point(position=position_jitter(width=0.3), alpha=0.1) + scale_color_manual(values=cols) + geom_boxplot(fill=0, outlier.size=0)However, I don't like it how my box plots completely disappear when the points get too dense. I know I can adjust alpha, which is fine in some cases, but not when my groups have varying densities (For example when the lightest group would completely disappear if I were to decrease alpha enough so that the darkest group doesn't obscure the box plot). What I'm trying to do is systematically shift the colors for the box plots - a bit darker, perhaps - so that they show up even when the background points max out the alpha. For example:plot(1:9, rep(1, 9), pch=19, cex=2, col=cols)cols_dk = rgb2hsv(col2rgb(brewer.pal(9, 'Set1'))) - c(0, 0, 0.2)cols_dk = hsv(cols_dk[1,], cols_dk[2,], cols_dk[3,])points(1:9, rep(1.2, 9), pch=19, cex=2, col=cols_dk)So far I haven't found a way to fake in a different scale_color for the geom_boxplot layer (which would seem the simplest route if there's a way to do it). Nor have I been able to find a simple syntax to systematically adjust the colors the same way you can easily offset a continuous aesthetic like aes(x=x+1).The closest thing I've been able to get is to completely duplicate the levels of the factor...ggplot(dat, aes(x=group, y=value, color=group, group=group)) + geom_point(position=position_jitter(width=0.3), alpha=0.1) + scale_color_manual(values=c(cols[1:4], cols_dk[1:4])) + geom_boxplot(aes(color=factor(as.numeric(group)+4)), fill=0, outlier.size=0)but then I have to deal with that ugly legend. Any better ideas? 解决方案 For now, you could define your own version of GeomBoxplot (calling it, say, GeomPlotDark), differing from the original only in that it first 'darkens' the colors before plotting them.With proto, you can do this by creating a proto object, GeomBoxplotDark, that inherits from GeomBoxplot, and differs only in its draw function. Most of the draw function's definition is taken from the GeomBoxplot sources; I have annotated the lines I changed with comments like this # ** ... **: require(ggplot2)GeomBoxplotDark <- proto(ggplot2:::GeomBoxplot, draw <- function(., data, ..., outlier.colour = "black", outlier.shape = 16, outlier.size = 2) { defaults <- with(data, { # ** OPENING "{" ADDED ** cols_dk <- rgb2hsv(col2rgb(colour)) - c(0, 0, 0.2) # ** LINE ADDED ** cols_dk <- hsv(cols_dk[1,], cols_dk[2,], cols_dk[3,]) # ** LINE ADDED ** data.frame(x = x, xmin = xmin, xmax = xmax, colour = cols_dk, # ** EDITED, PASSING IN cols_dk ** size = size, linetype = 1, group = 1, alpha = 1, fill = alpha(fill, alpha), stringsAsFactors = FALSE )}) # ** CLOSING "}" ADDED ** defaults2 <- defaults[c(1,1), ] if (!is.null(data$outliers) && length(data$outliers[[1]] >= 1)) { outliers_grob <- with(data, GeomPoint$draw(data.frame( y = outliers[[1]], x = x[rep(1, length(outliers[[1]]))], colour=I(outlier.colour), shape = outlier.shape, alpha = 1, size = outlier.size, fill = NA), ... ) ) } else { outliers_grob <- NULL } with(data, ggname(.$my_name(), grobTree( outliers_grob, GeomPath$draw(data.frame(y=c(upper, ymax), defaults2), ...), GeomPath$draw(data.frame(y=c(lower, ymin), defaults2), ...), GeomRect$draw(data.frame(ymax = upper, ymin = lower, defaults), ...), GeomRect$draw(data.frame(ymax = middle, ymin = middle, defaults), ...) ))) })Then create a geom_boxplot_dark() to be called by the user, and which appropriately wraps the call to GeomBoxplotDark$new():geom_boxplot_dark <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", outlier.colour = "black", outlier.shape = 16, outlier.size = 2, ...)GeomBoxplotDark$new(mapping = mapping, data = data, stat = stat, position = position, outlier.colour = outlier.colour, outlier.shape = outlier.shape, outlier.size = outlier.size, ...)Finally, try it out with code almost identical to your original call, just substituting a call to geom_boxplot_dark() for the call to geom_boxplot():library(ggplot2)library(RColorBrewer)cols = brewer.pal(9, 'Set1')n=10000dat = data.frame(value=rnorm(n, 1:4), group=factor(1:4))ggplot(dat, aes(x=group, y=value, color=group, group=group)) + geom_point(position=position_jitter(width=0.3), alpha=0.1) + scale_color_manual(values=cols) + geom_boxplot_dark(fill=0, outlier.size=0)I think the resulting plot looks pretty nifty. With a bit of tweaking, and viewed directly (not as an uploaded file), it'll look awesome: 这篇关于ggplot2:多种颜色比例或系统地在不同图层上移动颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:32