sf我正在尝试使用和geom_sffrom制作地图ggplot2,其中一组点数据使用连续比例(-1 到 1)着色,一组线数据使用离散比例(a、b、c、 d). 然而,当我在同一张地图中同时使用离散和连续美学时,我遇到了麻烦。这里发布了类似的问题,但它不涉及映射美学。
这是一个简化的示例:
#Libraries
library(sf)
library(tidyverse)
#Make point data
N <- 1000
pointdat <- data.frame(x=runif(N,-1,1),y=runif(N,-1,1),z=rnorm(N)) %>%
st_as_sf(coords=c('x','y'))
ggplot(pointdat)+geom_sf(aes(col=z)) #Plot point data - this works
Run Code Online (Sandbox Code Playgroud)
#Make line data
linedat <- rbind(c(1,1),c(1,-1),c(-1,-1),c(-1,1),c(1,1))*1.1
linedat <- lapply(1:(nrow(linedat)-1),function(x) st_linestring(linedat[c(x,x+1),]))
linedat <- st_sf(geometry=st_sfc(linedat)) %>% mutate(type=factor(letters[1:4]))
ggplot(linedat)+geom_sf(aes(col=type)) #Plot line data - this works
Run Code Online (Sandbox Code Playgroud)
#When I try to plot point and line data together, it throws an error
ggplot()+
geom_sf(data=linedat,aes(col=type))+
geom_sf(data=pointdat,aes(col=z))
#Error: Continuous value supplied to discrete scale …Run Code Online (Sandbox Code Playgroud)