【问题标题】:ggplot label bars in grouped bar plot分组条形图中的ggplot标签条
【发布时间】:2016-06-02 09:04:22
【问题描述】:

我试图绘制的数据结构如下:

      Year                        Country                           Count   
 1:   2010 St. Vincent and the Grenadines                               0
 2:   1970                        Ukraine                               0
 3:   1980                          Yemen                               1
 4:   1970                        Romania                               0
 5:   1950                         Cyprus                               0
 6:   1950                    Netherlands                               0
 7:   1980                     Mauritania                               0
 8:   1980                          Niger                               0
 9:   2010                        Grenada                               2
10:   1970                         Israel                               6
11:   1990                       Suriname                               0
12:   1990                      Singapore                               1
13:   1960                         Russia                               0
14:   1970                       Barbados                               0
15:   1950                         Panama                               0
16:   2010                           Mali                               3
17:   1980                         Greece                              11
18:   2010                      Venezuela                              15
19:   2000                         Malawi                               9
20:   2000                        Jamaica                              34
21:   1970                         Angola                               0
22:   1990                        Lebanon                               0
23:   1980       Central African Republic                               0
24:   1950                 United Kingdom                               1
25:   2010                        Iceland                              26

我从中创建了以下分组条形图:

我需要将国家/地区标签与相应的条对齐。即使在咨询 herehere 并按照链接中的建议使用 position = position_dodge(width= 0.9) 之后,标签似乎也没有对齐。我还尝试了 width 的不同值。

这里是创建上面图的代码:

> p<-ggplot(x[which(x$Count>0)], aes(Year, Count, label=Country)) + geom_bar(aes(fill = Country), position = "dodge", stat="identity")
> p+ theme(legend.position="none")+scale_x_discrete(limits=unique(x$Year))+geom_text(position = position_dodge(width= 0.9), aes(y=Count+10), angle=90)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    fill = Country 添加到geom_text 并明确指定闪避宽度以确保条形和标签对齐:

    library(data.table)
    library(ggplot2)
    
    # Fictional sample data
    x <- data.table(Year = c(2010,1970,1980,1970,1950,1950,1980,1980,2010), 
                    Country = c("St. Vincent and the Grenadines", "Ukraine", "Yemen", "Romania", "Cyprus", "Netherlands",
                                "Mauritania", "Niger", "Grenada"), Count = c(5,2,1,4,7,6,4,1,2))
    
    p <- ggplot(x[which(x$Count>0)], aes(Year, Count)) + geom_bar(aes(fill = Country), position = position_dodge(9), stat="identity")
    p + theme(legend.position="none") + scale_x_discrete(limits=unique(x$Year)) + geom_text(position = position_dodge(width= 9), aes(y=Count+0.25, fill=Country, label=Country, hjust=0), angle=90)
    

    (注:我也用hjust调整了位置)

    【讨论】:

      【解决方案2】:

      你可以试试

      library(ggplot2)
      ggplot(a,aes(factor(Year), Count, fill =Country, label =Country)) + 
        geom_col(position = position_dodge2(width = 0.9, preserve = "single"), show.legend = F) +
        geom_text(position = position_dodge2(width = 0.9, preserve = "single"), angle = 90, vjust=0.25, hjust=0) +
        ylim(0,40)
      

      数据

      a <- read.table(text="Year  Country Count
      2010    St.VincentandtheGrenadines  0
      1970    Ukraine 0
      1980    Yemen   1
      1970    Romania 0
      1950    Cyprus  0
      1950    Netherlands 0
      1980    Mauritania  0
      1980    Niger   0
      2010    Grenada 2
      1970    Israel  6
      1990    Suriname    0
      1990    Singapore   1
      1960    Russia  0
      1970    Barbados    0
      1950    Panama  0
      2010    Mali    3
      1980    Greece  11
      2010    Venezuela   15
      2000    Malawi  9
      2000    Jamaica 34
      1970    Angola  0
      1990    Lebanon 0
      1980    CentralAfricanRepublic  0
      1950    UnitedKingdom   1
      2010    Iceland 26", header=T)
      

      【讨论】:

        【解决方案3】:
         df<-data.frame(Year=c(1980,2010,1970,1990,2010,1980,2010,2000,2000,1950,2010),
                           Country=c("Yemen","Grenada","Israel","Singapore","Mali","Greece",
                                     "Venezuela","Malawi","Jamaica","UK","Iceland"),
                           Count=c(1,2,6,1,3,11,15,9,34,1,26))
        
            ggplot(df,aes(x=factor(Year),y=Count,fill=Country))+
                geom_bar(stat="identity",position = "dodge")+
                ylim(0,max(df$Count*1.5))+
                geom_text(aes(label=Country),hjust=-0.5,vjust=0,colour="red",angle=90,position = position_dodge(.9))
        

        【讨论】:

          猜你喜欢
          • 2018-12-03
          • 2013-08-12
          • 1970-01-01
          • 2021-09-26
          • 2018-04-01
          • 2012-07-24
          • 2016-08-16
          • 1970-01-01
          相关资源
          最近更新 更多