私はあなたの後ろに見えるのはよく分かりませんが、これはプロットパネルの外側にテキストを配置する2つの方法を示しています。どちらの方法でも、最初の作業は、テキスト "Lighter"と "Darker"を含む新しいテキストgrobを作成することです。最初の方法は、gtable
関数gtable_add_grob
を使用して新しいgrobを配置します。 2番目の方法は、ggplot
関数annotation_custom
を使用してgrobを配置します。
# Your data
data = read.table(text = ' "Subject" "Cond" "Distortion"
"1" "White" 10.7
"2" "White" 19.4
"3" "White" 15.9
"4" "White" 13.5
"5" "White" 15.4
"1" "Ambiguous" 13.4
"2" "Ambiguous" 11.4
"3" "Ambiguous" 8.9
"4" "Ambiguous" 11.0
"5" "Ambiguous" 11.4
"1" "Black" 8.4
"2" "Black" 1.7
"3" "Black" 7.7
"4" "Black" 8.0
"5" "Black" 5.7', header = TRUE)
library(ggplot2)
library(gtable)
library(grid)
# The plot
p <- ggplot(data, aes(x = Cond, y = Distortion)) +
stat_summary(fun.y = mean, geom = "bar", position = "dodge",
fill = "Gray", colour = "black") +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
position = position_dodge(width = 0.90), width = 0.2) +
labs(x = "Faces", y = "Lightness Distortion (gray levels)") +
theme_bw()
## Using `gtable` to position the text grob
# Set up text grob
text.grob1 <- textGrob("Darker", y = 0, just = "left",
rot = 90, gp = gpar(fontsize = 10, col = "red", fontface = "italic"))
text.grob2 <- textGrob("Lighter", y = 1, just = "right",
rot = 90, gp = gpar(fontsize = 10, col = "red", fontface = "italic"))
# Combine them into a single grob
text.grob <- gTree(children = gList(text.grob1, text.grob2))
# Get ggplot grob
g <- ggplotGrob(p)
# Add the grob to the column containing the y axis title
pos = subset(g$layout, grepl("ylab-l", name), t:l)
g <- gtable_add_grob(g, text.grob, l = pos$l, t = pos$t)
# Draw it
grid.newpage()
grid.draw(g)
## Using `annotation_custom` to position the text grob
# Set up text grob
text.grob1 <- textGrob("Lighter", y = 0, just = "left",
rot = 90, gp = gpar(fontsize = 10, col = "red", fontface = "italic"),
vjust = -2.5)
text.grob2 <- textGrob("Darker", y = 1, just = "right",
rot = 90, gp = gpar(fontsize = 10, col = "red", fontface = "italic"),
vjust = -2.5)
# Combine them into a single grob
text.grob <- gTree(children = gList(text.grob1, text.grob2))
# Add the grob to the plot
p <- p + annotation_custom(text.grob,
xmin = -Inf, xmax = -Inf, ymin = Inf, ymax = -Inf)
# The new grob is positioned outside the plot panel.
# To see the grob, clipping needs to be turned off.
g = ggplotGrob(p)
g$layout$clip <- "off"
# Draw it
grid.newpage()
grid.draw(g)
おかげサンディ!私は方法#1(gtable)を使用して終了しましたが、代わりに "Darker - Lighter"という文字列を持つ単一のテキストgrobを使用しました。私はtextGrobのy位置の引数に多くの "試行錯誤"を行い、最終的にy軸上のゼロをうまく整列させるダッシュを得ました。すべての有用な情報をありがとう!乾杯 – Moonbootica