Here is some base-R code to perform the task (wrapr is just there to give us a pipe operator and make defining the data easy).
library("wrapr")
library("ggplot2")
movie_select <- wrapr::build_frame(
"content_rating", "gross_income", "imdb_score" |
"PG" , 200 , 6.5 |
"PG" , 207 , 7 |
"PG" , 209 , 8 |
"PG-13" , 105 , 9 |
"PG-13" , 110 , 4.5 )
cor_map <- movie_select %.>%
split(., movie_select$content_rating) %.>%
vapply(.,
function(di) {
cor(di$imdb_score, di$gross_income)
}, numeric(1))
print(cor_map)
movie_select$cor <- cor_map[movie_select$content_rating]
movie_select$content_and_cor <- paste(movie_select$content_rating,
movie_select$cor)
ggplot(movie_select ,aes(gross_income,imdb_score))+
geom_point()+
facet_wrap(~content_and_cor, ncol=1, labeller = "label_both") +
ggtitle("correlation between imdb score grouped by content rating")