本文介绍了ggplot:对于覆盖相同分组的两个geom,换成不同的色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个问题来自另一个问题:的想法,让我说明一下: library(ggplot2) library(RColorBrewer) library(reshape2) library(gridExtra) library(gtable) #补充数据a = rnorm(100,mean = 1)b = rnorm(100,mean = 0,sd = 1) ab = data.frame (a,b) melt = melt(ab) bpColor = brewer.pal(4,'RdBu') ##创建boxplot g1< ; - ggplot(melt,aes(fill = variable,x = variable,y = value))+ geom_boxplot(notch = TRUE)+ scale_fill_hue(l = 40)+ theme_bw ) ##用较淡的色调创建一个点图 g2 geom_jitter(position = position_jitter(width = .05,height = 0),shape = 21,size = 3)+ scale_fill_hue(l = 100)+ theme(panel.background = element_rect(fill = NA)) gt_boxes< - ggplot_gtable(ggplot_build(g1)) gt_p oint< - ggplot_gtable(ggplot_build(g2)) ##覆盖框上的点 just_boxes< - c(subset(gt_boxes $ layout,name ==panel, se = t:r)) g_together< - gtable_add_grob(gt_boxes,gt_points $ grobs [[which(gt_points $ layout $ name ==panel)]], just_boxes $ t,just_boxes $ l,just_boxes $ b,just_boxes $ l) grid.draw(g_together) This question comes from another question: Different coloring of groups in R plotI'd like to elarn to change the hue of one geom without affecting another. So here the boxplot grouping and point grouping map to the same variable. Changing the hue for one changes the other. How could I change the hue for the box fill but not the point fill; in other words make the point fill lighter in color so they stand out against the same color of the box fill?#Datalibrary(RColorBrewer) library(reshape2)a=rnorm(100, mean=1)b=rnorm(100, mean=0, sd=1)ab=data.frame(a,b)melt=melt(ab)bpColor=brewer.pal(4, 'RdBu')#Currentggplot(melt, aes(fill=variable, x=variable, y=value)) + geom_boxplot(notch = TRUE) + geom_jitter(position = position_jitter(width = .05, height =0), shape=21, size=1.5) + scale_fill_hue(l=40) 解决方案 It may be simpler to create a boxplot and then a pointplot with a lighter hue, and overlay the points over the boxes. Borrowing the ideas from this post, let me illustrate:library(ggplot2)library(RColorBrewer) library(reshape2)library(gridExtra)library(gtable)# make up dataa=rnorm(100, mean=1)b=rnorm(100, mean=0, sd=1)ab=data.frame(a,b)melt=melt(ab)bpColor=brewer.pal(4, 'RdBu')## create a boxplotg1 <- ggplot(melt, aes(fill=variable, x=variable, y=value)) + geom_boxplot(notch = TRUE) + scale_fill_hue(l=40) + theme_bw()## create a pointplot with a lighter hueg2 <- ggplot(melt, aes(fill=variable, x=variable, y=value)) + geom_jitter(position = position_jitter(width = .05, height =0), shape=21, size=3) + scale_fill_hue(l=100) + theme(panel.background = element_rect(fill = NA))gt_boxes <- ggplot_gtable(ggplot_build(g1))gt_points <- ggplot_gtable(ggplot_build(g2))## overlay the points over the boxesjust_boxes <- c(subset(gt_boxes$layout, name == "panel", se = t:r))g_together <- gtable_add_grob(gt_boxes, gt_points$grobs[[which(gt_points$layout$name == "panel")]], just_boxes$t, just_boxes$l, just_boxes$b, just_boxes$l)grid.draw(g_together) 这篇关于ggplot:对于覆盖相同分组的两个geom,换成不同的色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 21:02