R 图形坐标翻转时轴下方的注释

R 图形坐标翻转时轴下方的注释,r,ggplot2,annotations,data-visualization,data-annotations,R,Ggplot2,Annotations,Data Visualization,Data Annotations,我希望建立一个图形,最终会像这样: 但是现在我可以让注释位于轴的下方,因为我使用的是coord\u flip,这会混淆注释的位置 现在我得到的是这样的东西: 每次我将位置更改为轴下方(例如x=-1),我都会丢失注释。我试过关闭剪辑,但还是没有希望 谢谢你 这是我的密码: library(grid) library(ggplot2) text_poor <- textGrob("Poor", gp=gpar(fontsize=13, fontface="bold")) text_fair

我希望建立一个图形,最终会像这样:

但是现在我可以让注释位于轴的下方,因为我使用的是
coord\u 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)
库(网格)
图书馆(GG2)

文本\u poor调整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

g调整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

g使用
grid
graphics第二个答案

使用的数据,例如:

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")
我只对您的
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))
    

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

    创建一个1x4
    gtable
    ,并向每个单元格添加textGrobs 然后从原始ggplot添加到现有GTTable

    labels <- c("Poor","Fair","Good","Excellent")
    labelsGrob <- gtable(unit(c(1,1,1,1), c("null")),unit(1, "null"))
    gtable_show_layout(labelsGrob)
    

    第二个答案使用
    网格
    图形

    使用的数据,例如:

    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")
    
    我只对您的
    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))
    

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

    创建一个1x4
    gtable
    ,并向每个单元格添加textGrobs 然后从原始ggplot添加到现有GTTable

    labels <- c("Poor","Fair","Good","Excellent")
    labelsGrob <- gtable(unit(c(1,1,1,1), c("null")),unit(1, "null"))
    gtable_show_layout(labelsGrob)
    

    干杯,杰里米,这是一个不错的工作。仍在试图找到一种将注释放在轴下方的方法,就像我更喜欢美学一样。你可能需要使用网格图形。我做了一个快速的尝试,但我对网格图形系统还不够熟悉。杰里米,干杯,这是一个很好的解决方法。仍在试图找到一种将注释放在轴下方的方法,就像我更喜欢美学一样。你可能需要使用网格图形。我做了一个快速的尝试,但我对网格图形系统还不够熟悉。这太棒了!谢谢你的帮助,不客气。我喜欢学习更多关于网格图形的知识。这太棒了!谢谢你的帮助,不客气。我喜欢学习更多关于网格图形的知识。