2017-04-21 38 views
0

我希望建立一个图形,其最终会看起来像这样:但是在现阶段,我可以得到注解坐轴下方诠释轴下方时图coord_flip混乱

enter image description here

,因为我使用coord_flip这使注释定位混淆。

在我得到这样的时刻: enter image description here

我每次改变位置去轴下方(例如,x = -1)我失去了注解。我试过关闭裁剪,但仍然没有希望。

Prementive谢谢你!

这里是我的代码:

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 

回答

1

第二个答案。

用于例如数据:

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. 删除自定义注释

我会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)) 

添加这些创建gtableggplot对象

g1Grob <- ggplotGrob(g1) 
gtable_show_layout(g1Grob) 

enter image description here

从布局,可以看到的地方的标签是( 10,4)。

创建一个1×4 gtable,并添加textGrobs到每个细胞 然后从原始ggplot

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最后绘制gtable对象

grid.newpage() 
grid.draw(g1Grob) 

enter image description here

+0

这太棒了!谢谢你的帮助。 – pr1g11

+0

不客气。我很喜欢学习更多关于网格图形的知识。 –

1

调节X规模,可为您的标签空间。虽然注释不在轴下方。

scale_x_discrete(expand = c(0, 0.9)) 

完整情节的代码使用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, 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

+0

干杯杰里米,这是一个很好的工作。仍然试图找到一种将注释放在轴下的方法,就像我更喜欢美学一样。 – pr1g11

+0

您可能需要为此使用网格图形。 –

+0

我有一个快速的尝试,但我还不熟悉网格图形系统。 –