2017-02-25 10 views
6

私はggplotのプロットを持っています。私はジッタのある点に相対的なエラーバーをシフトする必要があります。私のコードは次のとおりです。2つのギオムのx軸の位置をどのようにずらすのですか

data("cabbages", package = "MASS") 

require("ggplot2") 

pos_1 <- position_jitterdodge(
    jitter.width = 0.25, 
    jitter.height = 0, 
    dodge.width = 0.9 
) 

gg <- 
    ggplot(data = cabbages, 
      aes(
       x  = Cult, 
       y  = HeadWt, 
       colour = Cult, 
       fill = Cult 
       )) + 

    geom_jitter(alpha = 0.4, position = pos_1) + 

    stat_summary(fun.y = "mean", geom = "point", size = 3) + 

    stat_summary(fun.data = "mean_cl_normal", 
       geom = "errorbar", 
       width = 0.05, 
       lwd = 1, 
       fun.args = list(conf.int = 0.95) 
) + 

    theme_bw() 

print(gg) 

現在の結果は次のとおりです。

enter image description here

そして、私はこのような何か必要があります:あなたがでaesxにオフセットを追加することがあり

enter image description here

答えて

5

を各stat_summaryaes(x = as.numeric(Cult) + 0.2)):

ggplot(data = cabbages, 
     aes(x = Cult, 
      y  = HeadWt, 
      colour = Cult, 
      fill = Cult)) + 
    geom_jitter(alpha = 0.4, position = pos_1) + 
    stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.y = "mean", geom = "point", size = 3) + 
    stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.data = "mean_cl_normal", 
       geom = "errorbar", 
       width = 0.05, 
       lwd = 1, 
       fun.args = list(conf.int = 0.95)) + 
    theme_bw() 

enter image description here

+0

プロットを構築するには、あなたが 'X = as.numeric(因子)と' geom'を追加_before_カテゴリx軸と 'geom'が必要となりますことをご注意0.1 '。あなたが追加する最初の 'geom'はx軸タイプを設定するので、' x = as.numeric(factor)+ 0.1'を最初に 'geom'を追加すると、x軸は連続していて、最初に 'as.numeric()'を使用して変換しない限り、因子軸を連続軸に追加しないでください。 – filups21

関連する問題