gpt4 book ai didi

r - 在条形图中添加两条垂直线

转载 作者:行者123 更新时间:2023-12-02 01:31:36 24 4
gpt4 key购买 nike

我想在我制作的条形图中添加两条垂直线,但遇到了问题。

示例数据集

data<-data.frame(area=rep(c("ES","OC","VB"),each=2,times=2),num=c(0,10,23,40,25,60,80,45,10,25,10,0),
bin=rep(c(85,90),times=3))

条形图代码。大多数格式化代码是针对实际数据集的。

require(lattice)         
barchart(num~as.factor(bin), data=data,col=c("black","lightslategray","light gray"),
groups = area, stack = F, horizontal=F,xlab="Shell Length (mm)",ylab="Frequency (N)",
scales=list(y=list(alternating=1,tck=-1,limits=c(0,400),
at=c(0,50,100,150,200,250,300,350,400),
labels=c("0","50","100","150","200","250","300","350","400"),relation="same")),
par.settings = list( grid.pars = list(fontfamily = 'serif'),axis.line=list(col=0)),
panel = function(...) {
lims<-current.panel.limits()
panel.barchart(...)
panel.abline(v=as.factor(80),col="red",lty=2)
panel.abline(v=as.factor(90),h=0,col="black")
panel.abline(h=lims$ylim[1],v=lims$xlim[1])
panel.text(25,200,"OC (N = 1,947)",col="black",cex=.8)
panel.text(25,185,"ES (N = 2,623)",col="lightslategray",cex=.8)
panel.text(25,170,"VB (N = 2,780)",col="light gray",cex=.8)
})

当我在图表中运行代码时,一条垂直线出现,但第二条线没有出现。不管我给 v 赋予什么值(value),一行都不会改变位置。

R信息

R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets
[7] methods base

other attached packages:
[1] plyr_1.8.3 Hmisc_3.17-0 ggplot2_1.0.1
[4] Formula_1.2-1 survival_2.38-1 car_2.0-26
[7] MASS_7.3-40 xlsx_0.5.7 xlsxjars_0.6.1
[10] rJava_0.9-7 latticeExtra_0.6-26 RColorBrewer_1.1-2
[13] lattice_0.20-31

如有任何帮助,我们将不胜感激。谢谢

最佳答案

您在代码的开头将 bin 转换为一个因子(尽管即使您不这样做,它也会被强制转换为一个因子)创建一个分类轴,其隐式主刻度坐标为 1 和2 作为每组条形的中心,而不是您预期的 85 和 90。例如,当您按如下方式更改代码时会发生什么:

       panel.abline(v=1,col="red",lty=2)
panel.abline(v=2,h=0,col="black")
panel.abline(h=lims$ylim[1],v=lims$xlim[1])
panel.text(1,200,"OC (N = 1,947)",col="black",cex=.8)
panel.text(1.5,185,"ES (N = 2,623)",col="lightslategray",cex=.8)
panel.text(2,170,"VB (N = 2,780)",col="light gray",cex=.8)
})

关于r - 在条形图中添加两条垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com