如何在ggplot R中为漏斗图着色

时间:2017-11-24 11:27:35

标签: r ggplot2

我在ggplot中绘制了附加的漏斗图,但我有两个问题:

  1. 有没有办法让彩色的绿点更大(只有那一个);
  2. 有没有办法为置信区间的上部和下部着色?
  3. this is what I want to make

    这是我迄今为止所做的: this what I can make

    谢谢!

    我正在处理的数据集:

    df <-
    read.table(text = "
    school_id year sdq_emotional
    1060 7 4
    1060 7 5
    1060 7 7
    1060 7 6
    1060 7 4
    1060 7 7
    1060 7 8
    1115 7 5
    1115 7 9
    1115 7 3
    1136 7 1
    1136 7 8
    1136 7 5
    1136 7 9
    1135 7 4
    1139 7 7
    1139 7 3
    2371 7 6
    2371 7 3
    2372 7 4
    2372 7 1
    2378 7 6
    2378 7 7
    2378 7 5", header=TRUE)
    

    我的代码如下:

    # Format the data
    df1 <- plyr::count(df, c('school_id'))
    df2 <- merge(df,df1, by= c("school_id"))
    df <- df2 
    
    M3 <- aggregate(df$sdq_emotional[df$freq > 10], by=list(df$school_id[df$freq > 10]),mean,na.rm=T) 
    S3 <- aggregate(df$sdq_emotional[df$freq > 10], by=list(df$school_id[df$freq > 10]),nona)
    
     CG_PLOT1 <- merge(M3,S3,by="Group.1")
      names(CG_PLOT1) <- c("School","Mean","Size")
    LINE3 <- data.frame(M3=rep(mean(df$sdq_emotional,na.rm=T),max(CG_PLOT1$Size)+25), 
                        SD3=rep(sd(df$sdq_emotional,na.rm=T),max(CG_PLOT1$Size)+25),
                    N3=sqrt(1:(max(CG_PLOT1$Size)+25)))
    ID <- 1060
    
    filling3 <- rep("white",nrow(CG_PLOT1))
    filling3[CG_PLOT1$School ==ID]<-"green"
    
    # Build the graph
    ggplot(data = CG_PLOT1) + 
      geom_line(data = LINE3, aes(x = 1:(max(CG_PLOT1$Size) + 25), 
            y = M3 + qnorm(0.975) * SD3 / N3), size = 1, colour = "steelblue2",
            linetype = 5) +
      geom_line(data = LINE3, aes(x = 1:(max(CG_PLOT1$Size) + 25), 
            y = M3 - qnorm(0.975) * SD3 / N3), size = 1, colour = "steelblue2",
            linetype = 5) +
      geom_segment(xend = max(CG_PLOT1$Size)+25,yend=mean(LINE3$M3,na.rm=T)),
           aes(x = 1, y = mean(LINE3$M3,na.rm=T), size=1, colour="steelblue2") +
      geom_point(data = CG_PLOT1, aes(x = Size, y = Mean), size = 2,
            colour = "black", shape = 21,fill = filling3) + 
      ylim(0, 8)
    
    非常感谢你!

1 个答案:

答案 0 :(得分:1)

由于您没有提供可重现的示例,我使用this问题作为您问题的模板:

在此处创建数据集:

library(ggplot2)
set.seed(101)
x <- runif(100, min=1, max=10)
y <- rnorm(length(x), mean=5, sd=0.1*x)
df <- data.frame(x=x*70, y=y)
m <- lm(y ~ x, data=df) 
fit95 <- predict(m, interval="conf", level=.95)
fit99 <- predict(m, interval="conf", level=.999)
df <- cbind.data.frame(df, 
                       lwr95=fit95[,"lwr"],  upr95=fit95[,"upr"],     
                       lwr99=fit99[,"lwr"],  upr99=fit99[,"upr"])

要在漏斗图中添加颜色背景,我们可以使用ggplot中的geom_ribbon函数来填充yminymax之间的区域。在这种情况下,我们将使用用于构建每一行的数据:

ggplot(df, aes(x, y)) +
  # Add background
  geom_ribbon(ymin= df$upr99, ymax = Inf, fill = "#e2a49a", alpha = 0.5) +
  geom_ribbon(ymin = df$lwr99, ymax = df$upr99, fill = "#e0ba9d", alpha = 0.5 ) +
  geom_ribbon(ymin = 0, ymax = df$lwr99, fill = "#8fd6c9", alpha = 0.5 ) +

  # Overlay points and lines
  geom_point() + 
  geom_smooth(method="lm", colour="black", lwd=1.1, se=FALSE) + 
  geom_line(aes(y = upr95), color="black", linetype=2) + 
  geom_line(aes(y = lwr95), color="black", linetype=2) +
  geom_line(aes(y = upr99), color="red", linetype=3) + 
  geom_line(aes(y = lwr99), color="red", linetype=3)
  labs(x="No. admissions...", y="Percentage of patients...")

enter image description here

至于更改一个点的大小,您可以查看答案here。我建议将数据子集化以提取一个点,然后为geom_point添加另一个层,然后更改新图层的sizecolour参数

相关问题