Finn Tobias
Finn Tobias

Reputation: 33

Display all attributes of a point using tmap function

enter image description hereI am trying to make a shiny application that shows the location of public fruit trees around Bellingham, WA. When a user clicks on a tree location I want them to be able to see all attributes of a given tree such as status (confirmed/unconfirmed) and lat/long coordinates. When I create an interactive map, however, I can only see the attribute I used to color code the points by, in this case species. Species is made obvious by the tmap legend and I would like to be able to see status (confirmed/unconfirmed) and lat/long coordinates when I click on a point.

This is the code I used for creating a simple interactive map application using r shiny. I loaded the necessary packages.

library(dplyr)
library(shiny)
library(leaflet)
library(tmap)
library(sf)

I created the data frame.

dat <- data.frame(lat = c(48.7323,48.7308,
                          48.7301,48.7276,
                          48.7246,48.7323,
                          48.7211),
long = c(-122.4928,-122.4940,
         -122.4942,-122.4939,
         -122.4958,-122.4975,
         -122.4946),
 species = c("Apple", "Apple",
             "Pear", "Plum",
             "Fig", "Plum",
             "Pear"),
 status = c("Confirmed", "Unconfirmed", "Confirmed", "Confirmed", "Confimed", "Unconfirmed", "Confirmed"))
# created ui and server components 

ui <- fluidPage( mainPanel(
  tmapOutput(outputId = "tmapMap"),
)
)


server <- function(input, output, session) {
  output$tmapMap <- renderTmap({
 
# converted to spatial points df

dat2plot <- st_as_sf(dat2plot, coords = c("long","lat"), crs=st_crs("EPSG:4326"))
 
   # and plotted map 
    
map1 <- tm_shape(dat2plot) +
 tm_dots(col= "species", alpha=0.8,size = 0.1) +
 
  
tm_legend(show = TRUE) +
 tm_view(set.zoom.limits = c(14,16))
    map1

})
 

}

shinyApp(ui = ui, server = server)

The image attached is what is returned.

Upvotes: 1

Views: 145

Answers (2)

Simeon
Simeon

Reputation: 88

I found an answer to a similar question here.

You can control the fields that display using the popup.vars argument of tm_dots, including when you're using colour to symbolise a variable. This takes TRUE/FALSE to plot all/none of the variables, or a character vector of the variables you want to show.

#data
dat2plot <- st_as_sf(dat, coords = c("long","lat"), crs=st_crs("EPSG:4326"), remove = FALSE) 

#simple plot
tm_shape(dat2plot) +
  tm_dots(col= "species", alpha=0.8,size = 0.1,
          id = "species", #show on hover
          popup.vars = c("status", "lat", "long")) #character vector of desired variables

The id argument sets what variable shows when hovering over a point.

This method also allows for renaming the variables in the popup:

tm_shape(dat2plot) +
  tm_dots(col= "species", alpha=0.8,size = 0.1,
          id = "species", #show on hover
          popup.vars = c("Status" = "status",
                         "lat",  "long"))

The second code snippet returns this map, showing the named variables.

Upvotes: 0

mgrund
mgrund

Reputation: 1625

This seems to be a bug as it only works if you define a single color via col. If you adjust the corresponding lines as follows you will see at least all attributes, however the color coding does not appear:

# converted to spatial points df
dat2plot <- st_as_sf(dat, coords = c("long","lat"), crs = st_crs("EPSG:4326"), remove = FALSE) %>% 
   relocate(species, .before = lat)

# and plotted map 
map1 <- tm_shape(dat2plot) +
 tm_dots(col= "blue", alpha = 0.8, size = 0.1) +
 tm_legend(show = TRUE) +
 tm_view(set.zoom.limits = c(14,16))

map1

enter image description here

Upvotes: 0

Related Questions