I recently gave a talk at the Montreal R User Group about my favourite data visualization library, ggplot2, as well as rpivotTable, the R interface to my own PivotTable.js
As you can see in the video above, during the talk I just scrolled through an R file in RStudio. What you see below is the result of slightly modifying that file and running it through the RMarkdown process to capture the output.
library(ggplot2)
ggplot(mtcars)
ggplot(mtcars) +
geom_bar( aes(x=factor(cyl)) )
ggplot(mtcars) +
geom_bar( aes(x=factor(cyl), fill=factor(gear)) )
ggplot(mtcars) +
geom_bar( aes(x=factor(cyl), fill=factor(gear)), position="dodge" )
ggplot(mtcars) +
geom_bar( aes(x=factor(cyl), fill=factor(cyl)) )
ggplot(mtcars) +
geom_bar( aes(x=factor(1), fill=factor(cyl)), width=1 )
ggplot(mtcars) +
geom_bar( aes(x=factor(1), fill=factor(cyl)), width=1) +
coord_polar(theta="y")
ggplot(mtcars) +
geom_point( aes(x=mpg, y=disp) )
ggplot(mtcars) +
geom_point( aes(x=mpg, y=disp) ) +
geom_smooth( aes(x=mpg, y=disp) )
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_point() + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
ggplot(mtcars, aes(x=mpg, y=disp, color=factor(cyl))) +
geom_point() + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_point( aes(color=factor(cyl)) ) + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_point( aes(color=factor(cyl)), size=4) + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
ggplot(mtcars) +
geom_point( aes(x=mpg, y=disp, color=factor(cyl)), size=4) +
facet_grid(gear~am)
qplot(data=mtcars, geom="point", x=mpg, y=disp, color=factor(cyl), size=I(4), facets=gear~am)
qplot(data=mtcars, x=mpg, y=disp, color=factor(cyl), size=I(4), facets=gear~am) +
scale_colour_manual(values = c("red","blue", "green")) +
labs(title="Displacement vs MPG by Transmission Type & Cylinders\n\n") +
theme(legend.position="bottom")
See also: http://docs.ggplot2.org/
From /content/2015/03/mp_dotplot/
mps = read.csv("mps.csv")
head(mps)
## Name Party Province Age Gender
## 1 Liu, Laurin NDP Quebec 22 Female
## 2 Mourani, Maria Bloc Quebecois Quebec 43 Female
## 3 Sellah, Djaouida NDP Quebec NA Female
## 4 St-Denis, Lise NDP Quebec 72 Female
## 5 Fry, Hedy Liberal British Columbia 71 Female
## 6 Turmel, Nycole NDP Quebec 70 Female
mps$Province = factor(mps$Province, levels=c("British Columbia", "Alberta", "Saskatchewan",
"Manitoba", "Territories", "Ontario", "Quebec", "New Brunswick", "Prince Edward Island",
"Nova Scotia", "Newfoundland and Labrador"))
levels(mps$Province) = c("BC", "AB", "SK", "MB", "YK/NT/NU", "ON", "QC", "NB", "PE", "NS", "NL")
mps$Party = factor(mps$Party, levels=c("Conservative", "NDP", "Liberal", "Bloc Quebecois", "Green"))
levels(mps$Party) = c("CON", "NDP", "LIB", "BLQ", "GRN")
ggplot(subset(mps, !is.na(Age)), aes(x=Age,
fill=factor(Gender), color=factor(Gender))) +
geom_dotplot(stackgroups = TRUE, binwidth=5, method="histodot") +
facet_grid(Party~Province, space="free_y", scales="free_y") +
scale_y_continuous(breaks = NULL) +
theme(legend.position="bottom", legend.title=element_blank()) +
labs(y="", x="Age in bins of 5 years", color="", fill="", title = expression(atop(
"Canadian Members of Parliament in 2012 by Province, Party, Age & Gender",
atop(italic("each dot represents one MP; 3 are missing due to unknown birthdates"), ""))))
My PivotTable.js library: /pivottable/examples/
rpivotTable: https://github.com/smartinsightsfromdata/rpivotTable
library(rpivotTable)
rpivotTable(mtcars)
read.csv("mps.csv")
rpivotTable(mps)
⁂