Primary exercises

  1. Necessary libraries and data.
    In your R Markdown document load the tidyverse library and read the pulse.csv file into the pulse variable.

  2. A series of charts.
    Reproduce the sequence of charts.
    Each next chart is a small modification of the command.

ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point()

ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( aes( color = exercise ) )

ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( aes( color = exercise ), alpha = 0.6 )

ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( aes( color = exercise, shape = gender ), alpha = 0.6 )

ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( aes( color = exercise, shape = gender ), alpha = 0.6, size = 3 )

  1. Labels.
    Check Help for commands xlab, ylab, ggtitle.
    Adjust the last chart to get the chart below.
ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( aes( color = exercise, shape = gender ), alpha = 0.6, size = 3 ) +
  xlab( "Age [years]" ) + ylab( "Weight [kg]" ) +
  ggtitle( "All subjects from pulse.csv" )

Extra exercises

  1. Multiple layers.
    There can be more than one geom_ component in a single plot.
    Compose two geom_point components (so, add one more than used above) to reproduce the following chart.
ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( color = "yellow", size = 3 ) +
  geom_point( color = "black", size = 1, shape = 3 )

  1. Different data in layers.
    By default, geom_ functions use the data provided in the call to the ggplot function.
    This can be changed with the data argument: geom_point( data = otherData ).

    Use the function filter to select from pulse all subjects with height >= 190; store the result in filtPulse. Now, modify the previous plot to draw the black symbol only for filtPulse subjects.
filtPulse <- pulse %>% filter( height >= 190 )
ggplot( pulse, aes( x = age, y = weight ) ) +
  geom_point( color = "yellow", size = 3 ) +
  geom_point( color = "black", size = 1, shape = 3, data = filtPulse )



Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC