本文介绍了R中的ggplot2:在两个不同的`geom_smooth`行下使用`geom_ribbon`进行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用 我希望隐藏在 geom_smooth 行之下,如下所示: 或者只在粉红色的线条下有这些颜色,并且两行下的所有内容都是深灰色的。 我使用这段代码创建图表: p3 geom_point()+ theme_fivethirtyeight_mod()+ ggtitle('Seconds vs. Age')+ geom_hline(yintercept = 0,size = 1.2 ,color =#535353)+ geom_vline(xintercept = 0,size = 1.2,color =#535353)+ geom_smooth(se = F)+ geom_ribbon ymin = 0,ymax =预测(黄土(AGE〜SECONDS))),alpha = 1) theme_fivethirtyeight_mod()的代码如下: require(ggplot2) require(ggthemes) require(ggrepel) require(grid) require(gtable) theme_fivethirtyeight_mod< - function(base_size = 12, base_family = SANS){(theme_foundation(base_size = base_size,base_family = base_family)+ 主题(线= element_line(颜色= 黑), RECT = element_rect(填充= ggthemes_data $ fivethirtyeight [ltgray],linetype = 0,color = NA), text = element_text(color = ggthemes_data $ fivethirtyeight [dkgray]] ), axis.text = element_text(size = 11,color = ggthemes_data $ fivethirtyeight [dkgray],face =bold), axis.ticks = element_blank(), axis.line = element_blank(), axis.title = element_text(size = 11,color = ggthemes_data $ fivethirtyeight [dkgray],face =bold,vjust = 1.5), legend。 title = element_blank(), legend.background = element_rect(fill =gray90,size = .5,linetype =dotted), legend.position =bottom, legend.direction =horizontal, legend.box =vertical, panel.grid = element_line(color = NULL), panel.grid.major = element_line(color = ggthemes_data $ fivethyeyeight [medgray]), panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0.05,size = rel(1.5),face =bold) , plot.margin =单位(c(1,1,1,1),lines), panel.background = element_rect(fill =#F0F0F0), plot.background = element_rect(fill =#F0F0F0), panel.border = element_rect(color =#F0F0F0), strip.background = element_rect()))} 感谢您的所有帮助! 编辑 : @MLavoie评论了一个问题的链接,它给了我一个关于如何在 geom_smooth 使用预测(黄土(AGE〜SECONDS))行。 predict()可以像 geom_smooth 和 loess 当 n 。这使我能够遮蔽男性和女性的线条,但不允许我在两条曲线下找到区域。深灰色阴影区域是整个数据集 geom_smooth 下的区域。 我怀疑要找到我首先需要捕捉来自 geom_smooth (男性和女性)的数据的男性和女性曲线下的区域。然后,我将创建一个 data.frame ,其中x值为行,每列y值为一列。我会找到每个x值的最小y值,并且我会遮蔽该曲线下面的深灰色。有趣的是,阴影区域在光线蓝色,像点,图例显示填充了深灰色的红色或蓝色轮廓框。我将它添加到代码中,而不是原来的 geom_ribbon : geom_ribbon (data = df [df $ GENDER =='F',],aes(ymin = 0,ymax = predict(黄色(AGE〜SECONDS))),alpha = 1,fill =red)+ geom_ribbon(data = df [df $ GENDER =='M',],aes(ymin = 0,ymax = predict(黄色(AGE〜SECONDS))),alpha = 1,fill =blue)+ geom_ribbon(aes(ymin = 0,ymax = predict(黄土(AGE〜SECONDS))),alpha = 1) 这是创建此图所涉及的唯一新代码: 实质上,我想删除已填充区域的蓝色轮廓,并且想要移除深灰色从传说中的方框中填充,如果有人能够弄清楚我会如何去遮蔽它两条线下方的区域。再次感谢!解决方案关闭颜色或填充图例以获得您想要的图例。 $ b 关闭颜色图例: P3< - ggplot(DF,AES(X =秒,Y = AGE,颜色= GENDER))+ geom_point()+ theme_fivethirtyeight_mod()+ ggtitle('秒VS 。年龄')+ geom_hline(yintercept = 0,size = 1.2,color =#535353)+ geom_vline(xintercept = 0,size = 1.2,color =#535353)+ geom_smooth(se = F)+ geom_ribbon(data = df [df $ GENDER =='F',], aes(ymin = 0,ymax =预测(黄土(AGE〜SECONDS )), fill =Female),color = F)+ geom_ribbon(data = df [df $ GENDER =='M',], aes(ymin = 0, ymax =预测(黄土(AGE_秒)), fill =男),color = F)+ geom_ribbon(aes(ymin = 0,ymax = )), color = F)+ scale_fill_manual(values = c('Female'='red','Male'='blue'))+ guides(color = F) 几点要注意: 我不确定为什么要使用第三个 geom_ribbon 。如果您想遮蔽另外两个色带下的区域的交叉点,为黄色区域填充整个数据并不会给您交集 - 您可以观察到,通过使图形不太透明(通过指定 alpha < 1) alpha = 1 ,所以你不需要明确指定。 Using this dataset, I created this graph:I wish to shade under the geom_smooth lines, like so:I want points only under the blue line or only under the pink line to have those colors, and everything under both lines to be dark-grey.I used this code to create the graph:p3 <- ggplot(df, aes(x = SECONDS, y = AGE, color = GENDER)) +geom_point() + theme_fivethirtyeight_mod() + ggtitle('Seconds vs. Age') +geom_hline(yintercept = 0, size = 1.2, colour = "#535353") +geom_vline(xintercept = 0, size = 1.2, colour = "#535353") +geom_smooth(se = F) +geom_ribbon(aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), alpha = 1)The code for theme_fivethirtyeight_mod() is this:require(ggplot2)require(ggthemes)require(ggrepel)require(grid)require(gtable)theme_fivethirtyeight_mod <- function (base_size = 12, base_family = "sans") {(theme_foundation(base_size = base_size, base_family = base_family) + theme(line = element_line(colour = "black"), rect = element_rect(fill = ggthemes_data$fivethirtyeight["ltgray"], linetype = 0, colour = NA), text = element_text(colour = ggthemes_data$fivethirtyeight["dkgray"]), axis.text = element_text(size = 11, colour = ggthemes_data$fivethirtyeight["dkgray"], face = "bold"), axis.ticks = element_blank(), axis.line = element_blank(), axis.title = element_text(size = 11, colour = ggthemes_data$fivethirtyeight["dkgray"], face = "bold", vjust = 1.5), legend.title = element_blank(), legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), legend.position = "bottom", legend.direction = "horizontal", legend.box = "vertical", panel.grid = element_line(colour = NULL), panel.grid.major = element_line(colour = ggthemes_data$fivethirtyeight["medgray"]), panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0.05, size = rel(1.5), face = "bold"), plot.margin = unit(c(1, 1, 1, 1), "lines"), panel.background = element_rect(fill = "#F0F0F0"), plot.background = element_rect(fill = "#F0F0F0"), panel.border = element_rect(colour = "#F0F0F0"), strip.background = element_rect()))}Thanks for all the help!EDIT:@MLavoie commented a link to a question that gave me a basic idea of how to shade under the geom_smooth lines by using a predict(loess(AGE ~ SECONDS)). predict() works like geom_smooth, and loess is the method used when n < 1000.This enabled me to shade under the male and female lines, but did not allow me to find the area under both curves. The dark-grey shaded area is the area under the geom_smooth for the entire dataset.I suspect that to find the area under the male and female curves I would first need to capture the data from the geom_smooths (male and female). I would then create a data.frame with the x-values as rows and a column for each set of y-values. I would find the minimum y-value for each x-value and I would shade the dark-grey underneath that curve.Interestingly, the shaded areas are outlined in a light blue, like the points, and the legend shows red or blue outlined boxes filled with a dark-grey color. I added this to the code instead of the original geom_ribbon:geom_ribbon(data = df[df$GENDER == 'F',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), alpha = 1, fill = "red") +geom_ribbon(data = df[df$GENDER == 'M',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), alpha = 1, fill = "blue") +geom_ribbon(aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), alpha = 1)That was the only new code involved in creating this graph:In essence, I want to remove the blue outlines of the filled areas and I want to remove the dark-grey fill from the boxes in the legend, and if someone can figure out how I would love to shade the area underneath both lines. Thanks again! 解决方案 Switch off the legend either for the colours or for the fill to get what you want.Switching off colours legend:p3 <- ggplot(df, aes(x = SECONDS, y = AGE, color = GENDER)) + geom_point() + theme_fivethirtyeight_mod() + ggtitle('Seconds vs. Age') + geom_hline(yintercept = 0, size = 1.2, colour = "#535353") + geom_vline(xintercept = 0, size = 1.2, colour = "#535353") + geom_smooth(se = F) + geom_ribbon(data = df[df$GENDER == 'F',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS)), fill = "Female"),colour = F) + geom_ribbon(data = df[df$GENDER == 'M',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS)), fill = "Male"),colour = F) + geom_ribbon(aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), colour = F) + scale_fill_manual(values = c('Female' = 'red','Male' = 'blue')) + guides(colour = F)Switching off fill legend:p4 <- ggplot(df, aes(x = SECONDS, y = AGE, color = GENDER)) + geom_point() + theme_fivethirtyeight_mod() + ggtitle('Seconds vs. Age') + geom_hline(yintercept = 0, size = 1.2, colour = "#535353") + geom_vline(xintercept = 0, size = 1.2, colour = "#535353") + geom_smooth(se = F) + geom_ribbon(data = df[df$GENDER == 'F',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), fill = 'red',colour = F) + geom_ribbon(data = df[df$GENDER == 'M',], aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), fill = 'blue',colour = F) + geom_ribbon(aes(ymin = 0, ymax = predict(loess(AGE ~ SECONDS))), colour = F) + guides(fill = F)Few points to note:I'm not sure why you're using a third geom_ribbon. If you want to shade the intersection of the areas under the other two ribbons, shading the area under the loess for the full data does not give you the intersection - you can observe that by making the graphs less opaque (by specifying alpha < 1)alpha=1 by default, so you don't need to specify it explicitly. 这篇关于R中的ggplot2:在两个不同的`geom_smooth`行下使用`geom_ribbon`进行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:19