2

私の目標は、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")
4

1 に答える 1

2
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)

ここに画像の説明を入力

于 2017-08-14T17:16:23.237 に答える