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 ()
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?
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.
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, ...)
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With