data frame
# Create a data frame with two columns and three rows
my_data <- data.frame(
x = c(1, 2, 3),
y = c(4, 5, 6)
)
# View the data frame
my_data
x | y |
---|---|
<dbl> | <dbl> |
1 | 4 |
2 | 5 |
3 | 6 |
ggplot
syntax
ggplot(dataframe, aes(x = col-of-dataframe, y = col-of-dataframe)) + plottype()
plottypes are
geom_point()
scatter graphgeom_line()
linegeom_bar()
- bar - need one axis only, frequency on othergeom_histogram()
- histogram - need one axis only, frequency on other
library(ggplot2)
df <- data.frame(
x = c(1,2,3,4,5,5,5),
y = c(2,4,6,8,10,11,12)
)
df
x | y |
---|---|
<dbl> | <dbl> |
1 | 2 |
2 | 4 |
3 | 6 |
4 | 8 |
5 | 10 |
5 | 11 |
5 | 12 |
Scatter plot
- use
geom_point()
ggplot(df, aes(x = x, y = y)) + geom_point()
ggplot(df, aes(x = x, y = y)) + geom_line()
ggplot(df, aes(x = x)) + geom_bar()
ggplot(df, aes(x = x)) + geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
more customizations
ggplot(df, aes(x = x, y = y)) + geom_point() + labs(title = "My Scatterplot", x = "X Variable", y = "Y Variable")
ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "My Scatterplot", x = "X Variable", y = "Y Variable") +
scale_x_continuous(limits = c(0, 100)) +
scale_y_continuous(limits = c(0, 100))