0

I'm currently working on creating a funnel plot in R for a set of mortality rates. I've used the following code to create my funnel plot, and got the following plot:

fp1<-ggplot(data=agg.hd2,
            aes(x=total, group=OverallDR))
fp1<-fp1 + geom_smooth(aes(y=lcl95), 
                       se = FALSE,
                       linetype="solid",
                       color = "red", 
                       size=0.5)
fp1<-fp1 + geom_smooth(aes(y=ucl95), 
                       se = FALSE,
                       linetype="solid", 
                       color = "red", 
                       size=0.5)

fp1<-fp1 + geom_smooth(aes(y=lcl99.8), 
                       se = FALSE,
                       linetype="solid",
                       color="blue", 
                       size=0.5)
fp1<-fp1 + geom_smooth(aes(y=ucl99.8), 
                       se = FALSE,
                       linetype="solid",
                       color="blue", 
                       size=0.5)

fp1<-fp1+geom_smooth(aes(y=OverallDR), 
                     se=FALSE, 
                     color="black", 
                     size=0.5)

fp1<-fp1 + geom_point(aes(y=DRbyhosp), 
                      color ="black")

fp1<-fp1 + theme_classic()
fp1<-fp1 + scale_x_continuous(breaks=seq(0,6000, by=500))
fp1<-fp1 + scale_y_continuous(labels = scales::percent)
fp1<-fp1 + labs(title="Funnel Plot showing Death Rate for Each Hospital")
fp1<-fp1 + labs(x="Operations Performed")
fp1<-fp1 + labs(y="Death Rate")
fp1

enter image description here

I wish to display labels for all of the points which are above or below the blue (99.8%) line. I've tried the subsetting solutions suggested on other threads, but haven't been able to make them work. Does anyone have any suggestions of how I can achieve this?

Skaqqs
  • 4,010
  • 1
  • 7
  • 21
R2014
  • 1
  • 1
  • `... + geom_label(data = agg.hd2[agg.hd2$DRbyhosp > agg.hd2$ucl95,], aes(x = total, y = DRbyhosp, label = YOURLABELFIELD))` – Skaqqs Jun 15 '22 at 12:55
  • Does this answer your question? [How do I label only a subset of points in a biplot?](https://stackoverflow.com/questions/70194151/how-do-i-label-only-a-subset-of-points-in-a-biplot) – Limey Jun 15 '22 at 13:00

1 Answers1

1

Here's a demonstration of my suggestion in the comments:

data(iris)

library(ggplot2)

# Define model
mymod <- lm(Petal.Length ~ Sepal.Length, data = iris)
mydat <- cbind(iris, predict(mymod, data = iris, se = TRUE))
mydat$upr <- mydat$fit + mydat$se.fit
mydat$lwr <- mydat$fit - mydat$se.fit

# Visualize
ggplot(data = mydat) +
  geom_point(aes(x = Sepal.Length, y = Petal.Length)) +
  geom_line(aes(x = Sepal.Length, y = upr), col = "blue") +
  geom_line(aes(x = Sepal.Length, y = lwr), col = "blue") +
  geom_line(aes(x = Sepal.Length, y = fit), col = "black") +
  geom_text(data = mydat[mydat$Petal.Length > mydat$upr+1.3,], aes(x = Sepal.Length, y = Petal.Length, label = Species), nudge_y = 0.1)

enter image description here

Skaqqs
  • 4,010
  • 1
  • 7
  • 21
  • You're welcome - if my answer was sufficient, you can mark it as 'correct' with a green arrow. This makes it easier for future readers to find and effectively marks the question as closed. Thanks! – Skaqqs Jun 16 '22 at 11:50