Home > database >  In ggplot2, unable to change point colors whose color I want to vary
In ggplot2, unable to change point colors whose color I want to vary

Time:03-07

Here is what my current output looks like. Right now, the points are different shades of blue.

enter image description here

  1. How can I make the points different shades of another color, e.g., purple.
  2. How can I choose the colors for the points from colors in a colorblind-friendly palette?

When I have tried to do it, I get an error such as this one: "discrete value supplied to continuous scale."

I have looked at stackoverflow solutions for my problem, but did not see what that enabled me to solve it. If one exists, I would appreciate it if you can share its link.

My dput

structure(list(Player = c("Al Jackson", "Dwight Gooden", "Scott Erickson", 
"Wally Whitehurst", "Larry Miller", "Jack Hamilton", "Brian Bohanon", 
"Greg Harris", "Julio Valera", "Mark Bomback", "Frank Viola", 
"Mark Clark", "Rafael Montero", "Jay Hook", "John Pacella", "Dave Roberts", 
"Juan Berenguer", "Bill Hepler", "Mike Bruhert", "Ed Lynch", 
"Brian Bannister", "Jack Fisher", "Rob Gardner", "Dillon Gee", 
"Mickey Lolich", "Tug McGraw", "John Candelaria", "Rich Hill", 
"Mike Hampton", "Tom Seaver", "Octavio Dotel", "Jason Isringhausen", 
"Jackson Todd", "Nelson Figueroa", "Galen Cisco"), QS = c(73, 
209, 1, 8, 1, 7, 9, 5, 1, 10, 52, 33, 9, 24, 7, 1, 4, 1, 8, 48, 
2, 73, 8, 58, 18, 12, 1, 2, 18, 293, 7, 25, 3, 7, 30), W = c(36, 
137, 0, 3, 0, 4, 5, 2, 1, 7, 35, 19, 2, 10, 3, 0, 1, 0, 3, 24, 
1, 32, 2, 34, 7, 7, 1, 0, 13, 173, 5, 14, 2, 3, 12)), row.names = c(NA, 
-35L), class = c("tbl_df", "tbl", "data.frame"))

My code

gQ <- ggplot(qs_dput, aes(x=QS, y=W, group=1))   geom_point(aes(col=W), size=3)   geom_smooth(method="lm", col="orange", se=FALSE) 

gQ2 <- gQ   coord_cartesian(xlim=c(0,300), ylim=c(0,180))   scale_x_continuous(breaks = seq(0, 300, by = 25))   scale_y_continuous(breaks = seq(0, 180, by = 30))  
  labs(title="Wins by Mets Pitchers in Their Quality Starts", x="Quality Starts", y="Wins", caption="Data from Stathead")   labs(colour = "Wins")

plot(gQ2)

CodePudding user response:

You need to use a palette designed for continuous variables. In the case of Brewer palettes, this would be a sequential (e.g., "Blues") or diverging (e.g., "PuOr") palette, but not a qualitative palette (e.g., "Set1"). You also need to pass continuous Brewer palettes to scale_color_distiller(), while discrete Brewer palettes are set via scale_color_brewer().

Information including palette type and colorblind safety is in RColorBrewer::brewer.pal.info. So to find continuous, colorblind-safe palettes:

library(ggplot2)
library(RColorBrewer)

brewer.pal.info[
  brewer.pal.info$colorblind & brewer.pal.info$category != "qual", 
]
#>         maxcolors category colorblind
#> BrBG           11      div       TRUE
#> PiYG           11      div       TRUE
#> PRGn           11      div       TRUE
#> PuOr           11      div       TRUE
#> RdBu           11      div       TRUE
#> RdYlBu         11      div       TRUE
#> Blues           9      seq       TRUE
#> BuGn            9      seq       TRUE
#> BuPu            9      seq       TRUE
#> GnBu            9      seq       TRUE
#> Greens          9      seq       TRUE
#> Greys           9      seq       TRUE
#> Oranges         9      seq       TRUE
#> OrRd            9      seq       TRUE
#> PuBu            9      seq       TRUE
#> PuBuGn          9      seq       TRUE
#> PuRd            9      seq       TRUE
#> Purples         9      seq       TRUE
#> RdPu            9      seq       TRUE
#> Reds            9      seq       TRUE
#> YlGn            9      seq       TRUE
#> YlGnBu          9      seq       TRUE
#> YlOrBr          9      seq       TRUE
#> YlOrRd          9      seq       TRUE

"Purples" is continuous and colorblind-safe, so let's try that:

gQ <- ggplot(qs_dput, aes(x=QS, y=W, group=1))   
  geom_point(aes(col=W), size=3)   
  geom_smooth(method="lm", col="orange", se=FALSE)

gQ2 <- gQ   
  coord_cartesian(xlim=c(0,300), ylim=c(0,180))   
  scale_x_continuous(breaks = seq(0, 300, by = 25))   
  scale_y_continuous(breaks = seq(0, 180, by = 30))  
  scale_color_distiller(palette = "Purples", direction = 1)  
  labs(
    title="Wins by Mets Pitchers in Their Quality Starts", 
    x="Quality Starts", 
    y="Wins", 
    caption="Data from Stathead",
    colour = "Wins"
  )

gQ2

Created on 2022-03-06 by the enter image description here

  • Related