How to Make Common Graphs with Lattice in R
How to Use Layers in ggplot2 in R
How to Step Through Debugging an R Function

How to Make Scatterplot and Line Charts in R with ggplot2

If you have downloaded and imported ggplot2 for use in your R installation, you can use it to plot your data. To create a scatterplot, you use the geom_point() function. To create a line chart, you use the geom_line() function.

How to make a scatterplot

A scatterplot creates points (or sometimes bubbles or other symbols) on your chart. Each point corresponds to an observation in your data.

You’ve probably seen or created this type of graphic a million times, so you already know that scatterplots use the Cartesian coordinate system, where one variable is mapped to the x-axis and a second variable is mapped to the y-axis.

In exactly the same way, in ggplot2 you create a mapping between x-axis and y-axis variables. So, to create a plot of the quakes data, you map quakes$long to the x-axis and quakes$lat to the y-axis:

image0.jpg
> ggplot(quakes, aes(x=long, y=lat)) + geom_point()

How to create line charts

You use this function in a very similar way to geom_point(), with the difference that geom_line() draws a line between consecutive points in your data.

This type of chart is useful for time series data in data frames, such as the population data in the built-in dataset longley. To create a line chart of unemployment figures, you use the following:

image1.jpg
> ggplot(longley, aes(x=Year, y=Unemployed)) + geom_line()

You can use either geom_line() or geom_path() to create a line drawing in ggplot2. The difference is that geom_line() first orders the observations according to x-value, whereas geom_path() draws the observations in the order found in the data.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Manipulate Files in R
How to Search for Individual Words in R
How to Read Errors and Warnings in R
How to Extend Text Functionality with Stringr in R
How to Debug Your Code in R
Advertisement

Inside Dummies.com