I am using stat qq plots to indicate normality of any given variable. I wish to colour each plot with a second variable so that I can see the distribution of each factor level in this second variable in terms of the normality distribution of the first variable. I can achieve this when I produce plots one by one. However, I would like to use purrr::map to iterate through a list of secondary variables. I have four secondary variables, and so would prefer not to have to duplicate the code several times
I have spent considerable time looking online and also reading through the questions and answers on here.
I use the following code. Following the sample data to create a reproducible example, I first create a gglayers object, which contains ggplot meta data, so as to avoid duplicating it in every plot
RSKPH2 <- data.frame(
release_speed_kph =c(87.42, 141.37, 133.41, 89.12, 96.64, 141.39, 137.16, 98.09, 144.22, 101.19),
batting_hand = c("left", "right", "right", "right", "right", "left", "right", "right", "left", "right"),
bowling_hand = c("right", "right", "left", "right", "left", "right", "right", "right", "left", "right"),
bowling_type = c("spin", "pace", "pace", "spin", "spin", "pace", "pace", "spin", "pace", "pace"),
wicket = c("Wicket", "No Wicket", "No Wicket", "Wicket", "No Wicket", "No Wicket", "Wicket", "No Wicket", "Wicket", "Wicket")
)
gglayers <- list(stat_qq(),
stat_qq_line(),
theme_classic2(),
theme(plot.title = element_text(hjust = 0.5)),
xlab("Theoretical Quantiles"),
ylab("Sample Quantiles"))
The following object informs R which title to use for each plot:
RSKPH2_Titles <- c("Release Speed Kph with Batting Hand QQ Plot",
"Release Speed Kph with Bowling Hand QQ Plot",
"Release Speed Kph with Bowling Type QQ Plot",
"Release Speed Kph with Wicket QQ Plot")
Then, the following object creates a vector of the names of the secondary variables I wish to use - to allow purrr::map to iterate through:
RSKPH2_names <- c("batting_hand", "bowling_hand", "bowling_type", "wicket")
Now that the required objects have been created, I use the following code in an attempt to enable purrr::map to iterate through:
TP2 <- RSKPH2 %>%
map( ~ {ggplot(RSKPH2,
aes(sample = release_speed_kph)) + geom_line(aes(color = RSKPH2_names)) + labs(title = RSKPH2_Titles) + gglayers}
)
TP2
I get the following error:
Error in geom_line()
:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in check_aesthetics()
:
! Aesthetics must be either length 1 or the same as the data (194368)
✖ Fix the following mappings: colour
I use the following code to successfully create one plot:
RSKPH_wicket <- ggplot(RSKPH2,
aes(sample = release_speed_kph, colour = wicket)) +
ggtitle("Release Speed Kph with Wicket QQ Plot") + gglayers
RSKPH_wicket
EDIT
When I use the code from the answer on the larger data, the below plot results. I have taken the second of the four plots as an example. An additional item has been added to the legend, which is not found in the data. How can I remove this?