2

正如标题所说。

当我将连续值映射到地图时,它运行良好。

当我将离散值映射到地图时,它也能正常工作。

但是当我结合这两层时会发生错误:

错误:提供给连续刻度的离散值

可重现的代码在这里:

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)


4

0 回答 0