This put up exhibits the right way to produce a plot involving three categorical variables
and one steady variable utilizing ggplot2 in R.
The following code can also be out there as a gist on github.
1. Create Information
First, let’s load ggplot2 and create some information to work with:
library(ggplot2)
set.seed(4444)
Information <- increase.grid(group=c("Apples", "Bananas", "Carrots", "Durians",
"Eggplants"),
yr=c("2000", "2001", "2002"),
high quality=c("Grade A", "Grade B", "Grade C", "Grade D",
"Grade E"))
Group.Weight <- information.body(
group=c("Apples", "Bananas", "Carrots", "Durians", "Eggplants"),
group.weight=c(1,1,-1,0.5, 0))
High quality.Weight <- information.body(
high quality=c("Grade A", "Grade B", "Grade C", "Grade D", "Grade E"),
high quality.weight = c(1,0.5,0,-0.5,-1))
Information <- merge(Information, Group.Weight)
Information <- merge(Information, High quality.Weight)
Information$rating <- Information$group.weight + Information$high quality.weight +
rnorm(nrow(Information), 0, 0.2)
Information$proportion.tasty <- exp(Information$rating)/(1 + exp(Information$rating))
2. Produce Plot
And here is the code to supply the plot.
ggplot(information=Information,
aes(x=issue(yr), y=proportion.tasty,
group=group,
form=group,
shade=group)) +
geom_line() +
geom_point() +
opts(title =
"Proportion Tasty by 12 months, High quality, and Group") +
scale_x_discrete("12 months") +
scale_y_continuous("Proportion Tasty") +
facet_grid(.~high quality )
And here is what it seems to be like:
