Ggplot2 add vertical line

Ggplot2 add vertical line DEFAULT

ggplot2 add straight lines to a plot : horizontal, vertical and regression lines


This tutorial describes how to add one or more straight lines to a graph generated using R software and ggplot2 package.

The R functions below can be used :

  • geom_hline() for horizontal lines
  • geom_abline() for regression lines
  • geom_vline() for vertical lines
  • geom_segment() to add segments

A simplified format of the function geom_hline() is :

It draws a horizontal line on the current plot at the specified ‘y’ coordinates :

add straight lines to a plot using R statistical software and ggplot2add straight lines to a plot using R statistical software and ggplot2add straight lines to a plot using R statistical software and ggplot2

Read more on line types here : Line types in R

A simplified format of the function geom_vline() is :

It draws a vertical line on the current plot at the specified ‘x’ coordinates :

add straight lines to a plot using R statistical software and ggplot2add straight lines to a plot using R statistical software and ggplot2

A simplified format of the function geom_abline() is :

The function lm() is used to fit linear models.

add straight lines to a plot using R statistical software and ggplot2add straight lines to a plot using R statistical software and ggplot2

Note that, the function stat_smooth() can be used for fitting smooth models to data.

add straight lines to a plot using R statistical software and ggplot2

A simplified format of the function geom_segment() is :

It’s possible to use it as follow :

add straight lines to a plot using R statistical software and ggplot2add straight lines to a plot using R statistical software and ggplot2

Note that, you can add an arrow at the end of the segment. grid package is required

add straight lines to a plot using R statistical software and ggplot2

This analysis has been performed using R software (ver. ) and ggplot2 (ver. )


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen times
Sours: http://www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizontal-vertical-and-regression-lines

Draw Vertical Line to X-Axis of Class Date in ggplot2 Plot in R (Example)

 

In this tutorial you&#;ll learn how to add a geom_vline to a ggplot2 plot with dates as x-axis in the R programming language.

The tutorial will contain one examples for drawing vertical lines. More precisely, the content of the post is structured as follows:

Let&#;s dive right into the examples.

 

Creating Exemplifying Data

In the example of this R programming tutorial, we&#;ll use the following R data frame as basement:

set.seed()# Create data frame data <- data.frame(my_dates = seq(as.Date(0, origin=""), length =72, by="1 month"), my_values = sample(, 72, replace =TRUE)) head(data)# Print data frame# my_dates my_values# 1 8# 2 46# 3 79# 4 53# 5 53# 6 88

set.seed() # Create data frame data <- data.frame(my_dates = seq(as.Date(0, origin=""), length = 72, by = "1 month"), my_values = sample(, 72, replace = TRUE)) head(data) # Print data frame # my_dates my_values # 1 8 # 2 46 # 3 79 # 4 53 # 5 53 # 6 88

Our example data contains 72 rows and two columns. The variable my_dates has the class Date and the column my_values consists of random numeric values.

If we want to draw graphics with the ggplot2 package, we also need to install and load ggplot2:

install.packages("ggplot2")# Install ggplot2 library("ggplot2")# Load ggplot2

install.packages("ggplot2") # Install ggplot2 library("ggplot2") # Load ggplot2

Now, we can draw a basic ggplot2 line plot without any vertical lines as follows:

ggp <- ggplot(data, aes(my_dates, my_values))+# Create basic ggplot geom_line() ggp # Draw basic ggplot

ggp <- ggplot(data, aes(my_dates, my_values)) + # Create basic ggplot geom_line() ggp # Draw basic ggplot

 

ggplot2 line plot in r

Figure 1: ggplot2 Line Plot with Default Specifications.

 

Figure 1 illustrates the output of the previous R code &#; A basic line plot showing values corresponding to dates on the x-axis.

 

Example: Adding geom_vline at Specific Dates

If we want to add vertical lines at some specific dates to our plot, we first need to specify the dates and their positions in our data:

dates_vline <-as.Date(c("", ""))# Define positions of vline dates_vline <- which(data$my_dates %in% dates_vline)

dates_vline <- as.Date(c("", "")) # Define positions of vline dates_vline <- which(data$my_dates %in% dates_vline)

Now, we can add vertical lines with the geom_vline function to our plot:

ggp +# Draw vlines to plot geom_vline(xintercept =as.numeric(data$my_dates[dates_vline]), col ="red", lwd =2)

ggp + # Draw vlines to plot geom_vline(xintercept = as.numeric(data$my_dates[dates_vline]), col = "red", lwd = 2)

 

ggplot2 graphics with vertical lines at date position

Figure 2: ggplot2 Line Plot with Vertical Lines at Date Positions.

 

Figure 2 shows the result: A ggplot2 line plot with two vertical lines.

 

Video, Further Resources &#; Summary

If you need more explanations on the R code of this tutorial, you might watch the following video of my YouTube channel. I&#;m explaining the R codes of this article in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might want to have a look at the other articles of my homepage. Some tutorials can be found below.

 

Summary: In this tutorial, I showed how to get a vertical geom_vline to an x-axis of class date in R programming. Don&#;t hesitate to let me know in the comments, in case you have further questions and/or comments.

Sours: https://statisticsglobe.com/draw-vertical-line-to-x-axis-of-class-date-in-ggplot2-plot-in-r
  1. Woodys pet deli woodbury
  2. Bf5 best sniper rifle
  3. Delta bath accessories
  4. Sims 3 map editor
  5. Drilling into basement wall

Add Vertical & Horizontal Line to gglot2 Plot in R (4 Examples)

 

In this tutorial you’ll learn how to draw vertical and horizontal lines to a ggplot2 graph in R programming.

The article will consist of four examples for the drawing of lines. More precisely, the tutorial consists of these contents:

Let’s get started:

 

Example Data, Packages & Default Graphic

I use the following data as basement for this R tutorial:

data <- data.frame(x =, # Create example data y =) data # Print example data

data <- data.frame(x = , # Create example data y = ) data # Print example data

 

table 1 data frame add vertical and horizontal line gglot2 r

 

Table 1 shows the structure of the example data: It consists of eight rows and two columns.

We also have to install and load the ggplot2 package, to be able to use the corresponding functions and commands.

install.packages("ggplot2")# Install ggplot2 package library("ggplot2")# Load ggplot2 package

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package

Now, we can plot the data without any lines as follows:

ggp <- ggplot(data, aes(x, y))+# Create ggplot2 plot geom_point() ggp # Draw ggplot2 plot

ggp <- ggplot(data, aes(x, y)) + # Create ggplot2 plot geom_point() ggp # Draw ggplot2 plot

 

r graph figure 1 add vertical and horizontal line gglot2 r

 

In Figure 1 it is shown that we have drawn a ggplot2 scatterplot by executing the previous R code.

 

Example 1: Add Vertical Line to ggplot2 Plot Using geom_vline() Function

In this example, I’ll explain how to print a vertical line to a ggplot2 plot.

For this, we can use the geom_vline function and the xintercept argument:

ggp +# Draw line to plot geom_vline(xintercept =)

ggp + # Draw line to plot geom_vline(xintercept = )

 

r graph figure 2 add vertical and horizontal line gglot2 r

 

In Figure 2 it is shown that we have created a scatterplot with a vertical line at the x-axis position

 

Example 2: Add Horizontal Line to ggplot2 Plot Using geom_hline() Function

Example 2 explains how to draw a horizontal line using the geom_hline function and the yintercept argument.

ggp +# Draw line to plot geom_hline(yintercept =)

ggp + # Draw line to plot geom_hline(yintercept = )

 

r graph figure 3 add vertical and horizontal line gglot2 r

 

In Figure 3 it is shown that we have added a vertical line to our plot using the previous R code.

 

Example 3: Add Vertical & Horizontal Lines to ggplot2 Plot

In Example 3, I’ll show how to add vertical and horizontal lines to the same ggplot2 graph.

ggp +# Draw line to plot geom_vline(xintercept =)+ geom_hline(yintercept =)

ggp + # Draw line to plot geom_vline(xintercept = ) + geom_hline(yintercept = )

 

r graph figure 4 add vertical and horizontal line gglot2 r

 

As shown in Figure 4, we have plotted two lines to our plot.

 

Example 4: Add Multiple Vertical Lines to ggplot2 Plot Using seq() Function

This example shows how to add a sequence of multiple lines using the seq function.

ggp +# Draw line to plot geom_vline(xintercept = seq(1, 5, by=))

ggp + # Draw line to plot geom_vline(xintercept = seq(1, 5, by = ))

 

r graph figure 5 add vertical and horizontal line gglot2 r

 

By running the previous syntax we have plotted Figure 5, i.e. a scatterplot with a sequence of vertical lines.

 

Video & Further Resources

Would you like to know more about ggplot2 graphics? Then you could have a look at the following video instruction of my YouTube channel. I’m explaining the contents of this article in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may want to have a look at some of the related articles on my homepage. A selection of articles about graphics in R is listed below.

 

This article has illustrated how to add a line to a ggplot2 graphic in the R programming language. Please let me know in the comments section, if you have any further questions or comments.

Sours: https://statisticsglobe.com/add-vertical-and-horizontal-line-to-gglot2-plot-in-r

Adding vertical line in plot ggplot

Regarding the first question:

This answer is assuming that the value of Y you desire is specifically within your data set. First, let's create a reproducible example as I cannot access your data set:

initial plot

What you need to do is to use the column in your data frame to search for the specific value. Essentially you will need to use

Using the data I generated above, here's an example. Since my data frame does not likely contain the exact value , I will use the function to search for it:

Now we know where all the values of 2 are. Since this graph is decreasing, we will reverse it, then the value closest to 2 will be at the beginning:

And we can use that value to specify where the x intercept should be:

plot with vertical line

Hope that helps!

answered Oct 27 '13 at

ZNKZNK

2, gold badge silver badges bronze badges

Sours: https://stackoverflow.com/questions//adding-vertical-line-in-plot-ggplot

Add line ggplot2 vertical

Add Vertical and Horizontal Lines to ggplot2 Plot in R

In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language.

Adding Vertical Line To R Plot using geom_vline()

For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position.

Syntax: geom_vline(xintercept)

Parameter: here xintercept is used control the X position of line.

Return: Vertical line on R plot.



Example: To add the vertical line on the plot, we simply add geom_vline() function to ggplot2() function and pass the xintercept, which basically has a location on X-axis, where we actually want to create a vertical line. Here we set to the xintercept.

R

 

 

Output:

with verticle line

Scatter plot with vertical line at X=

Adding Horizontal Line To R Plot using geom_hline()

And for adding Horizontal lines to the R plot, we will use geom_hline() function:

Syntax: geom_hline(yintercept)

Parameter: here yintercept is used control the y position of line.

Return: Horizontal line on R plot.

Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location onthe Y axis, where we actually want to create a vertical line. Here we set 20 to the xintercept.



R

 

 

Output:

with horizontal line

Scatter plot with Horizontal line at Y=20

Adding both vertical and horizontal lines to R plot

To add both lines, we add both geom_vline() and geom_hline() function to ggplot() function and set the values of xintercept and yintercept. 

Example: Here we will draw the vertical line at X= and the vertical line at Y=20.

R

 

 

Output:

with both lines

Scatter plot with both lines at X= and Y=20

 

Adding multiple vertical and horizontal lines to R plot using seq() function

We will use seq() function to represent the xintercept and yintercept and all other settings will remain the same as example 3.

Syntax: seq(from, to, by)

Parameters:

  • from : it is used to represent starting length.
  • to:  it is used to represent ending length.
  • by: it represents increment of the system.

Return: well-formed sequence.

Example: Here we&#;re drawing vertical lines from to with a difference of and horizontal lines from 10 to 50 with a difference of 5.

R

 

 

Output:

Scatter plot with multiple verticle and horizontal lines

Scatter plot with multiple vertical and horizontal lines




Sours: https://www.geeksforgeeks.org/add-vertical-and-horizontal-lines-to-ggplot2-plot-in-r/
LIFE EXCEL HACK - Adding Vertical Line in Graph #1

These geoms add reference lines (sometimes called rules) to a plot, either horizontal, vertical, or diagonal (specified by slope and intercept). These are useful for annotating plots.

Arguments

mapping

Set of aesthetic mappings created by or .

data

The data to be displayed in this layer. There are three options:

If , the default, the data is inherited from the plot data as specified in the call to .

A , or other object, will override the plot data. All objects will be fortified to produce a data frame. See for which variables will be created.

A will be called with a single argument, the plot data. The return value must be a , and will be used as the layer data. A can be created from a (e.g. ).

Other arguments passed on to . These are often aesthetics, used to set an aesthetic to a fixed value, like or . They may also be parameters to the paired geom/stat.

na.rm

If , the default, missing values are removed with a warning. If , missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? , the default, includes if any aesthetics are mapped. never includes, and always includes. It can also be a named logical vector to finely select the aesthetics to display.

xintercept, yintercept, slope, intercept

Parameters that control the position of the line. If these are set, , and are overridden.

Details

These geoms act slightly differently from other geoms. You can supply the parameters in two ways: either as arguments to the layer function, or via aesthetics. If you use arguments, e.g. , then behind the scenes the geom makes a new data frame containing just the data you've supplied. That means that the lines will be the same in all facets; if you want them to vary across facets, construct the data frame yourself and use aesthetics.

Unlike most other geoms, these geoms do not inherit aesthetics from the plot default, because they do not understand x and y aesthetics which are commonly set in the plot. They also do not affect the x and y scales.

Aesthetics

These geoms are drawn using with so support the same aesthetics: , , and . They also each have aesthetics that control the position of the line:

  • :

  • :

  • : and

See also

See for a more general approach to adding straight line segments to a plot.

Examples

Sours: https://ggplot2.tidyverse.org/reference/geom_abline.html

You will also be interested:

It spread without meeting the slightest resistance, absorbing the audience without a trace. Olya seemed to feel all the light movements of Nastya's tongue, even if she did not see them. I felt how the tongue slowly wrapped around the head, twitches barely noticeably, caressing the very edge, going down, turning into.

A large, slightly rough, scapula, sliding all over the penis to the base, rubbing from side to side, bringing unprecedented pleasure. Nastya released Victor's phallus from her mouth and looked at him.



1222 1223 1224 1225 1226