I am using the library ggplot2movies
for my data movies
Please keep in mind that I refer to mpaa rating and user rating, which are two different things. In case you don't want to load the ggplot2movies
library, here is a sample of the relevant data:
> head(subset(movies[,c(5,17)], movies$mpaa!=""))
# A tibble: 6 x 2
rating mpaa
<dbl> <chr>
1 5.3 R
2 7.1 PG-13
3 7.2 PG-13
4 4.9 R
5 4.8 PG-13
6 6.7 PG-13
Here I make a barplot that shows the frequency of films that have any mpaa rating:
ggplot(data=subset(movies, movies$mpaa!=""), aes(mpaa)) +
geom_bar()
Now I would like to color in the bars with a fill, based on the imdb user rating. I don't want to use factor(rating)
because there are an enormous number of different values in the rating column. However, when I try to use a continuous fill like in Assigning continuous fill color to geom_bar I get the same graph.
ggplot(data=subset(movies, movies$mpaa!=""), aes(mpaa, fill=rating)) +
geom_bar()+
scale_fill_continuous(low="blue", high="red")
I figure it has to do with the fact that my barplot is based on the frequency of a single variable, rather than a dataframe with a count column. I could make a new dataframe of the mpaa categories and their counts, but I'd rather know how to do this graph with the original movies
dataset and a single ggplot.
Edit: Using aes(mpaa, group = rating, fill = rating)
gives a chart that is almost correct, except that the bars and legend are swapped.