Aug 10, 2014 - Some terminology. ▷ data. ▷ aesthetics. ▷ geometry. ▷ The geometric objects in the plot. ▷ points, lines,
, color="black")
8
6 variable
value
Sepal.Length Sepal.Width
4
Petal.Length Petal.Width 2
0 setosa
versicolor
Species
virginica
Exercise 3 Using the d2 dataset you created earlier, generate this plot below. Take a quick look at the data first to see if it needs to be binned. 100
cut
75
count
Fair Good Very Good
50
Premium Ideal 25
0 I1
SI2
SI1
VS2
VS1
clarity
VVS2 VVS1
IF
Exercise 4 I
Using the climate dataset, create a new variable called sign. Make it logical (true/false) based on the sign of Anomaly10y.
I
Plot a bar plot and use sign variable as the fill.
Anomaly10y
0.5 sign FALSE TRUE 0.0
1920
1950
Year
1980
Section 13 Density Plots
Density plots ggplot(faithful, aes(waiting)) + geom_density()
density
0.03
0.02
0.01
0.00 50
60
70
waiting
80
90
Density plots ggplot(faithful, aes(waiting)) + geom_density(fill = "blue", alpha =
.1)
density
0.03
0.02
0.01
0.00 50
60
70
waiting
80
90
ggplot(faithful, aes(waiting)) + geom_line(stat = "density")
density
0.03
0.02
0.01
50
60
70
waiting
80
90
Section 14 Adding smoothers
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(aes(shape = Species), size = 3) + geom_smooth(method = "lm") 4.5
● ● ●
Sepal.Width
4.0
3.5
3.0
● ● ● ● ● ●● ● ●● ●●● ● ● ● ●●● ● ●● ● ●● ● ● ●● ●● ●●● ●
Species ● setosa
versicolor virginica
2.5 ●
2.0 5
6
Sepal.Length
7
8
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(aes(shape = Species), size = 3) + geom_smooth(method = "lm") + facet_grid(. ˜ Species) setosa
4.5
versicolor
virginica
● ●
Sepal.Width
4.0
3.5
3.0
● ● ● ● ● ●●● ● ●● ●●● ● ●●●●●● ●● ●●● ● ●●● ●● ●●● ●
Species ● setosa
versicolor virginica
2.5 ●
2.0 5
6
7
8
5
6
7
Sepal.Length
8
5
6
7
8
Section 15 Themes
Adding themes
Themes are a great way to define custom plots. + theme() # see ?theme() for more options
A more basic theme ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(size = 1.2, shape = 16) + facet_wrap( ˜ Species) + theme_bw() setosa
4.5
versicolor
virginica
●
● ●
4.0
● ● ●
●
● ●
Sepal.Width
● ●● ●
●●
3.5
●●●
●
● ● ●●● ●
●
●● ● ●●
●● ●
●
●
●
●
●
● ●●
3.0
●●
Species
●
●●
● ●
●●●
● ●● ● ●
●
●● ● ●
●
●●
●●●
●●● ● ● ●
●
● ● ●
●
● ● ●
● ●●
● ●
●●●
●
●●
●●
●
●
●
● ●
●
● ●
●
● ●
●
5
6
7
8
5
6
7
Sepal.Length
8
5
6
setosa
●
versicolor
●
virginica
●●
● ●
● ●
●
●●
● ●
2.0
●● ● ●
● ●
● ●●
2.5
●
7
8
A themed plot
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(size = 1.2, shape = 16) + facet_wrap( ˜ Species) + theme(legend.key = element_rect(fill = NA), legend.position = "bottom", strip.background = element_rect(fill = NA), axis.title.y = element_text(angle = ))
A themed plot
setosa
4.5
versicolor
virginica
● ●
4.0 3.5
Sepal.Width
3.0
● ● ● ● ● ●●● ● ●● ●●● ● ●●●●●● ●● ●●● ● ●●● ●● ●●● ●
●● ● ● ● ● ● ● ●● ●●●●● ●● ●● ●●●●● ● ● ●● ● ●●● ● ●●● ● ●● ● ● ● ● ● ● ●●
2.5 ●
2.0
●
●● ● ● ●● ●● ● ● ●● ● ●●● ●●● ●● ●● ● ● ●● ●●● ●● ● ●● ● ● ● ● ● ●
●
5 6 7 8
5 6 7 8
5 6 7 8
Sepal.Length Species
●
setosa
●
versicolor
●
virginica
ggthemes library
install.packages(ggthemes) library(ggthemes) # Then add one of these themes to your plot + theme_stata() + theme_excel() + theme_wsj() + theme_solarized()
Fan of Wes Anderson movies?
Yup, that’s a thing # install.packages(wesanderson) library("wesanderson") # display a palette display.wes.palette(4, "Royal1")
Royal1
Section 16 Create functions to automate your plotting
Write functions for day to day plots
my_custom_plot