Introduction to ggplot in R


This entry is part 1 of 3 in the series R ggplot

The ggplot2 package lets you make high-quality, customizable plots of your data. ggplot2 is based on the grammar of graphics, which is a system for describing and building data visualizations. The idea behind the grammar of graphics is that you can build any plot from the same basic components. The grammar of ggplot2 is provides the rules of how to write code to give us a beautiful graph as the output. What are those components or rules?

  1. a dataset
  2. a set of geoms
  3. A set of aesthetic attributes

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 and so on. To create the bars on the chart, use a geom function: geom_bar (). To create a trend line, use a geom function: geom_smooth (). To create the scatter plot, use a geom function: geom_point ().

An aesthetic is a visual property of an object in your plot. An aesthetic is 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. You could use an aesthetics function: col = ifelse (x<2, 'blue', 'yellow'). To label the axes, use an aesthetics function: aes (x = Average price (USD), y = Product)

To create your own plot using code, follow these three steps:

  1. Start with the ggplot() function and choose a dataset to work with.
  2. Add a geom_ function to display your data.
  3. Map the variables you want to plot in the arguments of the aes() function.

More Information

The ggplot2 website.

Series NavigationAnnotating ggplot in R >>

Leave a comment

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