![]() ![]() Let's review each of these changes: Moving from tree_1 to Orange We moved the color parameter inside of the aes() parentheses.Instead of specifying color = 'red', we specified color = Tree.The dataset changed from tree_1 (our filtered dataset) to the complete Orange dataset.This line graph is quite different from the one we produced above, but we only made a few minor modifications to the code! Did you catch the 3 changes? They were: Geom_line (aes (x = age, y = circumference, color = Tree )) 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 )) Tree_1 <- filter (Orange, Tree = 1 ) # Graph the data Library (tidyverse ) # Filter the data we need 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 ![]() A line graph is a type of graph that displays information as a series of data points connected by straight line segments. ![]() Right now, we're talking about line graphs. Whether it's scatter plots, bar graphs, or line graphs (the subject of this post!), common graph types make things easy for your audience, which means you can more easily share your message. In fact, in most cases, simplicity is key to making your audience understand your data. 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. ![]()
0 Comments
Leave a Reply. |