看着
this answer
引用R-Cookbook:
如果你同时使用颜色和形状,它们都需要给出比例规格。否则将有两个独立的图例。
因此我们可以推断它与
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()
生产: