如何格式化 R plotly 工具提示中的变量名称?
How do I format the names of the variables in the R plotly tooltip?
我一直在尝试制作 U.S 的地图。带有叠加的枪击散点图,每个点的 hoverinfo 标签中都有死亡和受伤的人数。但是,标签弹出为 "num_killed",我想将其格式化为 "Number Killed: "。到目前为止,我已经尝试在 ggplot 部分标记变量,但无济于事。有什么办法可以改变变量名在工具提示中的打印方式吗?
这是我的代码:
library(plotly)
library(dplyr)
library(ggplot2)
us <- map_data("state")
library(maps)
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured)) +
labs(color = "Number Injured", size = "Number Killed", num_killed = "Number Killed", num_injured = "Number Injured")
plot <- ggplotly(g)
结果如下:
附加的数据集是这个数据库处理后的csv:http://www.gunviolencearchive.org/reports/mass-shooting?page=1
(作为数据框处理)。
这应该能满足您的需求:
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured,
text = paste('lng: ', lng,
'<br>lat:', lat,
'<br>Number Killed:', num_killed,
'<br>num_injured:', num_injured)))
ggplotly(g, tooltip = "text")
我一直在尝试制作 U.S 的地图。带有叠加的枪击散点图,每个点的 hoverinfo 标签中都有死亡和受伤的人数。但是,标签弹出为 "num_killed",我想将其格式化为 "Number Killed: "。到目前为止,我已经尝试在 ggplot 部分标记变量,但无济于事。有什么办法可以改变变量名在工具提示中的打印方式吗?
这是我的代码:
library(plotly)
library(dplyr)
library(ggplot2)
us <- map_data("state")
library(maps)
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured)) +
labs(color = "Number Injured", size = "Number Killed", num_killed = "Number Killed", num_injured = "Number Injured")
plot <- ggplotly(g)
结果如下:
附加的数据集是这个数据库处理后的csv:http://www.gunviolencearchive.org/reports/mass-shooting?page=1 (作为数据框处理)。
这应该能满足您的需求:
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured,
text = paste('lng: ', lng,
'<br>lat:', lat,
'<br>Number Killed:', num_killed,
'<br>num_injured:', num_injured)))
ggplotly(g, tooltip = "text")