pros of ggplot 2 compared to other packages in R
format needed for ggplot2
- more rows than columns
important terms :
plot basics
layer: geoms
layer: stats
stat_function
statistical transformation rather than the visual appearance
e.g. -> str.identity() -> leave data as is
layer: position adjustment:
resolves overlapping gems; overrides the default of the geom_ or stat_ function
e.g. position_jitter() -> jitter points to avoid overplotting
layer: annotations
special types of layer: they don’t inherit global settings from the plot. they are used to add fixed reference data to plot
e.g. annotate() -> create an annotation layer
layer: scales
control the details of ow data values are translated to visual properties
e.g. scale_colour_continuous() -> apply a continuous colour scale
bar graphs - bar heights ?
-> default is stat_bin
make basic bar graph with ggplot (with value not number of cases in each group)
add or remove colour and legend + black outline
add x, y main labels
library(ggplot2)
# simple plot ggplot(data=dat, aes(x=time, y=total_bill)) + geombar(stat ="identity)
# add colour and legend + black outline ggplot(data=dat, aes(x=time, y=total_bill, fill = time)) + geombar(colour="black", stat ="identity)
# remove legend (if redundant) \+ guides(fill=FALSE)
add x, y, main labels
+ xlab(“”)
+ ylab(“”)
+ggtitle(“”)
code count of cases in bar graph
+ geom_bar(stat = “count”)
how to code bar graph with multiple variables bar
time : xaxis
sex: color fill
total bill: y-axis
how to change colour
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) + geom_bar(stat=”summary”,position = position_dodge())
-> make 2 bars right next to each other for each sex
# change colour
\+ scale_fill_manual(values=c("",""))code line graph
ggplot(data=tips, aes(x=time, y=total_bill, fill=sex)) + geom_line(stat=”summary”, size = 1.5) +
geom_point(stat=”summary”, size = 3)
how to add different symbols into plot
+ scale_shape_manual(values=c(“º”, “Ω”)
how to add error bars
+ geom_errorbars(aes(ymin=total_bill-sd, ymax=total_bill+sd),
width =0.1, position=position_dodge(1))
draw a histogram with black outline and white fill
draw a density curve
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=0.5, colour = “black”, fill = “white”)
ggplot(dat, aes(x=rating)) + geom_density()
histogram add mean line
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=0.5, colour = “black”, fill = “white”) + geom_vline(aes(xintercept = mean(rating, na.rm=T)),
color =”red”, livetype = “dashed”, size = 1
overlaied histograms (two seethrough
ggplot(dat, aes(x=rated, fill=cond)) + geom_histogram(binwidth=.5, aplha=.5, position = “identity”)
interleaved histograms
ggplot(dat, aes(x=rating, fill=cond)) + geom_histogram(binwidth=.5, position=”dodge”)
make a basic boxplot
ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot()
scaterplot points and with linear regression
how to alter theme (the way it looks / lines)
theme(plot.title = element_text(lineheight=.8, face=”bold”))
facets
can divide by levels
-> levels of sex
sp(the normal plotting stiff) + facet_grid(. ~sex)