【问题标题】:R Plotly Map, Hover Text reading from 2 data framesR Plotly Map,从 2 个数据帧中读取的悬停文本
【发布时间】:2018-10-06 00:54:22
【问题描述】:

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

任何帮助将不胜感激

干杯

【问题讨论】:

  • 第二个区块的第三个geom_point() 缺少数据框?
  • 这与 p=SCBenchmarks 有关,这几乎就像我需要做 2 个 p=otherdataframe 但我只是不知道如何编码。

标签: r ggplot2 plotly ggplotly


【解决方案1】:

我在朋友的帮助下想出了这个办法,可能不是最方便的方法,但它确实有效....见下面的代码

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

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2018-03-25
    • 2018-09-04
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多