4

我正在尝试为尺寸美学编辑图例并为该美学设置比例尺寸,但似乎不能同时做到这两点。

以下是步骤:

#create the map  
library(ggplot2)
world <- broders("world")

#generate a dataframe of geolocated data with attributes
points <- data.frame(lat = seq(0, 50, 10), 
                         lon = seq(0, 50, 10),
                         type = c("y", "n", "y", "n", "y", "n"), 
                         impact.x.r = seq(0, 50, 10))
points
lat lon type impact.x.r
1   0   0    y          0
2  10  10    n         10
3  20  20    y         20
4  30  30    n         30
5  40  40    y         40
6  50  50    n         50

然后我在 ggplot 中映射点 - 一切正常:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size_continuous(name = "impact")

工作ggplot代码

但是,一旦我尝试使用:设置比例大小+ scale_size(),我就会收到错误消息。我也失去了为尺寸美学图例所做的格式:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size_continuous(name = "impact") + 
  scale_size(range = c(0,15))

Scale for 'size' is already present. Adding another scale for 'size', which
will replace the existing scale.

通过添加 size_range() 破坏了 ggplot 代码

如果我切换scale_size_continuous()andscale_size()调用的顺序,我仍然会收到错误消息。这样,我保留了格式,但失去了重新调整大小的比例:

ggplot() + 
  world + 
  geom_point(data = points, aes(x = lon, y = lat, col = type, size = impact.x.r)) + 
  scale_color_discrete(name = "types of birds", breaks = c("n","y"), labels = c("no", "yes")) +
  scale_size(range = c(0,15)) +
  scale_size_continuous(name = "impact")
 Scale for 'size' is already present. Adding another scale for 'size', which
will replace the existing scale.    

另一段损坏的ggplot代码

所以它使用最后一次调用 scale_size ......

但是如何修改尺寸美学的图例并将 scale_size 设置为适合我的地图的东西?

4

0 回答 0