简体   繁体   中英

Using leaflet to produce plot with total count

I am trying to use the leaflet package in R to produce an interactive visual which helps me visualize the following data:

      Provice      Lat     Long     Date Confirmed Recovered Deaths
1       Anhui 31.82570 117.2264 20-01-22         1         0      0
2       Anhui 31.82570 117.2264 20-01-23         9         0      0
3       Anhui 31.82570 117.2264 20-01-24        15         0      0
4       Anhui 31.82570 117.2264 20-01-25        39         0      0
5       Anhui 31.82570 117.2264 20-01-26        60         0      0
6       Anhui 31.82570 117.2264 20-01-27        70         0      0
7       Anhui 31.82570 117.2264 20-01-28       106         0      0
8       Anhui 31.82570 117.2264 20-01-29       152         2      0
9       Anhui 31.82570 117.2264 20-01-30       200         2      0
10      Anhui 31.82570 117.2264 20-01-31       237         3      0

Note: There are 10 Provices in total each with their respective coordinates.

So far I have been able to create the following image: 在此处输入图片说明

using the following code:


dta2 %>% 
  leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  addMarkers(label = dta2$Confirmed  , lng = dta2$Long, lat = dta2$Lat ,clusterOptions = markerClusterOptions(), popup = ~paste("Provice:", dta2$Provice))

However, my goal is to display the total number of confirmed cases for this virus at each cluster as opposed to the number of observations for each cluster (there are 22 obs for each province). In other words, I would like to display the sum of the confirmed column for each of the 10 provinces in their individual clusters as opposed to a frequency. Can anybody please shed some light on this for me?

It's difficult to be sure my proposition works fine since I don't have access to your data. But I think the following might work. Please note that I'm using the data.table package to summarize the data, you will need to install it if you don't already have it.

library(data.table)

# Summarize the data
setDT(dta2)
dta2 <- dta2[, .(
  Confirmed = sum(Confirmed),
  Recovered = sum(Recovered),
  Deaths = sum(Deaths)
), .(Provice, Lat, Long)]

# Create the leaflet map
leaflet(dta2) %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  addLabelOnlyMarkers(
    lng = ~Long,
    lat = ~Lat,
    label = ~Confirmed,
    labelOptions = labelOptions(noHide = TRUE)
  )

You can customize the display with the argument labelOptions of the addLabelOnlyMarkers() function if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM