R:ggplot使用position_dodge和facets注释geom_text

时间:2015-03-13 15:09:40

标签: r plot ggplot2 geom-text

我有一个R数据帧data(由dplyr制作),我试图用ggplot()绘制:

require(dplyr)
data <- structure(list(gGroup = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L), .Label = c("MC", "R", "UC"), class = "factor"), 
    Episode = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("Morning", "Day", "Night", "24 hour"
    ), class = "factor"), variable = c("HF", "HF", "LF", "LF", 
    "HF", "HF", "LF", "LF", "HF", "HF", "LF", "LF"), parameter = c("RR", 
    "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", 
    "RT"), mean = c(3.90575222833804, 4.24572828952087, 5.14491629837998, 
    3.88189313775535, 4.02908403079823, 3.91129824615597, 4.73913642980089, 
    3.63973850905423, 4.66445796048274, 4.21723744674943, 5.57765585365275, 
    4.01444148455851), sd = c(1.09129154084895, 1.43102672123806, 
    1.17782114274004, 1.33381488706382, 1.33497319178289, 1.22259231099975, 
    1.33329948427898, 1.09625319168102, 1.19876558625356, 1.73746797295816, 
    1.05862249404741, 1.91144835753868), se = c(0.199241664579179, 
    0.261268538538247, 0.215039736195078, 0.243520167060353, 
    0.471984298305965, 0.432251656867227, 0.471392553343098, 
    0.387584032867524, 0.215304655178374, 0.312058460044998, 
    0.190134212775724, 0.343306259564318)), .Names = c("gGroup", 
"Episode", "variable", "parameter", "mean", "sd", "se"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
    gGroup = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("MC", 
    "R", "UC"), class = "factor"), Episode = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("Morning", "Day", "Night", 
    "24 hour"), class = "factor"), variable = c("HF", "LF", "HF", 
    "LF", "HF", "LF")), .Names = c("gGroup", "Episode", "variable"
), class = "data.frame", row.names = c(NA, -6L)))

目前我正在使用以下代码进行绘图:

require(ggplot2)
require(ggthemes)
pd <- position_dodge(width=0.9)
p <- ggplot(data, aes(x = gGroup, y = mean, fill = variable)) +
    facet_grid(parameter~Episode) +
    geom_bar(stat="identity", position=pd) +
    geom_errorbar(aes(ymin = mean-se, ymax = mean+se), width = .3, position=pd) +
    theme_hc() + scale_fill_hc() +
    labs(y = "Logit transform of spectral power (m/s2), mean±SE", x= NULL)

ann_text <- data.frame(gGroup = "MC", mean = 6, variable = "LF", parameter = "RR", Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2), data = ann_text, label="*", position=pd)

这给了我以下情节: example plot

我对结果非常满意,但正如您所看到的那样,星号未正确对齐。我在网上查了一下,然后阅读了thisthis以及手册。 每个人都看到了使用position=position_dodge(width=0.9)的建议,但这对我没有帮助。我试过hjust可能会将星号移到正确的位置,但这也没用。有趣的是,我的错误栏已正确对齐。

我觉得我忽略了一些非常简单的东西,但我无法弄清楚它是什么。 我在OSX 10.10.2上使用R 3.1.3,并加载最新版本的ggplot2ggthemes

1 个答案:

答案 0 :(得分:1)

为了使position_dodge起作用,需要有理由躲闪。那就是你需要用ann_text适当地改变variable = c("LF", "HF"),这样才有理由躲闪。然后恰当地定义标签。下面我假设你只想*超过LF栏。

ann_text <- data.frame(gGroup = rep("MC",2), 
                       mean = 6, 
                       variable = c("LF", 'HF'), 
                       label = c("*", ""),
                       parameter = "RR", 
                       Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2, label = label), data = ann_text, position=pd)