【问题标题】:R horizontal barplot with axis labels split between two axisR水平条形图,轴标签在两个轴之间拆分
【发布时间】:2016-08-31 12:32:30
【问题描述】:

我有一个水平条形图,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)

我尝试创建两个单独的轴,并且名称确实出现在每个条的所需一侧,但与相应的条不齐。我会尝试上传条形图的图像,我对 R 还是很陌生,如果我遗漏了一些基本的东西,我深表歉意!

barplot1- names in correct position but all on one axis

barplot2- names on both sides of plot but not in line with appropriate bar

【问题讨论】:

    标签: r bar-chart


    【解决方案1】:

    我们可以使用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)
    

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      相关资源
      最近更新 更多