2

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)


zhiwei li
  • 1,635
  • 8
  • 26
  • 1
    Try using the `ggnewscale` package, add `library(ggnewscale)` and then add `new_scale("fill") +` between `geom_map` and `geom_scatterpie` – Ben Oct 09 '20 at 14:58
  • there is a similar question here: https://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot The plot looks similar to what you are trying to achieve, right? – sequoia Oct 09 '20 at 15:14

0 Answers0