将刻度线与条形对齐-R

时间:2020-04-11 12:05:50

标签: r plot bar-chart

如何在下面的代码中使刻度线与条形对齐。 感激!

代码

barplot(Freq, ylab="Frequency",xaxt="n")
axis(1, las=2, at=1:20, labels=c("[0,70]","(70,140]","(140,210]","(210,280]","(280,350]","(350,420]","(420,490]","(490,560]","(560,630]","(630,700]","(700,770]","(770,840]","(840,910]","(910,980]","(980,1050]","(1050,1120]","(1120,1190]","(1190,1260]","(1260,1330]","(1340,1400]"),cex.axis=0.8)
mtext(side = 1, text = "Range of Feedbacck Length (Character)", line = 4)

数据(称为频率)

structure(c(`(0,70]` = 2435L, `(70,140]` = 6472L, `(140,210]` = 5635L, 
`(210,280]` = 4399L, `(280,350]` = 2426L, `(350,420]` = 2155L, 
`(420,490]` = 1550L, `(490,560]` = 763L, `(560,630]` = 404L, 
`(630,700]` = 249L, `(700,770]` = 138L, `(770,840]` = 111L, `(840,910]` = 63L, 
`(910,980]` = 22L, `(980,1.05e+03]` = 58L, `(1.05e+03,1.12e+03]` = 107L, 
`(1.12e+03,1.19e+03]` = 154L, `(1.19e+03,1.26e+03]` = 142L, `(1.26e+03,1.33e+03]` = 143L, 
`(1.33e+03,1.4e+03]` = 68L), .Dim = 20L, .Dimnames = structure(list(
    c("(0,70]", "(70,140]", "(140,210]", "(210,280]", "(280,350]", 
    "(350,420]", "(420,490]", "(490,560]", "(560,630]", "(630,700]", 
    "(700,770]", "(770,840]", "(840,910]", "(910,980]", "(980,1.05e+03]", 
    "(1.05e+03,1.12e+03]", "(1.12e+03,1.19e+03]", "(1.19e+03,1.26e+03]", 
    "(1.26e+03,1.33e+03]", "(1.33e+03,1.4e+03]")), .Names = ""), class = "table")

1 个答案:

答案 0 :(得分:1)

您可以通过存储barplot对象来获取位置:

# the par is to have more space from the bottom, so you can see the labels
par(mar=c(8,3,3,3))
bp = barplot(Freq, ylab="Frequency",xaxt="n")
axis(1, las=2, at=bp, labels=names(Freq),cex.axis=0.8)
mtext(side = 1, text = "Range of Feedback Length (Character)", line = 4)

enter image description here