Home > Back-end >  Fixing legends and color scheme of ggplot2 bubble grid plot
Fixing legends and color scheme of ggplot2 bubble grid plot

Time:01-25

Problem

I'm trying to create a bubble grid heatmap such as the one found Plot as it currently stands

Data

structure(list(Statement = c("A1", "A2", "A3", "A4", "A5", "A6", 
"A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "A16", 
"A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", 
"A26", "A27", "A28", "A29", "A30", "A31", "A32", "A33", "A34"
), Period = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Mar-1930", "Apr-1930"
), class = "factor"), Avg = c(4.6, 4.2, 4.6, 4.2, 4.2, 4.8, 4.2, 
3.6, 4, 4.2, 4.6, 4.6, 4.2, 4.2, 4.8, 4.6, 4.6, 4.33333333333333, 
4.5, 4.66666666666667, 4.33333333333333, 4.33333333333333, 4.33333333333333, 
4.33333333333333, 3.5, 3.83333333333333, 4, 4.5, 4.33333333333333, 
4.66666666666667, 4.33333333333333, 4.83333333333333, 4.83333333333333, 
4.66666666666667), Count = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6)), row.names = c(NA, -34L), class = "data.frame")

Environment

sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

CodePudding user response:

  1. How can I remove the old blue gradient legend for fill while keeping the new fill legend?

It looks like you don't actually use your color argument, and you could just remove col = Avg from your aes() function. In ggplot2, when it comes to shapes with an inside like circles color is for the outline, and fill is for the insides.

So removing col = Avg is one option. If you want to keep it for some reason but just don't want the legend, add guides(color = 'none') to your code to get rid of the color legend.

  1. How can I change the color scheme of the new fill legend by selecting an intermediate color? I'd like the color scheme to go from red to white to blue. If there is a way to add more colours that might be even better because the Avgs are quite tightly distributed
  2. How can I change the scale of the fill legend go from 1 (red) to 5 (blue)?

The scale_fill_gradient2() scale function incorporates a "middle". You can also make it run the entire range from 1 to 5 using the limits option. Note that this will give you a fairly narrow color range, since most of your data is only over a narrow range of that scale. The only way to really fix that is to pick low and high colors that are even farther apart. No real way around that - if you want 1 and 5 to have their own colors, then the small range of your data will naturally only cover a tiny part of that range. At best you could start messing with weird transformation functions to make your tiny data range cover a wider share of the color spectrum you've specified.

library(ggplot2)

ggplot(yzlong2, aes(x = Period, y = Statement))  
  geom_point(aes(fill = Avg, size = Count), shape = 21)  
  theme_minimal()  
  theme(
    legend.position = 'top',
    text = element_text(color = 'grey40', size = 20)
  )  
  scale_size_area(max_size = 15, guide='none')  
  scale_fill_gradient2(high = "blue", mid = 'white', low= "red", name="Average",
                      midpoint = 3,
                      limits = c(1, 5))

enter image description here

  • Related