【问题标题】:Rotating xlabels and decoupling x-axis title from labels旋转 xlabels 并将 x 轴标题与标签分离
【发布时间】:2020-01-22 16:12:11
【问题描述】:

我现在已经尝试了很多东西,但它似乎对我不起作用。我正在寻找一种解决方案来转动我的 x 轴标签(到 45°)并解耦轴的标题,这样标签就不会覆盖我的轴标题。这是我的代码

barplot(regions$no_rows,xlab="regions",ylab="number of occurences",
        main="XXX", 
        names.arg = regions$ID, las=2,cex.lab=1,cex.names= 0.65)

这就是我得到的:

我已经裁剪了边距并缩小了标签的字体,但没有一种解决方案可以简单地显示图表,然后将轴标题与标签分开。 有了这个解决方案,我也没有得到 45° 的倾角。我也为此尝试了几个代码,例如这个:

regions = regions[with(regions, order(-no_rows)), ] 
end_point = 0.5 + nrow(regions) + nrow(regions)-1 
barplot(regions$no_rows, col="grey50", 
        main="XXX",
        ylab="occurences", ylim=c(0,5+max(regions$no_rows)),
        xlab = "regions",
        space=1)
text(seq(-0.5,end_point,by=2), par("usr")[3], 
     srt = 45, adj= 1, xpd = TRUE,
     labels = paste(regions$ID), cex=0.65) 

也不好看,因为 x 轴标题太靠近标签,而且标签也太靠近条形:

非常感谢您的帮助! P.s.:可以说这是我得到的df

regions
ID    no_rows
A       100
B        8
C        50
......

【问题讨论】:

    标签: r label bar-chart axis


    【解决方案1】:

    保持基地barplot 你可以关闭标签和轴并像这样制作自定义的。

    op <- par(mar=c(6, 4, 5, 4))
    b <- barplot(Employed ~ Month, data=dat1,  # store invisible output
                 xlab="",  # switch off axis title
                 xaxt="n", # switch off labels
                 col="orange",
                 main="My main title here"
    )
    # labels
    text(x=b,  # us inv. output for label positions
         y=-4, # shuft them somewhat down
         srt=45, adj= 1, xpd=TRUE,
         labels=dat1$Month, cex=0.65) 
    # axis title
    mtext("Month", 1, 4)
    par(op)
    


    数据

    dat1 <- structure(list(Employed = c(63.221, 63.639, 64.989, 63.761, 66.019, 
    67.857, 68.169, 66.513, 68.655, 69.564, 69.331, 70.551), Month = c("foooooooo_January", 
    "foooooooo_February", "foooooooo_March", "foooooooo_April", "foooooooo_May", 
    "foooooooo_June", "foooooooo_July", "foooooooo_August", "foooooooo_September", 
    "foooooooo_October", "foooooooo_November", "foooooooo_December"
    )), row.names = c(NA, -12L), class = "data.frame")
    

    【讨论】:

      【解决方案2】:

      ggplot2中的替代方案

      我不确定它是否正是您要寻找的,但如果您不必继续使用barplot,您可以使用ggplot2 轻松解决此问题,方法是使用参数axis.text.x theme 函数允许改变 x 标记的角度并调整它们的位置以不与轴重叠:

      regions <- data.frame(ID = LETTERS[1:3],
                            rows = c(100,8,50))
      
      library(ggplot2)
      ggplot(regions, aes(x = reorder(ID,-rows), y = rows))+
        geom_col()+
        scale_y_continuous(expand = c(0,0))+
        xlab("New Name")+
        theme_classic()+
        theme(axis.text.x = element_text(angle = 45, hjust = 1),
              axis.line.x = element_blank(),
              axis.ticks.x = element_blank(),
              plot.title = element_text(hjust = 0.5))+
        ggtitle("Main Title")
      

      【讨论】:

      • 两个问题:在这种情况下如何重命名轴标题?我可以让背景消失吗?
      • 我相应地编辑了我的答案。简而言之,您可以使用与base r 绘图主题非常接近的theme_classic(),也可以使用xlab 重命名x 轴标题。
      • 谢谢。现在不幸的是,它切断了图表的顶部,我仍然没有我的主要标题。另外有没有办法改变ylim,让它从0开始?
      • 我添加了几个参数以使该图看起来更类似于来自r 基本图的条形图。否则,我认为@jay.sf 已经回答了你的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      • 2020-12-04
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多