【问题标题】:Add the quantilies to each line, ggplot2将分位数添加到每一行,ggplot2
【发布时间】:2020-10-26 15:50:57
【问题描述】:

我试图在这个包含三条线的图表上对 0.025 和 0.975 分位数进行着色。我试过geom_area、geom_ribbon,但无法突出显示每一行中的每一个分位数。

请注意,在此密度图中忽略了“y”。

example <-data.frame(source=c("Leaflitter","Leaflitter","Leaflitter","Leaflitter", 
"Leaflitter","Leaflitter","Leaflitter","Leaflitter","Leaflitter","Leaflitter",
"Biofilm","Biofilm","Biofilm","Biofilm","Biofilm","Biofilm","Biofilm","Biofilm",
"Biofilm","Biofilm","Algae","Algae","Algae","Algae","Algae","Algae","Algae","Algae",
"Algae","Algae"), n=c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
density=c(0.554786934, 0.650578421, 0.039317168, 0.53537613,0.435081982,0.904056941,0.556284164,0.855319434,
0.399169622,0.570246304,0.076722032,0.257427999,0.172736928,0.447424473,0.520976948,0.011720494,0.311348655,
         0.120698996,0.016336661,0.331741377,  0.368491034,0.09199358,0.787945904,0.017199397,0.04394107,
0.084222564,0.132367181,0.023981569,0.584493716,0.098012319))

example

一个子群和分位数

L <- filter(QPA_G_Feb17, source == "Leaflitter")
L <-as.data.frame(L)

Lq025  <- quantile(L$density, .025)
Lq975  <- quantile(L$density, .975)
ggplot(QPA_G_Feb17, aes(x=density, color=source)) + 
  labs(y="Density", x="Sorce contribution") +
  geom_density(aes(linetype = source), size=1.2) +
  scale_color_manual(values=c("#31a354", "#2c7fb8", "#d95f0e")) +
  scale_linetype_manual(values = c("solid", "dotted", "longdash")) +
  theme_classic()+
  ylim(0, 5)+
  theme(axis.text.y=element_text(angle=0, size=12, vjust=0.5, color="black")) +
  theme(axis.text.x =element_text(angle=0, size=12, vjust=0.5, color="black")) + 
  theme(axis.title.x = element_text(color="black", size=14))+ 
  theme(axis.title.y = element_text(color="black", size=14))  

非常感谢您的帮助,因为我查看了其他论坛,并且只有 1 行时有信息要突出显示。

【问题讨论】:

  • 请通过复制函数dput的输出来添加您的数据框
  • 您能否分享足够的数据来制作一个最小的可重现示例? dput() 或模拟假数据的代码都是完美的,因为它可以复制/粘贴。
  • 你能更好地解释一下“遮蔽每条线的 0.025 和 0.975 分位数”是什么意思吗?

标签: r ggplot2 plot graph quantile


【解决方案1】:

我认为这个数据更能代表你图中显示的数据:

set.seed(50)

QPA_G_Feb17 <- data.frame(density = c(rgamma(400, 2, 10), 
                                      rgamma(400, 2.25, 9),
                                      rgamma(400, 5, 7)),
                          source = rep(c("Algae", "Biofilm", "Leaflitter"), 
                                       each = 400))

我发现当您尝试在ggplot 中做一些复杂或非标准的事情时,最好的办法是提前计算您希望绘制的数据。在这种情况下,我们可以计算密度曲线和累积密度,包括它们的 0.025 和 0.975 分位数,并将它们全部放在一个数据框中,如下所示:

dens <- lapply(split(QPA_G_Feb17, QPA_G_Feb17$source), 
               function(x) density(x$density, from = 0, to = 1))

df <- do.call(rbind, mapply(function(x, y) {
                             data.frame(x = x$x, y = x$y, source = y)
                            }, dens, names(dens), SIMPLIFY = FALSE))

df <- df %>% 
  group_by(source) %>%
  mutate(cdf = cumsum(y * mean(diff(x))),
         lower = cdf < 0.025,
         upper = cdf > 0.975) 

现在很容易使用geom_area进行绘图:

ggplot(df, aes(x, y, color = source)) + 
  geom_area(data = df[df$lower,], aes(fill = source), alpha = 0.5,
            position = "identity") +
  geom_area(data = df[df$upper,], aes(fill = source), alpha = 0.5,
            position = "identity") +
  labs(y = "Density", x = "Source contribution") +
  geom_line(aes(linetype = source), size = 1.2) +
  scale_fill_manual(values = c("#31a354", "#2c7fb8", "#d95f0e")) +
  scale_color_manual(values = c("#31a354", "#2c7fb8", "#d95f0e")) +
  scale_linetype_manual(values = c("solid", "dotted", "longdash")) +
  theme_classic() +
  ylim(0, 5) +
  xlim(0, 1) +
  theme(axis.text.y  = element_text(size = 12, vjust = 0.5),
        axis.text.x  = element_text(size = 12, vjust = 0.5),  
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14)) 

在此,每条密度曲线的 2.5% 和 97.5% 的极值在每条线下方用阴影表示。例外是在“Leaflitter` 行中,它明显超出了您的示例中绘制的 0-1 范围。

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2015-05-30
    • 2012-10-05
    相关资源
    最近更新 更多