GGPLOT中点的选择性标记

发布于 2025-02-08 03:38:01 字数 1739 浏览 0 评论 0原文

我目前正在努力在R中创建一个漏斗地块,以达到一系列死亡率。我已经使用以下代码来创建我的漏斗图,并获得了以下图:

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

https://i.sstatic.net/71jrr.png“ alt =”在此处输入图像描述“>

我希望显示所有高于蓝色(99.8%)线上或下方所有点的标签。我尝试了其他线程建议的子集解决方案,但无法使它们起作用。有人对我如何实现这一目标有任何建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-02-15 03:38:01

这是我在评论中的建议:

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)

”在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文