Skip to content

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
A data.frame: 3 × 2
xy
<dbl><dbl>
14
25
36

ggplot

syntax

ggplot(dataframe, aes(x = col-of-dataframe, y = col-of-dataframe)) + plottype()

plottypes are

  • geom_point() scatter graph
  • geom_line() line
  • geom_bar() - bar - need one axis only, frequency on other
  • geom_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
A data.frame: 7 × 2
xy
<dbl><dbl>
1 2
2 4
3 6
4 8
510
511
512

Scatter plot

  • use geom_point()
ggplot(df, aes(x = x, y = y)) + geom_point()

png

ggplot(df, aes(x = x, y = y)) + geom_line()

png

ggplot(df, aes(x = x)) + geom_bar()

png

ggplot(df, aes(x = x)) + geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

png

more customizations

ggplot(df, aes(x = x, y = y)) + geom_point() + labs(title = "My Scatterplot", x = "X Variable", y = "Y Variable")

png

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))

png