【发布时间】:2018-09-06 19:15:49
【问题描述】:
我想在我的直方图中放置图例而不用重叠线。 我将展示我的 R 代码和直方图。
#Installing 'readxl' to load a xlsx file
install.packages("readxl")
library(readxl)
#Loading xlsx file
value <- read_excel("C:/Users/user/Desktop/value.xlsx")
#Setting 'graph' vector
graph <- hist(value$value)
#Calculating 'frequency percentage'
graph$density <- graph$counts / sum(graph$counts) * 100
#Drawing a histogram
plot(graph, main = "Title", xlab = "x-axis", ylab = "Frequency(%)", ylim = c(0, 100), breaks = "Sturges", col = "Gray", freq = FALSE, xaxt = "n")
axis(side = 1, at = seq(-8, 8, by =2))
#Plotting a frequency(%) polygon on the histogram(I have no idea how to draw smooth curve; thus, I used 'line() function')
lines(x = graph$mids, y =graph$density, type = "l", lty = 6, col = "Red")
#Putting a legend on the topright of the graph
legend("topright", c("The distribution of frequency(%)", "mean", "±2SD"), col = c("Red", "Black", "Black"), lty = c(6, 1, 2), lwd = c(2, 1, 1))
#Computing mean and standard deviations
m <- mean(value$value)
std <- sd(value$value)
#Plotting mean and ±2SD values
abline(v = m, lty = 1)
abline(v = m + 2*std, lty = 2)
abline(v = m - 2*std, lty = 2)
我可以将图例放在另一个不与图表重叠的区域吗? 我需要你的帮助,非常感谢。
【问题讨论】:
标签: r plot histogram legend distribution