将计算出的平均值添加到 R 图中的垂直线
Add calculated mean value to vertical line in plot in R
我创建了一个密度图,其中一条垂直线反映了平均值 - 我想在图中包含计算出的平均值,但不知道如何
(例如平均值 1.2 应该出现在图表中)。
beta_budget[2]是包含价格不同数字的列。
windows()
plot(density(beta_budget[,2]), xlim= c(-0.1,15), type ="l", xlab = "Beta Coefficients", main = "Preis", col = "black")
abline(v=mean(beta_budget[,2]), col="blue")
legend("topright", legend = c("Price", "Mean"), col = c("black", "blue"), lty=1, cex=0.8)
我用文本命令试过了,但没用...
感谢您的指教!
大致如下:
数据:
set.seed(123)
df <- data.frame(
v1 = rnorm(1000)
)
用密度线绘制直方图:
hist(df$v1, freq = F, main = "")
lines(density(df$v1, kernel = "cosine", bw = 0.5))
abline(v = mean(df$v1), col = "blue", lty = 3, lwd = 2)
将均值作为文本元素包括在内:
text(mean(df$v1), # position of text on x-axis
max(density(df$v1)[[2]]), # position of text on y-axis
mean(df$v1), # text to be plotted
pos = 4, srt = 270, cex = 0.8, col = "blue") # some graphical parameters
我创建了一个密度图,其中一条垂直线反映了平均值 - 我想在图中包含计算出的平均值,但不知道如何 (例如平均值 1.2 应该出现在图表中)。
beta_budget[2]是包含价格不同数字的列。
windows()
plot(density(beta_budget[,2]), xlim= c(-0.1,15), type ="l", xlab = "Beta Coefficients", main = "Preis", col = "black")
abline(v=mean(beta_budget[,2]), col="blue")
legend("topright", legend = c("Price", "Mean"), col = c("black", "blue"), lty=1, cex=0.8)
我用文本命令试过了,但没用...
感谢您的指教!
大致如下:
数据:
set.seed(123)
df <- data.frame(
v1 = rnorm(1000)
)
用密度线绘制直方图:
hist(df$v1, freq = F, main = "")
lines(density(df$v1, kernel = "cosine", bw = 0.5))
abline(v = mean(df$v1), col = "blue", lty = 3, lwd = 2)
将均值作为文本元素包括在内:
text(mean(df$v1), # position of text on x-axis
max(density(df$v1)[[2]]), # position of text on y-axis
mean(df$v1), # text to be plotted
pos = 4, srt = 270, cex = 0.8, col = "blue") # some graphical parameters