在R中复制柱状图的样式

在R中复制柱状图的样式,r,plot,histogram,R,Plot,Histogram,我试图使用以下数据集在R中重现直方图: ages <- c(26, 31, 35, 37, 43, 43, 43, 44, 45, 47, 48, 48, 49, 50, 51, 51, 51, 51, 52, 54, 54, 54, 54, 55, 55, 55, 56, 57, 57, 57, 58, 58, 58, 58, 59, 59, 62, 62, 63, 64, 65, 65, 65, 66, 66, 67, 67, 72, 86)

我试图使用以下数据集在R中重现直方图:

ages <- c(26, 31, 35, 37, 43, 43, 43, 44, 45, 47, 48, 48, 49, 50, 51, 51, 51, 51, 52, 54, 54, 54, 54, 
          55, 55, 55, 56, 57, 57, 57, 58, 58, 58, 58, 59, 59, 62, 62, 63, 64, 65, 65, 65, 66, 66, 67, 
          67, 72, 86)
然后是我对应的柱状图:

正确方向上的任何碰撞都是值得欣赏的。

1。年满18岁。 在您的数据中,52.5和61.5之间的类别中最多有17个数字。这与两侧的开放间隔是一致的:

ages <- c(26, 31, 35, 37, 43, 43, 43, 44, 45, 47, 48, 48, 49, 50, 51, 51, 51,
          51, 52, 54, 54, 54, 54, 55, 55, 55, 56, 57, 57, 57, 58, 58, 58, 58,
          59, 59, 62, 62, 63, 64, 65, 65, 65, 66, 66, 67, 67, 72, 86
          )

sum(ages >= 52.5 & ages <= 61.5)
[1] 17
图片如下:


我会把第二个移动到顶部。几乎完全符合所需的绘图(+1)如果这不是一个关于基本绘图好处的好广告,我不知道是什么。
ages <- c(26, 31, 35, 37, 43, 43, 43, 44, 45, 47, 48, 48, 49, 50, 51, 51, 51,
          51, 52, 54, 54, 54, 54, 55, 55, 55, 56, 57, 57, 57, 58, 58, 58, 58,
          59, 59, 62, 62, 63, 64, 65, 65, 65, 66, 66, 67, 67, 72, 86
          )

sum(ages >= 52.5 & ages <= 61.5)
[1] 17
library(plotrix) # for the break on x axis
library(shape)   # for styled arrow heads

# manually select axis ticks and colors
xticks <- c(25.5, 34.5, 43.5, 52.5, 61.5, 70.5, 79.5, 88.5)
yticks <- seq(2, 18, 2)
bgcolor  <- "#F2ECE4" # color for the background
barcolor <- "#95CEEF" # color for the histogram bars

# top level parameters - background color and font type
par(bg=bgcolor, family="serif")

# establish a new plotting window with a coordinate system
plot.new()
plot.window(xlim=c(23, 90), ylim=c(0, 20), yaxs="i")

# add horizontal background lines
abline(h=yticks, col="darkgrey")

# add a histogram using our selected break points
hist(ages, breaks=xticks, freq=TRUE, col=barcolor, xaxt='n', yaxt='n', add=TRUE)

# L-shaped bounding box for the plot
box(bty="L")

# add x and y axis
axis(side=1, at=xticks)
axis(side=2, at=yticks, labels=NA, las=1, tcl=0.5) # for inward ticks
axis(side=2, at=yticks, las=1)
axis.break(1, 23, style="zigzag", bgcol=bgcolor, brw=0.05, pos=0)

# add labels
mtext("Age", 1, line=2.5, cex=1.2)
mtext("Frequency", 2, line=2.5, cex=1.2)

# add arrows
u <- par("usr")
Arrows(88, 0, u[2], 0, code = 2, xpd = TRUE, arr.length=0.25)
Arrows(u[1], 18, u[1], u[4], code = 2, xpd = TRUE, arr.length=0.25)