can not mapping discrete and continuous value to a map in r

198 Views Asked by At

As the title says.

When I mapping a continuous value to a map it works well.

When I mapping a discrete value to a map it works also well.

But when I combine the two layers an error occur:

Error: Discrete value supplied to continuous scale

Reproducible code was here:

library(scatterpie)
library(tidyverse)
library(geosphere)

us <- map_data('state') %>% as_tibble()

n = length(unique(us$region))

# creat fake mapping data

temperature_data <- tibble(region = unique(us$region),
                           temp = rnorm(n = n))

coords <- us %>% select(long, lat, region) %>% distinct(region, .keep_all = T)
  

category_data <- tibble(region = unique(us$region),
                        cat_1 = sample(1:100, size = n),
                        cat_2 = sample(1:100, size = n),
                        cat_3 = sample(1:100, size = n)) %>% left_join(coords)
  

us <- left_join(us, temperature_data)



p <- ggplot(us, aes(long, lat)) 
# mapping temperautre value to map
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey')
# mapping pie chart to map
p + 
  geom_map(map = us, aes(map_id = region), color = 'grey') +
  geom_scatterpie(data = category_data,
                    aes(long, lat),
                    cols = c("cat_1", "cat_2", "cat_3"), 
                    alpha = 0.5)
# mapping temperautre and pie chart simultaneously
# ERROR OCCUR
# Error: Discrete value supplied to continuous scale
p + geom_map(map = us, aes(map_id = region, fill = temp), color = 'grey') +
    geom_scatterpie(data = category_data,
                  aes(long, lat),
                  cols = c("cat_1", "cat_2", "cat_3"), 
                  alpha = 0.5)


0

There are 0 best solutions below