在图ggplot中添加垂直线 [英] Adding vertical line in plot ggplot

查看:1574
本文介绍了在图ggplot中添加垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码绘制图表:

  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 - 如何在在y的特定值处相交的图(例如2)?

2 - 如何让y轴从0.5开始而不是1?

解决方案

您可以使用 geom_vline()添加垂直线。在你的情况下:

  + geom_vline(xintercept = 2)

如果您想在y轴上看到数字0.5,则添加 scale_y_continuous()并设置 limits = breaks =

  + scale_y_continuous(breaks = c(0.5,1,2,3,4,5),limits = c(0.5,6))


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?

解决方案

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))

这篇关于在图ggplot中添加垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆