gpt4 book ai didi

r - 带有切割 y 轴的分组条形图

转载 作者:行者123 更新时间:2023-12-04 10:37:15 25 4
gpt4 key购买 nike

我正在尝试使用分组条形图和切割 y 轴绘制一个图。但是,我似乎无法同时获得两者。使用这些数据:

d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
127,74,5140,1681,860,963,866,
470,26419,8795,4521,5375,4514,2487),
nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

我可以得到分组的条形图,例如:
barplot( d, beside = TRUE)

Example

然后我可以使用以下方法获得切割的 y 轴:
# install.packages('plotrix', dependencies = TRUE)
require(plotrix)
gap.barplot( as.matrix(d),
beside = TRUE,
gap=c(9600,23400),
ytics=c(0,3000,6000,9000,24000,25200,26400) )

enter image description here

但是,然后我松开了分组和 A、B、C... 标签。我怎样才能得到两者?

最佳答案

您可以手动执行此操作。赞 barplot , ?gap.barplot返回条形的中心位置。使用这些来添加标签。

使用 space用于组之间的间距,如常规 barplot似乎不起作用。我们可以使用一排 NA 来破解一个空间。

d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
127,74,5140,1681,860,963,866,
470,26419,8795,4521,5375,4514,2487),
nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

# add row of NAs for spacing
d=rbind(NA,d)

# install.packages('plotrix', dependencies = TRUE)
require(plotrix)

# create barplot and store returned value in 'a'
a = gap.barplot(as.matrix(d),
gap=c(9600,23400),
ytics=c(0,3000,6000,9000,24000,25200,26400),
xaxt='n') # disable the default x-axis

# calculate mean x-position for each group, omitting the first row
# first row (NAs) is only there for spacing between groups
aa = matrix(a, nrow=nrow(d))
xticks = colMeans(aa[2:nrow(d),])

# add axis labels at mean position
axis(1, at=xticks, lab=LETTERS[1:6])

关于r - 带有切割 y 轴的分组条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24202245/

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