ggplot2,注释y轴以指示使用的比例方向

时间:2016-04-22 22:37:50

标签: r ggplot2

我制作了一个带有(连续)y轴的条形图,它反映了感知亮度失真。我的数据测量方式,数字大于零意味着这些对象被判断为比实际更轻,数字低于零意味着这些对象被判断为比实际更暗。如何在y轴旁放置一些文字,表示我使用的刻度的方向,即“更暗 - 更轻”?我仍然希望将当前的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

感谢您阅读!

1 个答案:

答案 0 :(得分:0)

我不太确定你所看到的外观,但这显示了两种在文本面板外定位文字的方法。对于这两种方法,第一项任务是创建一个包含文本“Lighter”和“Darker”的新文本grob。第一种方法使用gtable函数gtable_add_grob定位新的grob;第二种方法使用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)