本文介绍了ggplot2 multiple stat_binhex()在一幅图像中绘制不同颜色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想使用ggplot2的stat_binhex()在同一个图表上同时绘制两个独立变量,每个变量都使用scale_colour_gradientn()自己的颜色渐变。如果我们不考虑x轴单位不匹配的事实,一个可重现的例子是在保持单独的填充梯度的同时在同一图像中绘制以下图形。 $ b / p> d stat_binhex =white,na.rm = TRUE)+ scale_fill_gradientn(colors = c(white,blue),name =Frequency,na.value = NA) try(ggsave (plot = d,filename =< some file>,height = 6,width = 8)) d stat_binhex(color =white,na.rm = TRUE)+ scale_fill_gradientn( (ggsave(plot = d,filename =<其他文件>,height = 6,宽度= 8)) 我在ggplot2谷歌组此处找到了一些相关问题的对话。解决方案这是另一种可能的解决方案:我已经采用了@ mnel的bin计数映射到alpha透明的想法,并且我已经将x变量所以它们可以绘制在相同的轴上。 library(ggplot2) #数据为0,1。 rangeTransform = function(x)(x - min(x))/(max(x) - min(x)) dat = diamonds dat $ norm_carat = rangeTransform(dat $ carat) dat $ norm_depth = rangeTransform(dat $ depth) p1 = ggplot(data = dat)+ theme_bw()+ stat_binhex(aes(x = norm_carat,y = price,alpha = .. count ..),fill =#002BFF)+ stat_binhex(aes(x = norm_depth,y = price,alpha = ... count ..),fill =#FFD500)+ guides(fill = FALSE,alpha = FALSE)+ xlab(范围转换单位) ggsave(情节= p1,filename =plot_1.png,height = 5,width = 5) 想法: 我尝试(并失败)显示明智的颜色/ alpha图例。看起来很棘手,但应该有可能给予ggplot2的所有图例定制功能。 X轴单元标签需要某种解决方案。在一个坐标轴上绘制两组单位是被许多人所忽视的,而ggplot2没有这样的特征。 解释颜色重叠的单元格似乎足够清晰例如,但可能会变得非常混乱,取决于所使用的数据集以及所选的颜色。 如果这两种颜色是可补的补充,会看到中性的灰色。在重叠不等的地方,灰色将变为更黄或更蓝。我的颜色并不完全相辅相成,从灰色重叠单元格的略带粉红色的色调来看。 https://i.stack.imgur.com/dNF3T.pngalt =在这里输入图片描述> I'd like to use ggplot2's stat_binhex() to simultaneously plot two independent variables on the same chart, each with its own color gradient using scale_colour_gradientn().If we disregard the fact that the x-axis units do not match, a reproducible example would be to plot the following in the same image while maintaining separate fill gradients.d <- ggplot(diamonds, aes(x=carat,y=price))+ stat_binhex(colour="white",na.rm=TRUE)+ scale_fill_gradientn(colours=c("white","blue"),name = "Frequency",na.value=NA)try(ggsave(plot=d,filename=<some file>,height=6,width=8))d <- ggplot(diamonds, aes(x=depth,y=price))+ stat_binhex(colour="white",na.rm=TRUE)+ scale_fill_gradientn(colours=c("yellow","black"),name = "Frequency",na.value=NA)try(ggsave(plot=d,filename=<some other file>,height=6,width=8))I found some conversation of a related issue in ggplot2 google groups here. 解决方案 Here is another possible solution: I have taken @mnel's idea of mapping bin count to alpha transparency, and I have transformed the x-variables so they can be plotted on the same axes.library(ggplot2)# Transforms range of data to 0, 1.rangeTransform = function(x) (x - min(x)) / (max(x) - min(x))dat = diamondsdat$norm_carat = rangeTransform(dat$carat)dat$norm_depth = rangeTransform(dat$depth)p1 = ggplot(data=dat) + theme_bw() + stat_binhex(aes(x=norm_carat, y=price, alpha=..count..), fill="#002BFF") + stat_binhex(aes(x=norm_depth, y=price, alpha=..count..), fill="#FFD500") + guides(fill=FALSE, alpha=FALSE) + xlab("Range Transformed Units")ggsave(plot=p1, filename="plot_1.png", height=5, width=5)Thoughts:I tried (and failed) to display a sensible color/alpha legend. Seems tricky, but should be possible given all the legend-customization features of ggplot2.X-axis unit labeling needs some kind of solution. Plotting two sets of units on one axis is frowned upon by many, and ggplot2 has no such feature.Interpretation of cells with overlapping colors seems clear enough in this example, but could get very messy depending on the datasets used, and the chosen colors.If the two colors are additive complements, then wherever they overlap equally you will see a neutral gray. Where the overlap is unequal, the gray would shift to more yellow, or more blue. My colors are not quite complements, judging by the slightly pink hue of the gray overlap cells. 这篇关于ggplot2 multiple stat_binhex()在一幅图像中绘制不同颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:44