当图形coord_flip混淆时,在轴下注释

时间:2017-04-21 12:00:52

标签: r ggplot2 annotations data-visualization data-annotations

我正在寻找一个最终看起来像这样的图形:

enter image description here

但是目前我可以将注释放在轴下方,因为我正在使用coord_flip,这会使注释定位混乱。

目前我得到的是这样的: enter image description here

每当我将位置更改为低于轴(例如x = -1)时,我都会丢失注释。我试过把剪掉,但仍然没有希望。

预感谢!

这是我的代码:

library(grid)
library(ggplot2)
text_poor <- textGrob("Poor", gp=gpar(fontsize=13, fontface="bold"))
text_fair <- textGrob("Fair", gp=gpar(fontsize=13, fontface="bold"))
text_good <- textGrob("Good", gp=gpar(fontsize=13, fontface="bold"))
text_excel <- textGrob("Excellent", gp=gpar(fontsize=13, fontface="bold"))

g<-ggplot(Fig1,aes(reorder(`Company ID`,Score),Score,fill=Colour))+geom_bar(stat = "identity",alpha=0.8,width=0.8,colour="black")
g1<-g +  geom_text(data=Fig1,aes(label=paste(Score,"%")),fill="white",hjust = -0.25)+
  theme(aspect.ratio = 0.75,
        legend.position = "none",
        plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), "mm"),
        axis.title  = element_blank(),
        panel.grid.major.x =element_line(size = 0.3,colour="grey",linetype=2),
        panel.grid.minor =element_line(size = 0.15,colour="grey",linetype=3),
        panel.background = element_rect(fill = "transparent",colour = NA),
        axis.line= element_line(size = 0.0, colour = 1),axis.line.y=element_line(size = 0.3, colour = 1),
        axis.text.y = element_text(colour="black", size=rel(1.4),vjust=0.5),
        axis.text.x = element_blank(),
        axis.ticks=element_blank())+
  coord_flip()+scale_fill_manual(values=reportcol)+scale_y_continuous(expand = c(0,0),limits= c(0,100))+
  annotation_custom(text_poor,xmin=1,xmax=1,ymin=12.5,ymax=12.5)+
  annotation_custom(text_fair,xmin=1,xmax=1,ymin=37.5,ymax=37.5)+
  annotation_custom(text_good,xmin=1,xmax=1,ymin=62.5,ymax=62.5)+
  annotation_custom(text_excel,xmin=1,xmax=1,ymin=87.5,ymax=87.5)

gt <- ggplot_gtable(ggplot_build(g1))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

我的数据:

# A tibble: 4 × 3
  Score Colour `Company ID`
  <int>  <chr>        <chr>
1    34  Black    Company A
2    56  Black    Company B
3    39  Black    Company C
4    43    Red      Average

2 个答案:

答案 0 :(得分:1)

调整x刻度可为标签提供空间。但注释不在轴下方。

scale_x_discrete(expand = c(0, 0.9))

完整的情节代码

g<-ggplot(Fig1,aes(reorder(`Company ID`,Score),Score,fill=Colour))+geom_bar(stat = "identity",alpha=0.8,width=0.8,colour="black")
g1<-g +  geom_text(data=Fig1,aes(label=paste(Score,"%")),fill="white",hjust = -0.25)+
    theme(aspect.ratio = 0.75,
          legend.position = "none",
          plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), "mm"),
          axis.title  = element_blank(),
          panel.grid.major.x =element_line(size = 0.3,colour="grey",linetype=2),
          panel.grid.minor =element_line(size = 0.15,colour="grey",linetype=3),
          panel.background = element_rect(fill = "transparent",colour = NA),
          axis.line= element_line(size = 0.0, colour = 1),axis.line.y=element_line(size = 0.3, colour = 1),
          axis.text.y = element_text(colour="black", size=rel(1.4),vjust=0.5),
          axis.text.x = element_blank(),
          axis.ticks=element_blank())+
    coord_flip()+scale_fill_manual(values=reportcol)+scale_y_continuous(expand = c(0,0),limits= c(0,100))+
    scale_x_discrete(expand = c(0, 0.9)) +
    annotation_custom(text_poor,xmin=0.3,xmax=0.3,ymin=12.5,ymax=12.5)+
    annotation_custom(text_fair,xmin=0.3,xmax=0.3,ymin=37.5,ymax=37.5)+
    annotation_custom(text_good,xmin=0.3,xmax=0.3,ymin=62.5,ymax=62.5)+
    annotation_custom(text_excel,xmin=0.3,xmax=0.3,ymin=87.5,ymax=87.5)

g1

Custom Labels

答案 1 :(得分:1)

使用grid图形的第二个答案。

例如使用数据:

Fig1 <- data.frame(Score = c(34,56,39,43), Colour = c("Black", "Black", "Black", "Red"), 'Company ID' = c("A", "B", "C", "D"), check.names = FALSE)
reportcol <- c("red", "blue", "yellow", "green")

的软件包:

library(grid)
library(ggplot2)
library(gtable)

我只对您的ggplot代码进行了两处更改:

  1. 增加底部地块保证金
  2. 删除了自定义注释
  3. 我将使用grid图形

    添加这些内容
    g<-ggplot(Fig1,aes(reorder(`Company ID`,Score),Score,fill=Colour))+geom_bar(stat = "identity",alpha=0.8,width=0.8,colour="black")
        g1<-g +  geom_text(data=Fig1,aes(label=paste(Score,"%")),fill="white",hjust = -0.25)+
            theme(aspect.ratio = 0.75,
                  legend.position = "none",
                  plot.margin = unit(c(0.1, 0.1, 10, 0.1), "mm"), #Bottom Margin increased from 0.1 to 10
                  axis.title  = element_blank(),
                  panel.grid.major.x =element_line(size = 0.3,colour="grey",linetype=2),
                  panel.grid.minor =element_line(size = 0.15,colour="grey",linetype=3),
                  panel.background = element_rect(fill = "transparent",colour = NA),
                  axis.line= element_line(size = 0.0, colour = 1),axis.line.y=element_line(size = 0.3, colour = 1),
                  axis.text.y = element_text(colour="black", size=rel(1.4),vjust=0.5),
                  axis.text.x = element_blank(),
                  axis.ticks=element_blank())+
            coord_flip()+scale_fill_manual(values=reportcol)+scale_y_continuous(expand = c(0,0),limits= c(0,100))
    

    gtable对象

    创建ggplot
    g1Grob <- ggplotGrob(g1)
    gtable_show_layout(g1Grob)
    

    enter image description here

    从布局中,您可以看到标签的位置是(10,4)。

    创建1x4 gtable并将textGrobs添加到每个单元格 然后从原始ggplot

    添加到现有的gtable
    labels <- c("Poor","Fair","Good","Excellent")
    labelsGrob <- gtable(unit(c(1,1,1,1), c("null")),unit(1, "null"))
    gtable_show_layout(labelsGrob)
    

    enter image description here

    for (i in 1:4) {
        text <- textGrob(labels[i], gp=gpar(fontsize=13, fontface="bold"))
        labelsGrob <- gtable_add_grob(labelsGrob, text, 1, i)    
    }
    
    g1Grob <- gtable_add_grob(g1Grob, labelsGrob, 10, 4)
    

    最后绘制gtable对象

    grid.newpage()
    grid.draw(g1Grob)
    

    enter image description here