R绘制地图,悬停从2个数据帧中读取文本 [英] R Plotly Map, Hover Text reading from 2 data frames

查看:49
本文介绍了R绘制地图,悬停从2个数据帧中读取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一张地图,该地图绘制了来自2个不同数据框的站点.我的一组有红点,另一组有蓝点.我可以将悬停文本从一个数据帧中读取,但是当将鼠标悬停在另一个颜色站点上时,如何从另一个数据帧中读取它?

到目前为止,这是我的代码

....获取世界多边形并提取英国

 库(地图)UK<-map_data("world")%>%filter(region =="UK")png("JCMap.png")JCMap<-ggplot()+geom_polygon(数据=英国,aes(x =长,y =纬度,组=组),fill =灰色",颜色=深灰色",alpha = 0.3)+geom_point(data = sitesgeo,aes(x = long,y = lat),color ='blue',alpha = 0.5)+geom_point(data = SCBenchmarks,aes(x = long,y = lat),size = 2,color ='red')+theme_void()+ ylim(50,59)+ coord_map()+主题(legend.position ="none")+ggtitle(网站")+主题(plot.background = element_rect(fill =#f5f5f2",颜色= NA),panel.background = element_rect(fill =#f5f5f2",颜色= NA),plot.title = element_text(大小= 16,高度= 0.1,颜色=#4e4d47",边距=边距(b = -0.1,t = 0.4,l = 2,单位="cm")),)打印(JCMap)JCMap 

...使其具有交互性!

 库(可打印)p = SCBenchmarks%&%;%mutate(mytext = paste(站点:",site_name,"\ n",客户:",claimant_key,sep ="))%>%ggplot()+geom_polygon(数据=英国,aes(x =长,y =纬度,组=组),fill =灰色",alpha = 0.3)+geom_point(data = sitesgeo,aes(x = long,y = lat),color ='blue',alpha = 0.5)+geom_point(aes(x = long,y = lat,text = mytext),color ='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()+主题(legend.position =无")p = ggplotly(p,tooltip ="text")p 

任何帮助将不胜感激

欢呼

解决方案

我在一个朋友的帮助下解决了这个问题,这可能不是最方便的方法,但是它可以工作....请参见下面的代码## 阴谋

SCBenchmarks<-SCBenchmarks%>%

mutate(mytext = paste(site_name,"\ n",客户:",customer_key,"\ n",每人客户:",Cust_Per_Person,"\ n",sep ="))

最终<-最终%>%

mutate(mytext = paste(site_name,"\ n",district_name,"\ n",客户:",Customer_Count,"\ n",sep ="))

##使静态绘图调用此文本:

p<-ggplot()+geom_polygon(数据=英国,aes(x =长,y =纬度,组=组),fill =灰色",alpha = 0.3)+

geom_jitter(data = Final,aes(x = long,y = lat,text = mytext),color ='blue',alpha = 0.5)+

geom_jitter(data = SCBenchmarks,aes(x = long,y = lat,text = mytext),color ='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()+

主题(legend.position =无")

p = ggplotly(p,tooltip ="text")

p

I currently have a map that has plotted sites from 2 different data frames. I have red dots for one set and blue dots for another set. I can get the hover text to read from one of the data frames but how do i get it to read from another when hovering over the other color site?

here is my code so far

....Get the world polygon and extract UK

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

.....make it interactive!

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

Any help would be much appreciated

Cheers

解决方案

I figured this out with a little help from a friend, probably not the most convenient way to do it but it works....see code below

## 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

这篇关于R绘制地图,悬停从2个数据帧中读取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆