首页 > 解决方案 > 我可以使用 ggplot2 在特定数量的值范围内绘制一条水平线吗?

问题描述

我有数据(来自excel),y轴作为范围(也在excel中计算),x轴作为单元格计数,我想在范围内的特定值处绘制一条水平线,如参考线。我尝试使用geom_hline(yintercept = 450),但我确信它非常幼稚,并且对于范围内的数字不起作用。我想知道是否有更好的建议:)

plot.new()
library(ggplot2)
d <- read.delim("C:/Users/35389/Desktop/R.txt", sep = "\t")
head(d)

d <- cbind(row.names(d), data.frame(d), row.names=NULL)
d
g <- ggplot(d, aes(d$CTRL,d$Bin.range))+ geom_col() 
g + geom_hline(yintercept = 450)

在此处输入图像描述

标签: rggplot2range

解决方案


首先,看看我的评论。

其次,我建议您这样做:不要在 Excel 上计算这些范围。让 ggplot 为您完成。

说,你的数据是这样的:

df <- data.frame(x = runif(100, 0, 500))
head(df)
#>          x
#>1 322.76123
#>2  57.46708
#>3 223.31943
#>4 498.91870
#>5 155.05416
#>6 107.27830

然后你可以制作这样的情节:

library(ggplot2)

ggplot(df) +
 geom_histogram(aes(x = x), 
                boundary = 0,
                binwidth = 50,
                fill = "steelblue",
                colour = "white") +
 geom_vline(xintercept = 450, colour = "red", linetype = 2, size = 1) +
 coord_flip()

在此处输入图像描述


推荐阅读