Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding vertical line in plot ggplot

Tags:

r

ggplot2

I am plotting a graph using the following piece of code:

library (ggplot2)  png (filename = "graph.png") stats <- read.table("processed-r.dat", header=T, sep=",") attach (stats) stats <- stats[order(best), ] sp <- stats$A / stats$B index <- seq (1, sum (sp >= 1.0)) stats <- data.frame (x=index, y=sp[sp>=1.0]) ggplot (data=stats, aes (x=x, y=y, group=1)) + geom_line() dev.off () 

enter image description here

1 - How one can add a vertical line in the plot which intersects at a particular value of y (for example 2)?

2 - How one can make the y-axis start at 0.5 instead of 1?

like image 667
Shahzad Avatar asked Oct 27 '13 18:10

Shahzad


People also ask

How do I add a vertical line in ggplot2?

To create a vertical line using ggplot2, we can use geom_vline function of ggplot2 package and if we want to have a wide vertical line with different color then lwd and colour argument will be used. The lwd argument will increase the width of the line and obviously colour argument will change the color.

How do I graph a vertical line in R?

The R function abline() can be used to add vertical, horizontal or regression lines to a graph. A simplified format of the abline() function is : abline(a=NULL, b=NULL, h=NULL, v=NULL, ...)

How do you add a line to a plot in R?

Use the lines() Function to Add a Line to a Plot in R Once the plot is drawn, we can call the lines() function and pass the coordinate vectors as needed to add lines to the plot.

What is the function of Abline H 5?

The h= and v= forms draw horizontal and vertical lines at the specified coordinates. The coef form specifies the line by a vector containing the slope and intercept. reg is a regression object with a coef method.


1 Answers

You can add vertical line with geom_vline(). In your case:

+ geom_vline(xintercept=2) 

If you want to see also number 0.5 on your y axis, add scale_y_continuous() and set limits= and breaks=

+ scale_y_continuous(breaks=c(0.5,1,2,3,4,5),limits=c(0.5,6)) 
like image 79
Didzis Elferts Avatar answered Sep 30 '22 08:09

Didzis Elferts