0

我目前有一张地图,它从 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

任何帮助将非常感激

干杯

4

1 回答 1

0

我在朋友的帮助下弄清楚了这一点,可能不是最方便的方法,但它有效....见下面的代码

######## 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
于 2018-05-04T15:41:04.927 回答