ggplot2 + geom_point +与大小成比例的气泡(错误“离散值提供给连续刻度”)

时间:2018-10-24 21:20:01

标签: r ggplot2

我正在尝试表示与随人口成比例增加规模的城市相对应的点/气泡。

尽管进行了几次尝试,但我总是遇到相同的错误(“离散值提供给连续刻度”),即使我的变量是数字变量也是如此。

使用的示例数据可以在此处找到

https://github.com/Lulliter/r-ggplot-points-question

  1. Brazil_bound_sf =边界
  2. Brazil_cit_longlat =过去几年人口#的城市列表

该图不起作用

ggplot() + 
  geom_sf (data = Brazil_bound_sf, 
         aes(fill = NULL), alpha = .1,colour = "darkgrey",size = 0.3) +
  geom_point(data = Brazil_cit_longlat, aes( x = lon, y = lat, size = 
           "2016"), fill = "red", color = "grey", alpha = .2) + 
  scale_size_continuous()  +

  coord_sf(crs = st_crs(Brazil_bound_sf), datum = NA) + 
  theme_void() 

# Error: Discrete value supplied to continuous scale

谢谢!

1 个答案:

答案 0 :(得分:1)

这对我有用(我只是用2016替换了“ 2016”。这样,您是指数据框中的列而不是字符串“ 2016”:

ggplot() + 
  geom_sf (data = x, 
           aes(fill = NULL), alpha = .1,colour = "darkgrey",size = 0.3) +
  geom_point(data = y, aes( x = lon, y = lat, size = 
                                               `2016`), fill = "red", color = "grey", alpha = .2) + 
  scale_size_continuous()  + 
  sf::plot_sf(crs = st_crs(x), datum = NA)