Graphics Lecture
Graphics Lecture
add ‘geoms’ – graphical representations of the data in the plot (points, lines,
bars). ggplot2 offers many different geoms; some common ones includes:
●
geom_point() for scatter plots, dot plots, etc.
●
geom_boxplot() for, well, boxplots!
●
geom_line() for trend lines, time series, etc.
●
geom_col() for making columns
The grammar of ggplot2
ggplot(mydata100, aes(x = factor(""), fill = workshop) ) +
geom_bar()
ggplot(mydata100,
aes(x = factor(""), fill = workshop) ) +
geom_bar() +
coord_polar(theta = "y") +
scale_x_discrete("")
The grammar of ggplot2
●
Aesthetics(aes()): We typically understand aesthetics as how
something looks, color, size etc. Aesthetics do not refer how something
looks in R
●
these are the roles that the variables play in each graph. A variable may
control where points appear,
●
the color or shape of a point, the height of a bar and so on.
●
map a variable to a visual cue.
●
aes(y = gdp, x = educ)
●
aes(label = country, color = net_users)
●
ggplot(data, aes(x=distance, y= dep_delay)) +
●
geom_point(color=blue)
Aesthetics
g <- ggplot(data = CIACountries, aes(y = gdp, x = educ))
g + geom_point(size = 3)
g + geom_text(aes(label = country, color = net_users), size = 3)
g+
geom_point(alpha = 0.9, aes(size = roadways)) +
coord_trans(y = "log10") +
facet_wrap(~net_users, nrow = 1) +
theme(legend.position = "top")
Canonical data graphics in R
●
Univariate displays: how a single variable is distributed
●
variable is numeric, then its distribution is commonly summarized
graphically using a histogram or density plot.
●
g <- ggplot(data = SAT_2010, aes(x = math))
●
g + geom_histogram(binwidth = 10) + labs(x = "Average math SAT
score")
Canonical data graphics in R
g + geom_density(adjust = 0.3)
g + facet_wrap(~
SAT_rate)
Canonical data graphics in R
g + facet_wrap(~ SAT_rate)
Canonical data graphics in R
Maps: Geographically
distributed data
Canonical data graphics in R
Networks:is a set of connections, called edges, between
nodes, called vertices. A vertex represents an entity. The
edges indicate pairwise relationships between those
entities.