Load libraries.
library(ggplot2)
Set seed.
set.seed(1392)
Create table of ratings for foods.
ratings <- data.frame(Rating = sample(1:10,5,replace=FALSE),
Food = c("Mashed potatoes","Green bean casserole","Sweet potato casserole","Turkey","Cranberry sauce"),
stringsAsFactors=FALSE)
ratings
## Rating Food
## 1 8 Mashed potatoes
## 2 10 Green bean casserole
## 3 4 Sweet potato casserole
## 4 3 Turkey
## 5 5 Cranberry sauce
Make barplot.
ggplot(ratings,
aes(x=Food,y=Rating)) +
geom_bar(stat="identity")
Add some fun colors.
ggplot(ratings,
aes(x=Food,y=Rating,fill=Food)) +
geom_bar(stat="identity") +
scale_fill_manual(values=c("red","green","#fffff2","orange","brown"))
Oops, need to remove legend.
ggplot(ratings,
aes(x=Food,y=Rating,fill=Food)) +
geom_bar(stat="identity") +
scale_fill_manual(values=c("red","green","#fffff2","orange","brown")) +
guides(fill=FALSE)
Actually, let’s color only green bean casserole and turkey, since it seems rating for one is super high and one is super low.
ggplot(ratings,
aes(x=Food,y=Rating,fill=Food)) +
geom_bar(stat="identity") +
scale_fill_manual(values=c("grey","green","grey","grey","brown")) +
guides(fill=FALSE)