我一直在试图制作一张美国地图,其中有一个叠加的枪击散点图,其中每个点的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 (作为数据框处理)。
答案 0 :(得分: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")