2015-03-13 54 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)