1/4
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
ggplot2
library(ggplot2)
ggplot(df, aes(x=col1, y=col2)) +
geom_point() + # scatter plot
geom_line() + # line
geom_smooth() + # regression/smoother line
geom_histogram() + # histogram (use aes(x=col) only)
labs(x="X label", y="Y label", title="Title") +
theme(plot.title=element_text(hjust=0.5)) # center title
aes() maps data columns to visual properties. Add layers with +.
qplot
qplot(x, y) # scatter
qplot(x, y, geom="line") # line
qplot(values, geom="histogram") # histogram
Shortcut for simple plots. Less flexible than full ggplot but quick for exploration.
to customize
# color by group
aes(x=col1, y=col2, color=group_col)
# size of points
geom_point(size=2)
x axis as dates — works automatically if col is Date type
# label specific points
geom_smooth(method="lm") # linear regression line specifically
plotly
library(plotly)
plot_ly(data=df, x=~col1, y=~col2,
type="scatter", mode="markers")
Note the ~ before column names. Makes charts zoomable and hoverable.
using pipes in plots
df %>%
filter(col > 5) %>%
ggplot(aes(x=col1, y=col2)) +
geom_point()
Use data=. when piping — the dot means "use whatever came through the pipe."