我正在寻找一个最终看起来像这样的图形:
但是目前我可以将注释放在轴下方,因为我正在使用coord_flip
,这会使注释定位混乱。
每当我将位置更改为低于轴(例如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
答案 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
答案 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
代码进行了两处更改:
我将使用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)
从布局中,您可以看到标签的位置是(10,4)。
创建1x4 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)
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)