rggplot2fillbubble-chart

change fill color of continuous bubbleplot ggplot


I want to change the colors of the bubbles in the plot

ggplot(sum_length, 
       aes(x=vb_width, y=slope, size = length)) +
    geom_point(alpha=0.5)+
   theme_classic() +
  ylab("Slope category (%)") +
  xlab("Valley bottom width (m)") +
  scale_y_discrete(labels= slopebins)+
  scale_size(range = c(.1, 12), name="Amount of occupied\n habitat (km)")

So, I would like each size to also be a different color, on the viridis palette if possible. I cannot use this solution because the size value is continuous for my data and for the example their data are discrete: Editing ggplot2 Bubble Plot in R: Size, colors, and labels

Data:

  slope vb_width MU      length
1   1to3     <35m 21  26.0568635
2  3to10     <35m 21 133.8732929
3   gt10     <35m 21  49.9302440
4    lt1     <35m 21  13.6335112
5   1to3 >285-427 21   9.5806268
6  3to10 >285-427 21   0.3888544
7   gt10 >285-427 21   0.0000000
8    lt1 >285-427 21  24.3259779
9   1to3   >35-70 21  66.0737107
10 3to10   >35-70 21 140.3568265
11  gt10   >35-70 21  23.9712848
12   lt1   >35-70 21  42.4018746
13  1to3     >427 21   9.0315019
14 3to10     >427 21   0.3993908
15  gt10     >427 21   0.0000000
16   lt1     >427 21 163.8209682
17  1to3  >70-285 21  72.3702302
18 3to10  >70-285 21  38.4964008
19  gt10  >70-285 21   7.9305055
20   lt1  >70-285 21  85.7479024

Solution

  • Some minor tweaks:

    E.g.

    library(tidyverse)
    
    df <- read.table(text = "  slope vb_width MU      length
    1   1to3     <35m 21  26.0568635
    2  3to10     <35m 21 133.8732929
    3   gt10     <35m 21  49.9302440
    4    lt1     <35m 21  13.6335112
    5   1to3 >285-427 21   9.5806268
    6  3to10 >285-427 21   0.3888544
    7   gt10 >285-427 21   0.0000000
    8    lt1 >285-427 21  24.3259779
    9   1to3   >35-70 21  66.0737107
    10 3to10   >35-70 21 140.3568265
    11  gt10   >35-70 21  23.9712848
    12   lt1   >35-70 21  42.4018746
    13  1to3     >427 21   9.0315019
    14 3to10     >427 21   0.3993908
    15  gt10     >427 21   0.0000000
    16   lt1     >427 21 163.8209682
    17  1to3  >70-285 21  72.3702302
    18 3to10  >70-285 21  38.4964008
    19  gt10  >70-285 21   7.9305055
    20   lt1  >70-285 21  85.7479024
    ", header = TRUE)
    
    df %>%
      ggplot(aes(x=vb_width, y=slope, size = length, fill = length)) +
      geom_point(alpha=0.5, shape = 21)+
      theme_classic() +
      ylab("Slope category (%)") +
      xlab("Valley bottom width (m)") +
      #scale_y_discrete(labels=slopebins)+
      scale_size(range = c(.1, 12), name="Amount of occupied\n habitat (km)") +
      scale_fill_viridis_c()
    

    Created on 2023-11-29 with reprex v2.0.2


    EDIT: To combine the legends you need to make them the same 'type' (guide_legend) and they need the same name, e.g.

    library(tidyverse)
    
    df <- read.table(text = "  slope vb_width MU      length
    1   1to3     <35m 21  26.0568635
    2  3to10     <35m 21 133.8732929
    3   gt10     <35m 21  49.9302440
    4    lt1     <35m 21  13.6335112
    5   1to3 >285-427 21   9.5806268
    6  3to10 >285-427 21   0.3888544
    7   gt10 >285-427 21   0.0000000
    8    lt1 >285-427 21  24.3259779
    9   1to3   >35-70 21  66.0737107
    10 3to10   >35-70 21 140.3568265
    11  gt10   >35-70 21  23.9712848
    12   lt1   >35-70 21  42.4018746
    13  1to3     >427 21   9.0315019
    14 3to10     >427 21   0.3993908
    15  gt10     >427 21   0.0000000
    16   lt1     >427 21 163.8209682
    17  1to3  >70-285 21  72.3702302
    18 3to10  >70-285 21  38.4964008
    19  gt10  >70-285 21   7.9305055
    20   lt1  >70-285 21  85.7479024
    ", header = TRUE)
    
    df %>%
      ggplot(aes(x=vb_width, y=slope, size = length, fill = length)) +
      geom_point(alpha=0.5, shape = 21)+
      theme_classic() +
      ylab("Slope category (%)") +
      xlab("Valley bottom width (m)") +
      #scale_y_discrete(labels=slopebins)+
      scale_size_continuous(range = c(.1, 12), name="Amount of occupied\n habitat (km)",
                            guide = guide_legend()) +
      scale_fill_viridis_c(name = "Amount of occupied\n habitat (km)",
                           guide = guide_legend())
    

    Or with guide_bins():

    df %>%
      ggplot(aes(x=vb_width, y=slope, size = length, fill = length)) +
      geom_point(alpha=0.5, shape = 21)+
      theme_classic() +
      ylab("Slope category (%)") +
      xlab("Valley bottom width (m)") +
      #scale_y_discrete(labels=slopebins)+
      scale_size_continuous(range = c(.1, 12), name="Amount of occupied\n habitat (km)",
                            guide = guide_bins()) +
      scale_fill_viridis_c(name = "Amount of occupied\n habitat (km)",
                           guide = guide_bins())
    

    Created on 2023-11-30 with reprex v2.0.2