2015-03-13 110 views
0

我的R数据框data(与dplyr制造),我试图用ggplot()绘制:目前我使用下面的代码绘制R:与position_dodge和ggplot注释geom_text微面

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

+0

用的东西[最小再现的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)是它们应该是最小的,并重复性。你的例子既不是。 – shadow 2015-03-13 15:30:27

+0

感谢您的评论! 我添加了一个dput和一些关于加载包的信息。我可以用提供的代码重现我的问题。我应该提供其他信息吗? – Jasper 2015-03-13 15:59:06

回答

1

为了使position_dodge工作,需要有一个闪避的理由。那就是你需要适当改变ann_textvariable = c("LF", "HF"),这样才有道理闪避。然后,恰当地定义标签。下面我假设你只想要栏上的*

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)