使用this dataset,我创建了这张图:

r - R : use  `geom_ribbon`  for shading under two different  `geom_smooth`  lines中的ggplot2-LMLPHP

我希望在geom_smooth行下添加阴影,如下所示:

r - R : use  `geom_ribbon`  for shading under two different  `geom_smooth`  lines中的ggplot2-LMLPHP

我希望仅在蓝线下方或在粉红色线下方的点具有这些颜色,而在两线下的所有点都应为深灰色。

我使用以下代码创建了图形:

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)
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) +
 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()))
}

感谢您提供的所有帮助!

编辑:

@MLavoie评论了一个问题的链接,该链接给了我一个基本的思想,该思想是如何使用geom_smoothpredict(loess(AGE ~ SECONDS))行下进行着色。 predict()的工作方式类似于geom_smooth,而loessn < 1000时使用的方法,这使我可以在公线和母线下着色,但无法在两条曲线下找到面积。深灰色阴影区域是整个数据集geom_smooth下的区域。

我怀疑要找到男性和女性曲线下的区域,我首先需要从geom_smooth(男性和女性)中捕获数据。然后,我将创建一个data.frame,将x值作为行,并为每组y值创建一列。我将为每个x值找到最小的y值,并在该曲线下方遮住深灰色。

有趣的是,阴影区域以浅蓝色勾勒出轮廓(与点一样),图例显示的红色或蓝色轮廓框充满了深灰色。我将其添加到代码中,而不是原始的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)

那是创建此图所涉及的唯一新代码:

r - R : use  `geom_ribbon`  for shading under two different  `geom_smooth`  lines中的ggplot2-LMLPHP

本质上,我想删除填充区域的蓝色轮廓,并且要从图例中的框中删除深灰色填充,并且如果有人可以弄清楚我希望如何在两条线下面着色该区域。再次感谢!

最佳答案

关闭图例以获取颜色或填充以获取所需的颜色。

关闭颜色图例:

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)

r - R : use  `geom_ribbon`  for shading under two different  `geom_smooth`  lines中的ggplot2-LMLPHP

关闭填充图例:
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)

r - R : use  `geom_ribbon`  for shading under two different  `geom_smooth`  lines中的ggplot2-LMLPHP

需要注意的几点:
  • 我不确定您为什么要使用第三个geom_ribbon。如果要对其他两个功能区下的区域的交点进行阴影处理,则对黄土下的完整数据进行阴影处理不会给您相交-您可以通过使图形变得更加不透明来观察到这一点(通过指定alpha
  • alpha = 1默认情况下,因此您无需显式指定它。
  • 关于r - R : use `geom_ribbon` for shading under two different `geom_smooth` lines中的ggplot2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37368778/

    10-12 17:59