Reputation: 520
Here is what my current output looks like. Right now, the points are different shades of blue.
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)
Upvotes: 0
Views: 147
Reputation: 17069
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 reprex package (v2.0.1)
The {colorBlindness} package is also a good resource. Its introductory vignette lists other colorblind-safe palettes.
Upvotes: 1
Reputation: 41235
I used a colorblind-friendly palette with black from this link: http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/. You can change the colors and number of colors in the cbbpalette code below. You can use this code:
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
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) + scale_color_gradientn(colours = cbbPalette)
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)
Output:
Upvotes: 0