i当前有一个映射,该地图绘制了来自两个不同数据帧的站点。我有一组红色的点,另一组蓝色的点。我可以从一个数据帧中获取悬停文本来阅读,但是在悬停在另一个颜色站点上时,我该如何从另一个数据框架中阅读?
这是我到目前为止的代码
....获取世界多边形并提取英国
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