我希望建立一个图形,其最终会看起来像这样:但是在现阶段,我可以得到注解坐轴下方诠释轴下方时图coord_flip混乱
,因为我使用coord_flip
这使注释定位混淆。
我每次改变位置去轴下方(例如,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
这太棒了!谢谢你的帮助。 – pr1g11
不客气。我很喜欢学习更多关于网格图形的知识。 –