M.T*_*ich 3 gis r ggplot2 ggmap
我正在地图上绘制点数据,并想缩放点大小并填充到另一列。但是,ggplot为大小和填充生成两个单独的图例,我只想要一个。我已经查看了针对同一问题的几种答案,例如,这个答案,但是无法理解我在做什么错。我的理解是,如果两种美学都映射到同一数据,那么应该只有一个图例,对吗?
这是一些代码来说明问题。任何帮助深表感谢!
lat <- rnorm(10,54,12)
long <- rnorm(10,44,12)
val <- rnorm(10,10,3)
df <- as.data.frame(cbind(long,lat,val))
library(ggplot2)
library(scales)
ggplot() +
geom_point(data=df,
aes(x=lat,y=long,size=val,fill=val),
shape=21, alpha=0.6) +
scale_size_continuous(range = c(2, 12), breaks=pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette="RdYlBu") +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)
看着这个答案援引R-食谱:
如果同时使用颜色和形状,则都需要为其指定比例尺规格。否则,将有两个两个单独的图例。
因此,我们可以推断出size
和和fill
参数相同。我们需要两个刻度都适合。为此,我们可以breaks=pretty_breaks(4)
在scale_fill_distiller()
零件中再次添加。然后通过使用guides()
我们可以实现我们想要的。
set.seed(42) # for sake of reproducibility
lat <- rnorm(10, 54, 12)
long <- rnorm(10, 44, 12)
val <- rnorm(10, 10, 3)
df <- as.data.frame(cbind(long, lat, val))
library(ggplot2)
library(scales)
ggplot() +
geom_point(data=df,
aes(x=lat, y=long, size=val, fill=val),
shape=21, alpha=0.6) +
scale_size_continuous(range = c(2, 12), breaks=pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette="RdYlBu", breaks=pretty_breaks(4)) +
guides(fill = guide_legend(), size = guide_legend()) +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
546 次 |
最近记录: |