2016-04-22 9 views
1

私は知覚的な明度の歪みを反映する(連続的な)y軸を持つ棒グラフを作成しました。私のデータが測定された方法では、ゼロより上の数字はそれらのオブジェクトが実際よりも軽いと判断されたことを意味し、0より小さい数字はそれらのオブジェクトが実際より暗いと判断されたことを意味します。私が使用したスケールの方向を示すy軸の横にテキストを配置するにはどうすればよいですか?私はまだ私の現在のY軸のラベルを "明度歪み(グレイレベル)"として保持したいと思っていますが、読者にスケールを明確にするアノテーションを追加するだけです。ここでggplot2、使用されるスケールの方向を示すy軸に注釈を付ける

は私のデータのサンプルです:

"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 

そして、私の棒グラフを生成するコード:

library(ggplot2) 
g <- ggplot(data, aes(x = Cond, y = Distortion)) 
g + 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() 

sample graph

をお読みいただきありがとうございました!

答えて

0

私はあなたの後ろに見えるのはよく分かりませんが、これはプロットパネルの外側にテキストを配置する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) 

enter image description here

## 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) 
+0

おかげサンディ!私は方法#1(gtable)を使用して終了しましたが、代わりに "Darker - Lighter"という文字列を持つ単一のテキストgrobを使用しました。私はtextGrobのy位置の引数に多くの "試行錯誤"を行い、最終的にy軸上のゼロをうまく整列させるダッシュを得ました。すべての有用な情報をありがとう!乾杯 – Moonbootica

関連する問題