I have drawn the attached funnel plot in ggplot, But I have 2 questions:
# Format the data
df1 <- plyr::count(df, c('random_school_id'))
df2 <- merge(df,df1, by= c("random_school_id"))
df <- df2
M3 <- aggregate(df$score_sdq_emotional_pr[df$freq > 10], by=list(df$random_school_id[df$freq > 10]),mean,na.rm=T)
S3 <- aggregate(df$score_sdq_emotional_pr[df$freq > 10], by=list(df$random_school_id[df$freq > 10]),nona)
CG_PLOT3 <- merge(M3,S3,by="Group.1")
names(CG_PLOT3) <- c("School","Mean","Size")
LINE3 <- data.frame(M3=rep(mean(df$score_sdq_emotional_pr,na.rm=T),max(CG_PLOT 3$Size)+25),
SD3=rep(sd(df$score_sdq_emotional_pr,na.rm=T),max(CG_PLOT3$Size)+25),
N3=sqrt(1:(max(CG_PLOT3$Size)+25)))
ID <- 1063
filling3 <- rep("white",nrow(CG_PLOT3))
filling3[CG_PLOT3$School ==ID]<-"green"
# Build the graph
ggplot(data = CG_PLOT3) +
geom_line(data = LINE3, aes(x = 1:(max(CG_PLOT3$Size) + 25),
y = M3 + qnorm(0.975) * SD3 / N3), size = 1, colour = "steelblue4",
linetype = 5) +
geom_line(data = LINE3, aes(x = 1:(max(CG_PLOT3$Size) + 25),
y = M3 - qnorm(0.975) * SD3 / N3), size = 1, colour = "steelblue4",
linetype = 5) +
geom_segment(xend = max(CG_PLOT3$Size)+25,yend=mean(LINE3$M3,na.rm=T)),
aes(x = 1, y = mean(LINE3$M3,na.rm=T), size=1, colour="steelblue4") +
geom_point(data = CG_PLOT3, aes(x = Size, y = Mean), size = 2,
colour = "black", shape = 21,fill = filling3) +
ylim(0, 8)
As you didn't provide a reproducible example, I have used this question as a template for your problem:
Creating a dataset here:
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"])
To add a colour background to the funnel plot, we can use the geom_ribbon
function within ggplot to fill the area between a ymin
and ymax
. In this case, we will use the data used to construct each of the lines:
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...")
As for changing the size of one point, you can check out the answer here. I would recommend subsetting the data to extract the one point, and then add another layer for the geom_point
and then changing the size
and colour
argument of the new layer`