ggplot2

What is ggplot2?

The ggplot2 package lets you make high quality, customizable plots of your data
ggplot2 is based on the grammar of graphics – a system for describing and building data visualizations
The idea behind grammar of graphics is that all graphics are built from the same foundation.

The foundation of a grammar graphics:

  • A dataset
  • A set of geoms: A geom refers to the geometric object used to represent your data. For example, you can use points to create a scatterplot, bars to create a bar chart, lines to create a line diagram, etc.
  • A set of aesthetic attributes: An aesthetic is a visual property of an object in your plot. You can think of an aesthetic as a connection, or mapping, between a visual feature in your plot and a variable in your data. For example, in a scatterplot, aesthetics include things like the size, shape, color, or location (x-axis, y-axis) of your data points.

Creating a Plot in ggplot2
here is an example code: ggplot(data = penguins) + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

 

Alternative way to write the above code:

Changing all the colors to a specific color

to change all the data points to a specific color, the color code shuold be added outside of the aes() function.

ggplot(data = penguin) + geom_point(x,y, …)
ggplot(data=penguins, mapping=aes x = flipper_length_mm, y = body_mass_g)) +geom_point()

Let us unpack the code:

      1. ggplot(): use this function to initalize the plot. The first argument is the “data” as data =…
      2.  “+” adds a layer to the plot
      3. geom_point():  next is the geom function. There are many geom function. In this case, the geom function being used is the geom_pont(). Geom points plot scatter plots
      4. (mapping = aes(x = {variable1}, y = {variable2})): Each geom function in ggplot2 takes a mapping argument. The x and y arguments of the aes() function. This is where the variables to map are added.

Alpha esthetics. Thsi controls the transparency of data points

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top