r - R Plotly Map,从 2 个数据帧中读取的悬停文本
问题描述
我目前有一张地图,它从 2 个不同的数据框中绘制了站点。我有一组红点和另一组蓝点。我可以让悬停文本从一个数据框中读取,但是当悬停在另一个颜色站点上时,我如何让它从另一个数据框中读取?
到目前为止,这是我的代码
....获取世界多边形并提取英国
library(maps)
UK <- map_data("world") %>% filter(region=="UK")
png("JCMap.png")
JCMap <- ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", color = "dark grey",alpha=0.3) +
geom_point( data=sitesgeo, aes(x=long, y=lat), colour = 'blue', alpha = 0.5)+
geom_point( data=SCBenchmarks, aes(x=long, y=lat), size = 2, colour = 'red') +
theme_void() + ylim(50,59) + coord_map()+
theme(legend.position="none")+
ggtitle("Sites")+
theme(
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 16, hjust=0.1, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
)
print(JCMap)
JCMap
.....让它互动!
library(plotly)
p=SCBenchmarks %>%
mutate( mytext=paste("Site: ", site_name, "\n", "Customers: ", claimant_key, sep="")) %>%
ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=sitesgeo,aes(x=long, y=lat), colour = 'blue', alpha = 0.5) +
geom_point(aes(x=long, y=lat, text=mytext), colour = 'red', alpha = 1) +
scale_size_continuous(range=c(1,15)) +
scale_color_viridis(option="inferno", trans="log" ) +
scale_alpha_continuous(trans="log") +
theme_void() +
ylim(50,59) +
coord_map() +
theme(legend.position = "none")
p=ggplotly(p, tooltip="text")
p
任何帮助将非常感激
干杯
解决方案
我在朋友的帮助下弄清楚了这一点,可能不是最方便的方法,但它有效....见下面的代码
######## plot
SCBenchmarks <- SCBenchmarks %>%
mutate( mytext=paste(site_name, "\n", "Customers: ", customer_key, "\n", "Customers per Person: ", Cust_Per_Person, "\n", sep=""))
Final <- Final %>%
mutate( mytext=paste(site_name, "\n", district_name,"\n", "Customers: ", Customer_Count, "\n", sep=""))
## Make the static plot call this text:
p <- ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_jitter(data=Final,aes(x=long, y=lat, text=mytext), colour = 'blue', alpha = 0.5) +
geom_jitter(data=SCBenchmarks,aes(x=long, y=lat, text=mytext), colour = 'red', alpha = 1) +
scale_size_continuous(range=c(1,15)) +
scale_color_viridis(option="inferno", trans="log" ) +
scale_alpha_continuous(trans="log") +
theme_void() +
ylim(50,59) +
coord_map() +
theme(legend.position = "none")
p=ggplotly(p, tooltip="text")
p
推荐阅读
- sqlclient - 在 .Net Core 2.0 中使用 System.Data.SqlClient.dll 时出现版本冲突错误
- c - 如何在 C 语言中获取 asm 变量值?
- android - 如何在 Firebase 的分析中将事件发送到操作 - Android
- mysql - 使用外键不仅仅是为了约束
- perl - 从子例程返回 2 个或更多值时,仅返回定义的最后一个变量
- c++ - 将矩形添加到梯形 openCV C++
- mysql - 地理数据到数据库以进行报告
- python - 使用 Python 运行 powershell 脚本并在 Python 中捕获输出,同时仍将其显示在 powershell 控制台上
- linux - 如何列出所有子目录的目录大小?
- c - strcat 函数中的“\n”和“\n”有什么区别?