【问题标题】:How can I unify width of bars in ggplot2?如何统一ggplot2中条的宽度?
【发布时间】:2019-08-06 23:02:50
【问题描述】:

我正在尝试使用 geom_bar(stat="count") 按性别获取条形图,除了条形的宽度不一致之外,一切似乎都很好。例如,如果在“F”组中没有得分值为 3 的观察值,则“M”组的条形将变为两倍大。如何使其与示例中的其他条一样窄?

Score = read.table(header=TRUE,text='score sex
    1 0 F
    2 0 F
    3 0 M
    4 0 M
    5 0 F
    6 1 F
    7 1 M
    8 1 M
    9 2 F
    10 2 M
    11 2 F
    12 3 M
    13 3 M'
    )

b1=ggplot(Score, aes(x=factor(score), fill=sex))+
    geom_bar(stat="count", position=position_dodge())+
    theme_classic()
b1

【问题讨论】:

    标签: width bar-chart


    【解决方案1】:

    将 position=position_dodge2(preserve="single") 添加到 geom_bar() 行解决了它:

    b1=ggplot(Score, aes(x=factor(score), fill=factor(sex)))+
        geom_bar(stat="count", position=position_dodge2(preserve="single"))+
        theme_classic()
    b1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多