

Compare the ggplot code below to the code we just executed above. Now, let's try something a little different. You can use most color names you can think of, or you can use specific hex colors codes to get more granular. Experiment a bit with different colors to see how this works on your machine. You'll note that this geom_line call is identical to the one before, except that we've added the modifier color = 'red' to to end of the line. Geom_line(aes(x = age, y = circumference), color = 'red') In this second layer, I told ggplot to use age as the x-axis variable and circumference as the y-axis variable.Īnd that's it, we have our line graph! Changing line color in ggplot + geom_lineĮxpanding on this example, let's now experiment a bit with colors. In ggplot, you use the + symbol to add new layers to an existing graph. Next, I added my geom_line call to the base ggplot graph in order to create this line. In this case, I passed tree_1 to ggplot, indicating that we'll be using the tree_1 data for this particular ggplot graph. It's essentially a blank canvas on which we'll add our data and graphics. Let's review this in more detail:įirst, I call ggplot, which creates a new ggplot graph. Once I had filtered out the dataset I was interested in, I then used ggplot + geom_line() to create the graph. If you're not familiar with dplyr's filter function, it's my preferred way of subsetting a dataset in R, and I recently wrote an in-depth guide to dplyr filter if you'd like to learn more! I used dplyr to filter the dataset to only that first tree. For this simple graph, I chose to only graph the size of the first tree. Here we are starting with the simplest possible line graph using geom_line. Geom_line(aes(x = age, y = circumference)) Simple example of ggplot + geom_line() library(tidyverse)

There are 7 observations for each Tree, and there are 5 Trees, for a total of 35 observations in all. The dataset contains 3 columns: Tree, age, and cimcumference. Let’s take a look at this dataset to see what it looks like: This dataset contains information on the age and circumference of 5 different orange trees, letting us see how these trees grow over time. Throughout this post, we’ll be using the Orange dataset that’s built into R. It’s the tool I use to create nearly every graph I make these days, and I think you should use it too! Investigating our dataset This makes ggplot a powerful and flexible tool for creating all kinds of graphs in R. When components are unspecified, ggplot uses sensible defaults. You can then modify each of those components in a way that’s both flexible and user-friendly. Ggplot takes each component of a graph–axes, scales, colors, objects, etc–and allows you to build graphs up sequentially one component at a time. Ggplot is a package for creating graphs in R, but it’s also a method of thinking about and decomposing complex graphs into logical subunits. Introduction to ggplotīefore we dig into creating line graphs with the ggplot geom_line function, I want to briefly touch on ggplot and why I think it’s the best choice for plotting graphs in R. There are many different ways to use R to plot line graphs, but the one I prefer is the ggplot geom_line function. Line graph of average monthly temperatures for four major cities The price of Netflix stock (NFLX) displayed as a line graph
PLOT TWO DATASETS ON SAME GRAPH R GGPLOT SERIES
In fact, one of the most powerful ways to communicate the relationship between two variables is the simple line graph.Ī line graph is a type of graph that displays information as a series of data points connected by straight line segments. But if you’re trying to convey information, flashy isn’t always the way to go. When it comes to data visualization, it can be fun to think of all the flashy and exciting ways to display a dataset.
