1

I am working with the R Programming language.

Using the following link as a tutorial (https://plotly.com/r/lines-on-maps/), I was able to make an interactive plot:

#load libraries
library(dplyr)
library(leaflet)
library(plotly)
library(data.table)

#generate data for example (longitude and latitude of cities)
lat = rnorm(100, 43, 3)
long = rnorm(100, -79, 3)

map_data = data.frame(lat, long)
map_data$type = as.factor(1:100)

#change format of the data so that it is compatible for this example 

result = rbind(
  cbind(map_data[1:nrow(map_data)-1,c(1,2)], map_data[-1,c(1,2)]),
  cbind(map_data[nrow(map_data), c(1,2)], map_data[1,c(1,2)])
)
colnames(result) <- c("start_lat", "start_long", "end_lat", "end_long")

my_data = result

my_data$type = as.factor(1:nrow(my_data))
my_data$type1 = as.character(1:100)
my_data$count = as.integer(1)
my_data$id = 1:100

#### begin visualization

# map projection
geo <- list(
    scope = 'north america',
    projection = list(type = 'azimuthal equal area'),
    showland = TRUE,
    landcolor = toRGB("gray95"),
    countrycolor = toRGB("gray80")
)

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, alpha = 0.5
)


fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, hoverinfo = "text", alpha = 0.5
)


fig <- fig %>% add_segments(
    data = group_by(my_data, type),
    x = ~start_long, xend = ~end_long,
    y = ~start_lat, yend = ~end_lat,
    alpha = 0.3, size = I(1), hoverinfo = "none"
)

fig <- fig %>% layout(
    title = 'Plot 1',
    geo = geo, showlegend = FALSE, height=800
)

#final result
fig

This produces the following result:

enter image description here

Now, I am trying to get the "interactive text" to work:

# map projection
geo <- list(
    scope = 'north america',
    projection = list(type = 'azimuthal equal area'),
    showland = TRUE,
    landcolor = toRGB("gray95"),
    countrycolor = toRGB("gray80")
)

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, alpha = 0.5
)


fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, text = ~type1, size = ~count, hoverinfo = "text", alpha = 0.5
)


fig <- fig %>% add_segments(
    data = group_by(my_data, type),
    x = ~start_long, xend = ~end_long,
    y = ~start_lat, yend = ~end_lat,
    alpha = 0.3, size = I(1), hoverinfo = "none"
)

fig <- fig %>% layout(
    title = 'Plot 1',
    geo = geo, showlegend = FALSE, height=800
)

fig

enter image description here

The interactive text is now working, but the data points are appearing "much bulkier".

My Question: Is it possible to make the interactive text work, but have the data points appear the same way they do in the first picture?

I originally tried to do this without a "count" variable:

fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat, text = ~type1, hoverinfo = "text", alpha = 0.5
)

But when I do this, the interactive text isn't working - the interactive text only works when a "count" variable is added.

Is this "count" variable necessary? Can someone please show me how to fix this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

1

You don't need to use count. However, there is something odd here with the segments. Either way, this achieves what I think you're looking for.

I have provided two examples because you didn't say what you wanted to have in the hover text. In the first example, I just use the x and y (lat and long). In the second, I used custom hover content.

Everything that precedes the creation of fig was left unchanged.

Notable changes:

  • the order the fig elements are assembled; segments seems to only work if it is before the markers
  • hoverinfo for the segments add is now set to text--this didn't add hover content, but for some reason none here was a problem...odd
  • I dropped a call to fig or two, that seemed to be doing nothing...
  • in add_markers, this changed differently in the two options
    • in one, hovertext = "text" was changed to hovertext = "lat+lon"
    • in the other, there were multiple changes--you'll have to look at the code for this one
  • in layout, I deleted the height argument; it's ignored
fig <- plot_geo(locationmode = 'USA-states', color = I("red"))
fig <- fig %>% add_segments( # add segments
  data = group_by(my_data, type),
  x = ~start_long, xend = ~end_long,
  y = ~start_lat, yend = ~end_lat,
  alpha = 0.3, size = I(1), hoverinfo = "text" # changed hoverinfo
)
fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat,
  alpha = 0.5, hoverinfo = "lat+lon"           # changed hoverinfo
)
fig <- fig %>% layout(
  title = 'Plot 1',
  geo = geo, showlegend = FALSE  # removed height argument
  )

#final result
fig

enter image description here

Here's the custom text version

fig <- plot_geo(locationmode = 'USA-states', color = I("red"))
fig <- fig %>% add_segments( # add segments
  data = group_by(my_data, type),
  x = ~start_long, xend = ~end_long,
  y = ~start_lat, yend = ~end_lat,
  alpha = 0.3, size = I(1), hoverinfo = "text" # changed hoverinfo
)
fig <- fig %>% add_markers(
  data = my_data, x = ~start_long, y = ~start_lat,
  alpha = 0.5, hoverinfo = "text",          # hoverinfo unchanged
  text = ~paste0("Longitude: ",             # text changed here**
                 round(my_data$start_long, 2),
                 "<br>Latitude: ", 
                 round(my_data$start_lat, 2))
  )
fig <- fig %>% layout(
  title = 'Plot 1',
  geo = geo, showlegend = FALSE  # removed height argument
)

#final result
fig

enter image description here

Let me know if you have any questions!

Kat
  • 15,669
  • 3
  • 18
  • 51
  • @ Kat: wow, wow, wow! this is great! thank you so much for your help! – stats_noob Feb 23 '22 at 02:56
  • 1) I was just curious to put some arbitrary text at each point (e.g. suppose there was a column for each point that said "abc", "def", "ghi", etc). I will have to study your answer closely and see what I was missing – stats_noob Feb 23 '22 at 02:57
  • 2) Do you know if its possible to replace each "red point" with a "number"? For instance, I added an "id" column to "my_data" that represents the row number. Do you think it might be possible to make it so that each red point is a number, e.g. "1", "2", "3", ... "99", "100"? – stats_noob Feb 23 '22 at 03:00
  • 3) I wonder if it is possible to get more "detail" on the map? Do you think you get state boundaries or even street names as you zoom in? – stats_noob Feb 23 '22 at 03:01
  • 4) I just wanted to show you the end goal - in the end, I am working on something like this: https://stackoverflow.com/questions/71197813/r-connecting-dots-on-a-map . Check it out! – stats_noob Feb 23 '22 at 03:02
  • Thank you so much for all your help! I really appreciate it! :) – stats_noob Feb 23 '22 at 03:03
  • You can put whatever you want into the tooltips. The primary constraint is that you need labels for each point (exact count kind of thing). Adding a field to `my_data` is a great way to add the content you want to see. Your end goal is leaflet or leaflet-like? You can add many details to this map or add these points to a leaflet map. – Kat Feb 23 '22 at 13:45