如何在 R 中的直方图上叠加频率多边形?

How do I superimpose a frequency polygon on top of a histogram in R?

这是我在 R 中使用的代码(使用 RGui 64 位,R 版本 3.3.1)绘制数据直方图和频率多边形。我没有使用 ggplot2。我怎样才能将频率多边形叠加在直方图上,这样我就不必做两个单独的图了?也就是说,我想要绘制直方图,频率多边形覆盖在它上面。

# declare your variables
data <- c(10, 7, 8, 4, 5, 6, 6, 9, 5, 6, 3, 8,
+ 4, 6, 10, 5, 9, 7, 6, 2, 6, 5, 4, 8, 7, 5, 6)

# find the range
range(data)

# establish a class width
class_width = seq(1, 11, by=2)
class_width

# create a frequency table
data.cut = cut(data, class_width, right=FALSE)
data.freq = table(data.cut)
cbind(data.freq)

# put both graphs together
par(mfrow=c(1,2))

# histogram of this data
hist(data, 
breaks=class_width, 
col="slategray3", 
border = "dodgerblue4",
right=FALSE,
xlab = "Scores", 
main = "Histogram of Quiz Data")

# create a frequency polygon for the birth weight data
plot(data.freq, type="b", 
xlab="Scores", 
ylab="Frequency",
add=TRUE,
main="A Frequency Polygon of Quiz")

这将覆盖两个图表。我已经删除了额外的主标题和标签,因为它们也会被覆盖,看起来很乱。

# declare your variables
data <- c(10, 7, 8, 4, 5, 6, 6, 9, 5, 6, 3, 8,
+ 4, 6, 10, 5, 9, 7, 6, 2, 6, 5, 4, 8, 7, 5, 6)

# find the range
range(data)

# establish a class width
class_width = seq(1, 11, by=2)
class_width

# create a frequency table
data.cut = cut(data, class_width, right=FALSE)
data.freq = table(data.cut)
cbind(data.freq)

# put both graphs together
par(mfrow=c(1,2))

# histogram of this data
hist(data, 
breaks=class_width, 
col="slategray3", 
border = "dodgerblue4",
right=FALSE,
xlab = "Scores", 
main = "Histogram of Quiz Data")

# this is key to the overlay
par(new=TRUE)

# create a frequency polygon for the birth weight data
plot(data.freq, type="b")