gpt4 book ai didi

r - 如何为分组条形图在轴中添加中断?

转载 作者:行者123 更新时间:2023-12-01 01:00:28 25 4
gpt4 key购买 nike

# set up data

x1=c(3,5,6,9,375,190);
x1
x2=c(2,2,3,30,46,60);
x2
data=rbind(x1,x2);
data
colnames(data)=c("Pig","Layer","Broiler","Dairy","Beef","Sheep")
rownames(data)=c("1980","2010")
data

# plot grouped bar by using barplot
barplot(data,
beside=T,
ylab="Number of animal",
#cex.names=0.8,
#las=2,
col=c("darkblue","red")
)

# Since there are large differences in numbers, so I want to add a break between 200 to 340 as below:
data_T=t(data);
data_T
#install.packages("reshape")
library(reshape)
mdata <- melt(data_T, id=c("1980","2010"));
mdata
colnames(mdata)=c("Animal","Year",'value');
mdata
gap.barplot(mdata$value,
gap=c(200,340),
xlab="Animal",
ytics=c(0,50,100,150,200,300,350,400),
ylab="Number of animal",
xaxlab=mdata$Animal,
xaxt="n")
# xaxt="n" is esentiall to remove everything from x axis (e.g. a clean x axis)
# then define a axis using the following
axis(side = 1, at = seq_along(mdata$Animal),mdata$Animal,tick = FALSE)
abline(h=seq(200,205,.001), col="white") # hiding vertical lines
axis.break(axis=2,breakpos=202.5,style="slash") # break the left Y axis

但这并不是我想要的,因为条形图(在 gap.barplot 中)没有像以前的条形图图形一样分组:(然后我的问题是如何在分组的条形图图形中制作一个断开的 y 轴?换句话说,如何组合barplot 和 gap.barplot 的功能?非常感谢您的帮助!

最佳答案

非常感谢 gap.barplot 的作者 Jim Lemon ,他通过电子邮件回复了我(见下文)。
有了他的代码,我得到了我想要的。
您可能需要安装和加载包 plotrix .
嗨,魏,gap.barplot功能不做分组条(还)。你大概可以
得到你想要的:

#install.packages("plotrix") # only need to install once
library(plotrix)
newdata<-data
newdata[newdata>200]<-newdata[newdata>200]-140
barpos<-barplot(newdata,names.arg=colnames(newdata),
ylim=c(0,250),beside=TRUE,col=c("darkblue","red"),axes=FALSE)
axis(2,at=c(0,50,100,150,200,235),
labels=c(0,50,100,150,200,375))
box()
axis.break(2,210,style="gap")
吉姆

关于r - 如何为分组条形图在轴中添加中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884991/

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