0

I am trying to plot a hollow bubble plot. The plot is fine except the legend is not showing the bubble size in the legend. It can be seen in the legend the increment in the bubble is not shown in legend.

How can I fix this?

I did look at this and this question, but it did not fix the legend.

Code + sample data

library(elevatr)
library(sf)
library(sp)
library(tidyverse)

# Create an example data.frame
set.seed(65.7)
examp_df = data.frame(x = runif(3, min = -73, max = -72.5), y = runif(3, min = 42,
    max = 43))
prj_dd = "EPSG:4326"

# Create and example data.frame with additional columns
cats = data.frame(category = c("H", "M", "L"))

examp_df2 = data.frame(examp_df, cats)

# Create an example SpatialPoints
examp_sp = SpatialPoints(examp_df, proj4string = CRS(prj_dd))

    # Create an example SpatialPointsDataFrame
    examp_spdf = SpatialPointsDataFrame(examp_sp, data = cats)
    # Get elevation data
    spdf_elev_epqs = get_elev_point(examp_spdf, src = "epqs")
    # Convert to sf object
    examp_sfdf = st_as_sf(spdf_elev_epqs)
    # Plot
    ggplot() +
      geom_sf(data = examp_sfdf, aes(size = elevation ), shape = 1, show.legend = T) +
      coord_sf() +
      theme(axis.text.x = element_text(angle = 90)) +
          labs( size = "Mean Elevation (meters)")

Plot

enter image description here

Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 08 '21 at 07:50
  • @MrFlick, done, please check. – Ed_Gravy Dec 08 '21 at 08:26
  • @MrFlick, please note that that I used the `elevatr` package to get the sample `DEM` values. – Ed_Gravy Dec 08 '21 at 08:28
  • 1
    My legend works after running your example: The bubbles have different sizes. (R 4.1.0) – danlooo Dec 08 '21 at 09:03
  • @danlooo, this is strange, did you modify the code in the question or did you run the exact code? – Ed_Gravy Dec 08 '21 at 09:05
  • 1
    I did an exact rerun of the code you provided on a fresh environment. – danlooo Dec 08 '21 at 09:05
  • @danlooo, wow, could this be a glitch in `RStudio` then? Should I restart the studio? – Ed_Gravy Dec 08 '21 at 09:06
  • 1
    This must be due to R itself. RStudio is just the IDE. If a session restart did not work, you can try isolated fresh docker images (I used `rocker/verse:4.1.0`) – danlooo Dec 08 '21 at 09:08
  • @danlooo, turns out not running `set.seed(65.7)` was causing the issue. I don't know how `set.seed` relates to plotting a correct `ggplot` bubble legend! Thanks for your time and help. – Ed_Gravy Dec 08 '21 at 09:35
  • 1
    You probably cleaned the environment just when you incorporated the seed – danlooo Dec 08 '21 at 09:52
  • @danlooo, yes, you are absolutely right, and it turns out as soon I start storing variables in the environment, the legend problem returns back. I don't know what's really going on here. – Ed_Gravy Dec 08 '21 at 09:56
  • 1
    Looks like a bug in the packages you are using: Some things might not be properly isolated from the global environment. – danlooo Dec 08 '21 at 10:01
  • @danlooo, is there a work around this instead of restarting the session every time? I am actually not familiar with `docker` on `Windows 10`. – Ed_Gravy Dec 08 '21 at 10:17
  • 1
    It is called docker desktop on win10. If you have wsl2, you can also just use the Linux subsystem so you can just install the docker engine directly – danlooo Dec 08 '21 at 10:20

1 Answers1

2

EDIT: Newer versions of the question involves only one size scale.

It seems that the aesthetic size is used in both the rectangle and in the circle geom. ggplot2 merges the axes by default and only shows one of them. You can use the R package ggnewscale to explicitly have multiple size scales.

Here is a minimal reproducible example:

library(tidyverse)
library(ggnewscale)

diamonds %>%
  head() %>%
  ggplot(aes(cut, color)) +
    geom_point(aes(size = x), shape = 1, color = "red") +
    scale_size(limits = c(0, 10)) +
    new_scale(new_aes = "size") +
    geom_point(aes(size = y), shape = 1, color = "blue")

Created on 2021-12-08 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • thank you but when I use the `scale_size` and `new_scale` arguments in the above code, I get this error `Error in check.length(gparname) : 'gpar' element 'fontsize' must not be length 0`. Also in my case I only have one `size` argument. – Ed_Gravy Dec 08 '21 at 08:58
  • 1
    your new example has only one size scale thus you do not need `new_scale` anymore – danlooo Dec 08 '21 at 09:04