How To Color Between Two Line Processing
Add Color to Region Between Two Lines in ggplot2 Line Plot in R (2 Examples)
This commodity explains how to highlight the region betwixt ii ggplot2 lines with color in R.
The post consists of these content blocks:
So at present the function you have been waiting for – the examples!
Instance Information, Packages & Default Graphic
The following information is used equally a footing for this R tutorial:
information <- data. frame (x = 1 : v, # Create example data frame y1 = c( ane, iii, 1, 4, 4 ), y2 = c( 5, 4, 4, 5, seven ) ) data # Print example data frame
data <- data.frame(x = 1:v, # Create example data frame y1 = c(one, 3, one, 4, 4), y2 = c(5, 4, iv, five, 7)) data # Print example data frame
Table 1 shows the structure of the example data – It is constituted of five rows and three columns.
In this tutorial, nosotros'll also need to install and load the ggplot2 packet.
install. packages ( "ggplot2" ) # Install ggplot2 package library( "ggplot2" ) # Load ggplot2 package
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package
At present, we can depict our data as shown below:
ggp <- ggplot(information, aes(x = 10) ) + # Create default ggplot2 line plot geom_line(aes(y = y1) ) + geom_line(aes(y = y2) ) ggp # Draw default ggplot2 line plot
ggp <- ggplot(data, aes(10 = x)) + # Create default ggplot2 line plot geom_line(aes(y = y1)) + geom_line(aes(y = y2)) ggp # Describe default ggplot2 line plot
Every bit illustrated in Figure ane, we have plotted a ggplot2 line plot by running the previous code.
Example 1: Add Color Between 2 Lines in ggplot2 Line Plot
This case explains how to fill up the area between two ggplot2 lines.
For this task, we can employ the R programming syntax beneath:
ggp + # Add color between lines geom_ribbon(aes(x = x, ymin = y1, ymax = y2), fill = "#1b98e0" )
ggp + # Add together color between lines geom_ribbon(aes(x = 10, ymin = y1, ymax = y2), fill = "#1b98e0")
Equally shown in Effigy 2, we have managed to create a ggplot2 line graph with blue color between the lines with the previous R programming code.
Example 2: Add Shading Between Two Lines in ggplot2 Line Plot
In Example 2, I'll evidence how to draw a transparent greyness shade between the lines of a ggplot2 graphic.
ggp + # Add shading between lines geom_ribbon(aes(ten = x, ymin = y1, ymax = y2), fill = "grayness", blastoff = 0.4 )
ggp + # Add together shading between lines geom_ribbon(aes(x = ten, ymin = y1, ymax = y2), fill = "greyness", alpha = 0.4)
After running the previous R code the shaded region between our two lines visualized in Figure 3 has been drawn.
Video & Farther Resource
Exercise you need more explanations on the R codes of the present article? Then I recommend watching the post-obit video on my YouTube channel. In the video, I'm explaining the R code of this article.
The YouTube video volition exist added soon.
Furthermore, you lot may take a look at some of the related R tutorials on https://statisticsglobe.com/. I have published several tutorials on topics such as text elements, colors, ggplot2, and graphics in R.
- Add Table to ggplot2 Plot
- Change Fill and Border Color of ggplot2 Plot
- Add Text to ggplot2 Plot in R
- Add Bold & Italic Text to ggplot2 Plot in R
- Graphics Overview in R
- Introduction to R Programming
Summary: In this tutorial, I take shown how to depict color between ii lines in a ggplot2 plot in R programming. Please let me know in the comments below, if you have any additional questions. Furthermore, please subscribe to my email newsletter in lodge to receive regular updates on new articles.
Source: https://statisticsglobe.com/add-color-region-between-two-lines-ggplot2-line-plot-r
Posted by: brooksbuslow.blogspot.com
0 Response to "How To Color Between Two Line Processing"
Post a Comment