r - 在我的 ggplot2 图中添加一个指向 x 轴的箭头

标签 r ggplot2 annotations grob

我的目标是得到一个带有指向我的 x 轴的文本的箭头来标记平均词频。我无法弄清楚如何在 ggplot2 的绘图区域之外获取箭头或文本。

这是我的代码:

ggplot(SUMMARY.PCTDIFF, aes(principle, pctdiff)) +xlab("Principle")+ylab("% Difference")+
    geom_bar(aes(fill = Sector),position = "dodge", stat="identity",col="black")+
    ggtitle("How Differing Sectors Talk about Different Co-operative Principles")+theme(plot.title=element_text(hjust=0.5),panel.border = element_rect(colour = "black", fill=NA, size=1),legend.background = element_rect(color = "black"),legend.position =c(1.10,0.7),plot.margin=unit(c(0.5,3,0.5,0.5),"cm"))+
    geom_hline(yintercept = 0)

这是我的数据:

structure(list(principle = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 
3L, 4L, 5L, 6L, 7L), .Label = c("principle 1", "principle 2", 
"principle 3", "principle 4", "principle 5", "principle 6", "principle 7"
), class = "factor"), pctdiff = c(-6.71369900758148, 9.44233503731219, 
11.0107840625484, -53.1259224412594, -9.22356636157099, -41.0142340880256, 
-9.6288000905822, -55.3759198976217, -19.1012386886889, -75.4996296064581, 
-44.7688906852688, -58.5771031808126, 48.9559446961189, 4.52522805921763, 
29.4368505336144, 51.7181015822617, -2.26377179780978, 48.1990295720174, 
57.4736669182075, 8.41689226435695, -15.8983115124042, 30.6261564806236, 
79.5018869249509, 87.2542807992621, -15.3228747516659, 13.5915020818539, 
48.2301858238767), Sector = c("AG", "AG", "AG", "AG", "AG", "AG", 
"AG", "FIN", "FIN", "FIN", "FIN", "FIN", "FIN", "FIN", "SERV", 
"SERV", "SERV", "SERV", "SERV", "SERV", "SERV", "OTHER", "OTHER", 
 "OTHER", "OTHER", "OTHER", "OTHER")), .Names = c("principle", 
 "pctdiff", "Sector"), row.names = c(NA, -27L), class = "data.frame")

最佳答案

df <- structure(list(principle = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 
3L, 4L, 5L, 6L, 7L), .Label = c("principle 1", "principle 2", 
"principle 3", "principle 4", "principle 5", "principle 6", "principle 7"
), class = "factor"), pctdiff = c(-6.71369900758148, 9.44233503731219, 
11.0107840625484, -53.1259224412594, -9.22356636157099, -41.0142340880256, 
-9.6288000905822, -55.3759198976217, -19.1012386886889, -75.4996296064581, 
-44.7688906852688, -58.5771031808126, 48.9559446961189, 4.52522805921763, 
29.4368505336144, 51.7181015822617, -2.26377179780978, 48.1990295720174, 
57.4736669182075, 8.41689226435695, -15.8983115124042, 30.6261564806236, 
79.5018869249509, 87.2542807992621, -15.3228747516659, 13.5915020818539, 
48.2301858238767), Sector = c("AG", "AG", "AG", "AG", "AG", "AG", 
"AG", "FIN", "FIN", "FIN", "FIN", "FIN", "FIN", "FIN", "SERV", 
"SERV", "SERV", "SERV", "SERV", "SERV", "SERV", "OTHER", "OTHER", 
 "OTHER", "OTHER", "OTHER", "OTHER")), .Names = c("principle", 
 "pctdiff", "Sector"), row.names = c(NA, -27L), class = "data.frame")

library(ggplot2)
library(grid)
p <- ggplot(df, aes(principle, pctdiff)) +xlab("Principle")+ylab("% Difference")+
    geom_bar(aes(fill = Sector),position = "dodge", stat="identity",col="black")+
    ggtitle("How Differing Sectors Talk about Different Co-operative Principles")+theme(plot.title=element_text(hjust=0.5),panel.border = element_rect(colour = "black", fill=NA, size=1),legend.background = element_rect(color = "black"),legend.position =c(1.10,0.7),plot.margin=unit(c(0.5,3,0.5,0.5),"cm"))+
    geom_hline(yintercept = 0) 

# Add text in the bottom right corner outside the plotting area
p <- p + annotation_custom(
  grob = grid::textGrob(label = "Text", hjust=0, gp=gpar(col="blue", cex=1.7)),
  xmin = 8, xmax = 8, ymin =-90, ymax = -90
) 
# Add an arrow in the bottom right corner outside the plotting area
p <- p + annotation_custom(
  grob = linesGrob(arrow=arrow(type="open", ends="first", length=unit(3,"mm")), gp=gpar(col="red", lwd=3)), 
  xmin = 8.25, xmax = 8.5, ymin = -85, ymax = -60
) 

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

enter image description here

关于r - 在我的 ggplot2 图中添加一个指向 x 轴的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45679394/

相关文章:

java - Hibernate @Any 注解用法

r - 在 rpart 分类树中打印没有科学记数法的日期

r - ggplot 条形图,条形方向翻转

ggplot2 - 使用facet_wrap_paginate分离出facet_wrap

R:使用 ggplot 创建自定义形状

java - Enunciate 中收集的 TypeHint

r - 索引操作删除属性

r - 如何通过索引在初始化向量中存储不同大小的 for 循环输出

r - 如何在 EC2 上运行 R 版本 > 3 或更新 Debian

django - 为什么这个 Django 按 'annotate' 变量过滤不起作用?