【问题标题】:How to center bars in R barplot() around ticks如何在 R barplot() 中围绕刻度居中条形
【发布时间】:2019-07-30 17:59:43
【问题描述】:

我仍在努力解决与 R: in barplot midpoints are not centered w.r.t. bars 类似的问题。

我想制作一个条形图,但我的条形图以刻度为中心。但是,如果我按照下面描述的方式进行操作,我的条将向右移动 1.2 而不是 1 点。

我需要让我的条居中以便在条上放置标签,所以我想问你是否有办法做到这一点。

Part1 <- c(2,4,9,18,20)
Part2 <- c(2,5,1,4,0)
counts <- rbind(Part1, Part2)
colnames(counts) <- c(1,2,3,4,5)

x <- barplot(counts, 
  axes = FALSE,  
  col = c("darkgreen", "red"),
  xlim = c(0, 5*1.50),
  ylim = c(0,60)
)
axis(side = 2, pos = 0) 
axis(side = 1, at = c(0,1,2,3,4,5), tick = TRUE)

【问题讨论】:

    标签: r plot bar-chart


    【解决方案1】:
    Part1 <- c(2,4,9,18,20)
    Part2 <- c(2,5,1,4,0)
    counts <- rbind(Part1, Part2)
    colnames(counts) <- c(1,2,3,4,5)
    
    x<-barplot(counts, 
      axes = FALSE,  
      space = 0,         
      col = c("darkgreen", "red"),
      xlim = c(0, 5*1.50),
      ylim = c(0,60)
    )
    
    #create positions for tick marks, one more than number of bars
    ticks <- seq_len(length(counts) + 1)
    axis(side = 2, pos = 0)
    #adding x-axis with offset positions, with ticks, but without labels
    axis(side = 1, at = ticks - 0.5, labels = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 2013-04-13
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多