I'm trying to create a map with two variables where one is represented by polygon colors (choropleth fill) and the other is represented by bubble size at the centroid of each polygon. To make it concrete, let's consider nc
data's two variables AREA
and BIR74
where AREA
fills the polygon and BIR74
is represented by size. To make it as similar as my actual case, I created a few NA values for BIR74
variable.
library(tidyverse)
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
nc_centroids <- data.frame(nc %>% st_centroid() %>% st_coordinates())
nc <- nc %>% bind_cols(nc, nc_centroids) %>% mutate(BIR74 = replace(BIR74, AREA < 0.08, NA))
Now, I hope to create a custom break for BIR74
so that the bubble size is either "big" or "small", but even after supplying scale_size_continuous(breaks = c(1000, 20000))
, the bubble size on the map seems to be continuous. For instance, see the red circle area in the map, which show at least three different circle sizes.
Here's what I've tried.
ggplot(nc) + geom_sf(aes(fill = AREA), colour = "white") +
geom_point(aes( x = X, y = Y, size = BIR74)) +
scale_size_continuous(breaks = c(1000, 20000))
I could potentially translate the BIR74
variable to character/factor but in this case I couldn't prevent the NA values show up on the map, which I don't want. Any comments would be appreciated!
Scale breaks do not change how your data is displayed. Also setting the limits has not resulted in a change.
Therefore, transform your data first, and then plot the different sizes.
library(tidyverse)
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
nc_centroids <- data.frame(nc %>% st_centroid() %>% st_coordinates())
nc2 <-
nc %>%
bind_cols(nc, nc_centroids) %>%
mutate(BIR74 = replace(BIR74, AREA < 0.08, NA),
BIR74 = if_else(BIR74 < 10000, 1, 2)) %>%
drop_na("BIR74") # this is not necessary, but it makes it visually clearer where you have missing values
ggplot(nc2) +
geom_sf(aes(fill = AREA), colour = "white") +
geom_point(aes( x = X, y = Y, size = BIR74)) +
scale_size_continuous(breaks = 1:2)
Created on 2020-04-26 by the reprex package (v0.3.0)
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.