如何在逻辑图上制作标签(使用 ggplot/ggplotly 制作)显示 % 与小数
Posted
技术标签:
【中文标题】如何在逻辑图上制作标签(使用 ggplot/ggplotly 制作)显示 % 与小数【英文标题】:How to make labels on a logistic plot (made with ggplot/ggplotly) show % vs decimals 【发布时间】:2021-12-23 02:56:19 【问题描述】:我不确定这是否是显示此图表的最佳方式 - 但我正在尝试为我的同事提供一种简单的方式来解释此图表。我想让他们尽可能容易地看到被添加到列表的概率是基于分数的。
我有这个代码。
data <- data %>%
mutate(Add = case_when(
Added == "Yes" ~ 1,
Added == "No" ~ 0,
))
ggplotly(
ggplot(data, aes(Score, Add)) +
geom_point(alpha = .25, size = 1 , color = 'grey', fill = 'grey',shape = 21) +
stat_smooth(method = "glm" , se=FALSE, method.args = list(family=binomial),size = 1.5 ,lty = 1) +
labs(x="Score",y="Probability") + theme_minimal()
)
这给了我这个输出。是否可以将分数标签四舍五入,而添加标签是百分比?因此,例如在下面的屏幕截图中,我会:
得分。 8.50 添加:72%
谢谢
以下数据
data <- structure(list(Added = c("Yes", "Yes", "Yes", "No", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "Yes", "Yes", "Yes",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",
"No", "No", "Yes", "No", "No", "No", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "Yes", "No", "No", "No", "No", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "No", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes",
"No", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes",
"Yes", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes",
"Yes", "Yes", "Yes", "No", "Yes", "No", "No", "No", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes",
"No", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "No", "No", "No", "No", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "No", "No", "Yes", "No", "Yes", "Yes", "No", "No",
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No",
"No", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes",
"No", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", "Yes",
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No",
"No", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No",
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "No", "No",
"No", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"
), Score = c(10, 13, 13, 10, 10, 11, 10, 12, 13, 12, 12, 12,
10, 10, 9, 9, 9, 8, 8, 8, 8, 8, 11, 8, 12, 5, 4, 2, 4, 4, 4,
0, 0, 3, 4, 0, 0, 6, 3, 3, 5, 4, 5, 6, 6, 4, 5, 5, 0, 0, 6, 0,
5, 0, 5, 5, 0, 3, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 3, 6, 6, 4, 5,
4, 4, 5, 5, 5, 5, 5, 4, 4, 4, 4, 3, 4, 5, 5, 3, 3, 3, 3, 3, 14,
11, 11, 0, 13, 11, 13, 12, 12, 0, 11, 10, 11, 11, 10, 12, 12,
11, 10, 8, 12, 12, 12, 12, 12, 12, 13, 11, 0, 0, 12, 0, 0, 12,
11, 11, 10, 0, 12, 11, 11, 12, 12, 9, 0, 11, 11, 10, 13, 0, 0,
11, 12, 14, 11, 11, 11, 14, 14, 10, 11, 16, 12, 11, 10, 14, 13,
14, 12, 11, 12, 12, 12, 11, 13, 10, 11, 12, 14, 12, 11, 0, 9,
13, 11, 11, 11, 11, 11, 13, 12, 15, 10, 8, 13, 11, 0, 11, 0,
12, 14, 11, 12, 11, 0, 8, 0, 10, 0, 15, 10, 13, 12, 15, 8, 8,
10, 14, 10, 10, 10, 11, 15, 15, 14, 11, 13, 15, 12, 11, 14, 11,
10, 10, 11, 14, 12, 9, 12, 0, 12, 0, 9, 10, 13, 12, 15, 11, 12,
10, 11, 11, 10, 15, 10, 11, 14, 13, 13, 13, 12, 13, 9, 0, 10,
0, 0, 13, 12, 10, 10, 11, 12, 10, 13, 11, 13, 13, 12, 13, 12,
12, 11, 12, 11, 13, 0, 10, 10, 11, 14, 12, 13, 13, 12, 10, 13,
11, 0, 0, 0, 0, 0, 11, 12, 10, 10, 12, 12, 12, 13, 13, 12, 12,
13, 13, 12, 13, 12, 13, 0, 11, 12, 0, 0, 9, 13, 11, 15, 14, 14,
13, 11, 11, 14, 10, 13, 9, 9, 12, 11, 16, 14, 14, 14, 10, 8,
14, 13, 9, 11, 12, 14, 10, 13, 12, 13, 10, 13, 15, 10, 7, 8,
12, 11, 14, 12, 10, 0, 10, 13, 11, 11, 0, 11, 9, 11, 10, 7, 9,
12, 9, 10, 9, 7, 11, 11, 11, 16, 10, 13, 13, 19, 7, 12, 6, 9,
13, 5, 10, 13, 10, 9, 14, 9, 10, 13, 14, 14, 9, 12, 12, 13, 15,
14, 13, 14, 13, 0, 14, 0, 0, 8, 10, 13, 0, 13, 5, 10, 11, 13,
13, 13, 12, 11, 9, 13, 7, 9, 10, 10, 11, 12, 15, 8, 11, 9, 9,
14, 15, 13, 9, 13, 13, 13, 12, 13, 0, 10, 10, 9, 9, 8, 10, 9,
11, 10, 10, 0, 0, 10, 9, 0, 12, 0, 10, 11, 10, 10, 10, 12, 12,
12, 13, 9, 10, 13, 8, 10, 13, 12, 11, 13, 8, 16, 11, 12, 12,
9, 13, 9, 19, 10, 9, 19, 19, 11, 0, 0, 12, 9, 10, 9, 11, 10,
8, 9, 9, 13, 13, 11, 11, 10, 11, 10, 11, 12, 8, 12, 10, 9, 13,
11, 10, 10, 15, 10, 9, 12, 12, 13, 11, 10, 15, 8, 10, 8, 10,
12, 9, 11, 10, 9, 9, 10, 10, 12, 10, 10, 11, 8, 11, 9, 11, 11,
10, 10, 8, 10, 10, 11, 0, 5, 8, 14, 10, 10, 10, 11, 12, 11, 10,
11, 9, 12, 10, 11, 10, 11, 9, 11, 9, 10, 10, 13, 12, 10, 12,
10, 0, 10, 18, 9, 10, 9, 14, 12, 12, 13, 13, 10, 12, 10, 10,
15, 11, 14, 8, 10, 16, 10, 12, 11, 7, 7, 13, 10, 13, 9, 11, 10,
10, 8, 8, 9, 10, 10, 13, 12, 12, 14, 12, 13, 9, 11, 9, 0, 12,
0, 0, 11, 11, 12, 12, 11, 11, 10, 9, 15, 21, 11, 12, 11, 9, 12,
0, 10, 14, 14, 13, 13, 7, 13, 10, 10, 10, 12, 0, 13, 12, 10,
11, 8, 12, 0, 0, 0, 0, 13, 11, 0, 13, 7, 10, 10, 11, 13, 11,
10, 9, 12, 0, 11, 0, 11, 10, 13, 11, 11, 12, 8, 12, 15, 10)), row.names = c(NA,
-707L), class = c("tbl_df", "tbl", "data.frame"))
【问题讨论】:
【参考方案1】:这是尝试的一种方法。首先,存储您的 ggplotly
对象。您可以访问 Score
和 Add
数据,四舍五入到所需的小数位数,并包含您喜欢的任何格式,并存储为字符向量。这可用于设置style
,如下所示。
这将调整stat_smooth
曲线点的标签文本。如果您想为其他灰点调整不同的标签,您可以从g$x$data[[1]]
访问数据并在设置style
时为traces
添加参数(例如style(text = custom_text, traces = 1
)。
library(plotly)
g <- ggplotly(
ggplot(data, aes(Score, Add)) +
geom_point(alpha = .25, size = 1 , color = 'grey', fill = 'grey',shape = 21) +
stat_smooth(method = "glm" , se=FALSE, method.args = list(family=binomial),size = 1.5 ,lty = 1) +
labs(x="Score",y="Probability") + theme_minimal()
)
custom_text <- sprintf("Score:%s\nAdd:%s%%",
format(round(g$x$data[[2]]$x, 2), nsmall = 2),
format(round(g$x$data[[2]]$y * 100, digits = 0)))
g |>
style(text = custom_text)
情节
【讨论】:
非常感谢,这太棒了,我不知道我可以使用情节元素进行那种级别的自定义。是否有您可以推荐的用户指南或文档? 很高兴这对您有所帮助。我不知道具体的指南,但通过关于 SO 的相关问题和检查 ggplotly 对象发现了很多有用的信息。以上是关于如何在逻辑图上制作标签(使用 ggplot/ggplotly 制作)显示 % 与小数的主要内容,如果未能解决你的问题,请参考以下文章