0

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

2
  • The third geom_point() of your second block lacks a dataframe ? Commented Apr 25, 2018 at 18:06
  • That relates to the p=SCBenchmarks, it’s almost like I need to do 2 of the p=otherdataframe but I just don’t know how to code it. Commented Apr 30, 2018 at 11:51

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.