【问题标题】:Set breaks of axis by level of factor in ggplot2在ggplot2中按因子水平设置轴的中断
【发布时间】:2015-09-29 11:16:32
【问题描述】:

给定一个 x 轴是离散的图,由有序因子给出,如下图:

等级:

D E F G H I J
1 1 1 2 2 3 4

有没有办法:

scale_x_discrete(breaks=c("D","G", "I", "J"), lables=c(1,2,3,4))

无需手动复制中断?

【问题讨论】:

    标签: r ggplot2 axes


    【解决方案1】:

    这就是你所追求的吗?

    keeps<-c(1,4,6,7) # Indices for levels you want to show
    ggplot(data=diamonds)+
      geom_bar(aes(x=color,y=carat),stat="identity")+
      scale_x_discrete(
        breaks=levels(d$color)[keeps],
        labels=table(d$color)[keeps])
    

    【讨论】:

    • 这里的问题是我必须手动选择休息时间,即在本例中为 c(1,4,6,7)。在我的问题中,我将其称为“手动复制休息时间”。是否有任何动态方法可以按因子水平对刻度进行分组?
    • 你是如何选择关卡的?
    • 这个想法是在因子水平发生变化时放置中断。 G 将是第一个 2 级,I 第一个 3,J 第一个 4。因此 D,G,I,J
    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多