r - ggplot 线条和线段填充

标签 r ggplot2 fill

我想创建一个看起来类似于 this one on David Robinson's variance explained blog 的图:

http://varianceexplained.org/figs/2015-10-21-credible_intervals_baseball/jeter_plot-1.png

除了可信区间之间和后曲线下方的填充外,我认为我已经完成了。如果有人知道如何做到这一点,那么得到一些建议会很棒。

下面是一些示例代码:

library(ebbr)
library(ggplot2)
library(dplyr)

sample<- data.frame(id=factor(1:10), yes=c(20, 33, 44, 51, 50, 50, 66, 41, 91, 59), 
                total=rep(100, 10))

sample<-
  sample %>%
  mutate(rate=yes/total)

pri<-
  sample %>%
  ebb_fit_prior(yes, total)

sam.pri<- augment(pri, data=sample)

post<- function(ID){
  a<-
    sam.pri %>%
    filter(id==ID)

  ggplot(data=a, aes(x=rate))+
    stat_function(geom="line", col="black",  size=1.1, fun=function(x) 
      dbeta(x, a$.alpha1, a$.beta1))+
    stat_function(geom="line", lty=2, size=1.1,
                  fun=function(x) dbeta(x, pri$parameters$alpha,     pri$parameters$beta))+
    geom_segment(aes(x=a$.low, y=0, xend=a$.low, yend=.5), col="red", size=1.05)+
    geom_segment(aes(x = a$.high, y=0, xend=a$.high, yend=.5), col="red", size=1.05)+
    geom_segment(aes(x=a$.low, y=.25, xend=a$.high, yend=.25), col="red", size=1.05)+
xlim(0,1)
}

post("10")

最佳答案

我通常通过手动生成数据来描述曲线,为阴影区域的最小值和最大值添加零 y 值,并使用 geom_polygon() 来做到这一点。

library(ebbr)
library(ggplot2)
library(dplyr)

sample <- data.frame(id = factor(1:10), yes = c(20, 33, 44, 51, 50, 50, 66, 41, 91, 59), 
                    total = rep(100, 10)) %>%
  mutate(rate=yes/total)

pri <- sample %>%
  ebb_fit_prior(yes, total)

sam.pri <- augment(pri, data = sample)

a <- sam.pri %>%
  filter(id == 10)

# Make the x values for the shaded region
x <- seq(from = a$.low, to = a$.high, length.out = 100)

# Make the y values for the shaded region
y <- dbeta(x, a$.alpha1, a$.beta1)

# Make a data.frame for the shaded region, including zeroes
shaded <- data.frame(x = c(x, a$.high, a$.low), y = c(y, 0, 0))

ggplot(data = a, aes(x = rate)) +
  stat_function(geom = "line", col = "black", size = 1.1, 
                fun = function(x)  dbeta(x, a$.alpha1, a$.beta1)) +
  geom_polygon(data = shaded, aes(x, y),
               fill = "red", alpha = 0.1) +
  stat_function(geom = "line", lty = 2, size = 1.1,
                fun = function(x ) dbeta(x, pri$parameters$alpha,     pri$parameters$beta)) +
  geom_segment(aes(x = a$.low, y = 0, xend = a$.low, yend = 0.5), col = "red", size = 1.05) +
  geom_segment(aes(x = a$.high, y = 0, xend = a$.high, yend = .5), col = "red", size = 1.05) +
  geom_segment(aes(x = a$.low, y = .25, xend = a$.high, yend = .25), col = "red", size = 1.05) +
  xlim(0,1)

enter image description here

关于r - ggplot 线条和线段填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878566/

相关文章:

xml - 使用xpath选择某些子节点

r - 在图例中显示不同的几何图形以实现共同的美感

c++ - 如何在二维 vector 上进行内存设置

javascript - 为什么调用描边会导致该元素绘制在 HTML Canvas 上?

R&Excel : Creating Pivot Tables & Slicers

r - 连接字符串/字符向量

r - R:ggplot2,我可以设置图标题来环绕并缩小文本以适合图吗?

Android listview项目高度fill_parent不起作用

r - 如何从 str_extract 中提取精确的字符串?

r - 在 RCommander 中使用 ggplot2