Manual Scale in ggplot2Megan KressOctober 5, 2016
Manual ScaleIf you are want to use ggplot2 to plot two different data frames, you probably want the two data frames to be represented by different colors. Using the geom_point function, you can easily assign different colors to the different sets of data, but creating a legend for two different data frames in one ggplot is more less intuitive. The code below explains how to take two data frames and plot them together with a legend that explains the colors for each of the data sets. This code uses teh scale_colour_manual function from ggplot2.
R Code
Preparing Data for Ggplot2September 14th, 2016Description: Ggplot2 is an R package can quickly and efficiently plot a variety of graphs and outputs; it’s easy to use with a little practice. Compared to commands such as plot(), Ggplot2 commands differ in that they do not read formats of data other than data frames. However, there are many ways to transform data into a suitable format. This tutorial will cover using the command data.frame() and a bonus tip on aggregate() to prepare data for use in Ggplot2. This R tip is modified from: http://stats.stackexchange.com/questions/8225/how-to-summarize-data-by-group-in-r http://stackoverflow.com/questions/35681614/error-when-making-a-simple-boxplot-with-ggplot2 Let’s say we were interested in the R dataset “rivers”, which includes the lengths in miles of 141 rivers in North America, and we wanted to make a boxplot to look at the distribution of lengths. First, let’s load the data, along with Ggplot2, and see what the data looks like.
Since Ggplot2 has a command geom_boxplot(), let’s see what happens when we try to use this command with the data as it stands.
If you look at the R documentation on the dataset rivers, you’ll notice that the dataset is in the form of a vector, which is not compatible with Ggplot2. To fix this, we’ll use a command which comes with the R package, data.frame().
The data are the same, but you’ll notice the formatting is slightly different, indicating that it is now in the data frame format. Let’s try generating a boxplot again, with the data in the new format.
Success! Data.frame() is a convenient way to prepare any data that you have calculated or modified in R that is currently not stored as a data frame to be compatible for use with Ggplot2. Let’s move on to the bonus tip about the command aggregate(). We’ll use another data set that comes with R, “C02,” which contains information on a greenhouse study on ambient carbon dioxide uptake in plants from two different origins (6 from Quebec and 6 from Mississippi). Half of the plants from both origins were chilled overnight prior to the experiment. The data set includes information for each plant’s origin, treatment (chilled or not chilled), ambient carbon dioxide, and carbon dioxide uptake rates.
For simplicity, let’s say we were interested in the differences in uptake at different ambient concentrations (regardless of origin or treatment). To calculate the mean uptake for each concentration, we can use the function aggregate(), which can generate subsets of data and compute summary statistics.
Let’s try to plot the data in Ggplot2…
It worked! The command aggregate() returns the subset of your data already in a dataframe, meaning no further adjustment is necessary to use it in Ggplot2. Data.frame() is a great way to transform any data into a data.frame for use in Ggplot2, while aggregate() can help you pull information from your dataset in a format conveniently ready-to-use in Ggplot2. |
Spatial Ecology @ MSUClick on "Category" below to search for R code compiled by the Zarnetske Spatial & Community Ecology Lab and students in MSU's Spatial Ecology graduate course (FOR870/FW870) Category
All
Archive
October 2016
|