我有一个水平条形图,x轴的中间为零,希望每个条形的名称与条形本身出现在同一侧。我使用的代码是:
abun<-data$av.slope
species<-data$Species
cols <- c("blue", "red")[(abun > 0)+1]
barplot(abun, main="Predicted change in abundance", horiz=TRUE,
xlim=c(-0.04,0.08), col=cols, names.arg=species, las=1, cex.names=0.6)
我尝试创建两个单独的轴,并且名称确实出现在每个条的所需侧,但未与相应条对齐。我将尝试上传该barplot的图像,这对于R还是很新的,如果我缺少一些基本知识,我们深表歉意!
我们可以使用mtext
以下方法完成此操作:
由于您没有在问题中包含数据,因此我生成了自己的虚拟数据集。如果您发布dput
自己的数据,我们可以使该解决方案适应您的数据。
set.seed(123)
df1 <- data.frame(x = rnorm(20),
y = LETTERS[1:20])
df1$colour <- ifelse(df1$x < 0, 'blue', 'red')
bp <- barplot(df1$x, col = df1$colour, horiz = T)
mtext(side = ifelse(df1$x < 0, 2, 4),
text = df1$y,
las = 1,
at = bp,
line = 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句