如何在 R 中将平均值添加到直方图中?

发布于 2024-11-18 02:21:34 字数 120 浏览 4 评论 0原文

我想绘制一个带有平均值的直方图(例如,我们可以用蓝色粗线标记它)。

我尝试使用 plot 命令来完成此操作,但即使我设置了参数 add=TRUE 它不起作用。

I would like to plot a histogram with mean (average) value on it (e.g. we could mark it with a blue and bold line).

I tried to do it using plot command, but even if I set the parameter add=TRUE
it didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房敞 2024-11-25 02:21:34

您可以使用 abline() 向绘图添加线条:

x <- rnorm(100)
mx <- mean(x)
hist(x)
abline(v = mx, col = "blue", lwd = 2)

另请参阅 ?par 了解图形参数(例如 col>lwd)。


一般来说,您还可以使用lines()绘制线条:

x <- rnorm(100, mean = 10)
mx <- mean(x)
hist(x)
lines(c(mx,mx), c(0,15), col = "red", lwd = 2)
lines(c(10, 11.5), c(0, 10), col = "steelblue", lwd = 3, lty = 22)
text(mx, 18 , round(mx, 2))
text(mx, 12 , "big", cex = 5)

其中text()用于添加文本。参数cex描述了“字符扩展因子”。

另外,请查看 Quick-R 了解以下内容的概述使用 R 进行基本绘图。

You can use abline() to add lines to a plot:

x <- rnorm(100)
mx <- mean(x)
hist(x)
abline(v = mx, col = "blue", lwd = 2)

Have also a look at ?par for graphic parameters (like col and lwd).


In general, you can also plot lines using lines():

x <- rnorm(100, mean = 10)
mx <- mean(x)
hist(x)
lines(c(mx,mx), c(0,15), col = "red", lwd = 2)
lines(c(10, 11.5), c(0, 10), col = "steelblue", lwd = 3, lty = 22)
text(mx, 18 , round(mx, 2))
text(mx, 12 , "big", cex = 5)

where text() is used for adding text. The argument cex describes the "character expansion factor".

Also, have a look at Quick-R for an overview of basic plotting with R.

白云悠悠 2024-11-25 02:21:34
hist(data)
abline(v=mean(data),col="blue")
hist(data)
abline(v=mean(data),col="blue")
忆悲凉 2024-11-25 02:21:34

如果您有包含更多列的数据框,那么使用 ggplot2 包是我的首选:

ggplot (data, aes (x = colname)) + geom_vline(xintercept=mean(data$colname), color="red")

Colname 是 data.frame 中您想要为其绘制直方图和平均值的列。

If you have data frames with more columns using of ggplot2 package is my preferred option:

ggplot (data, aes (x = colname)) + geom_vline(xintercept=mean(data$colname), color="red")

Colname is column in your data.frame for which you would like to plot the histogram and mean.

醉殇 2024-11-25 02:21:34

我遇到了一个问题,即平均线没有出现,并且我没有收到任何错误来帮助我找出原因。我意识到没有发生任何事情,因为我缺少一些数据,因此平均值被计算为 NA。将 na.rm = T 添加到mean() arg 中得到了一个实数,并且出现了平均线。这是一个小疏忽和一个简单的修复,几乎不值得一写,但无论如何我都会发布它,以防它可以减轻某人的痛苦。

hist(data$Defect.rate, 
 xlim = c(0, 1),
 col = "light blue")

abline(v = mean(data$Defect.rate, na.rm = T),
            col = "red",
            lwd = 2)

I ran into a problem where the mean line wasn't appearing, and I wasn't getting any error to help me figure out why. I realized that nothing was happening because I had some missing data, so the mean was calculated as NA. Adding na.rm = T to the mean() arg got me a real number, and the mean line appeared. It's a small oversight and a simple fix hardly worth writing about, but I'm posting it anyway in case it might save someone some grief.

hist(data$Defect.rate, 
 xlim = c(0, 1),
 col = "light blue")

abline(v = mean(data$Defect.rate, na.rm = T),
            col = "red",
            lwd = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文