2016-05-20 9 views
1

だから私は、このデータフレーム使用しています:ggplot2:手段(stat_summary)を示す線とポイントを追加

xym <- data.frame(
    Var1 = c("vloga", "odločitve", "dolgoročno", 
     "krizno", "uživa v", "vloga", "odločitve", 
     "dolgoročno", "krizno", "uživa v", "vloga", 
     "odločitve","dolgoročno", "krizno", "uživa v", 
     "vloga","odločitve", "dolgoročno", "krizno", 
     "uživa v"), 
    Var2 = c("Nad","Nad", "Nad", "Nad", "Nad", "Pod", 
     "Pod", "Pod", "Pod", "Pod", "Enak","Enak", 
     "Enak", "Enak", "Enak", "Sam.", "Sam.", "Sam.", 
     "Sam.", "Sam."), 
    value = c(4, 3, 4, 4, 3, 3, 3, 2, 3, 3, 3, 2.5, 2.5, 
      2, 3.5 ,5 ,6 ,6 ,5 ,6)) 

そして、このコードで:

p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) + coord_flip()+ 
    theme_bw() + scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) + xlim(rev(levels(xym$Var1)))+ theme(axis.title=element_blank(),axis.ticks.y=element_blank(),legend.position = "bottom", 
                                    axis.text.x = element_text(angle = 0,vjust = 0.4)) + 
    geom_bar(stat = "identity", width = 0.7, position = position_dodge(width=0.7)) + 
    geom_text(aes(x = Var1, y =max(value), label = round(value, 2), fill = Var2), 
      angle = 0, position = position_dodge(width = 0.7), size = 4.2) 
p + labs(fill="") 

p + stat_summary(fun.y=mean, colour="red", geom="line", aes(group = 1)) 

を私は出力を生成:

enter image description here

しかし、赤線 whiの横にchは問題の合計(すなわち、 「dolgoročno」、「krizno」など)私は

私の出力は(、下図のようになるはずです意味ポイントやバーの横を追加するだけでなく、ラベルの個別質問・グループのしたいと思います私は塗料でそれをしました)、黒い点が私の望ましい点を表し、最初の点の値3.6は(6,2,4,2.5)の平均であり、私の希望する値のラベルを表します。

enter image description here

私も見てきました:

Plot average line in a facet_wrap

ggplot2: line connecting the means of grouped data

How to label graph with the mean of the values using ggplot2

答えて

3

1つのオプションは、次のようになります。私はあなたのコードに沿っていくつかの行を追加しました。

# Your code 
p <- ggplot(xym, aes(x = Var1, y = value, fill = Var2)) + 
    coord_flip() + 
    theme_bw() + 
    scale_fill_manual(values = c("yellow", "deepskyblue1", "yellowgreen","orchid4")) + 
    xlim(rev(levels(xym$Var1))) + 
    theme(axis.title = element_blank(), 
      axis.ticks.y = element_blank(), 
      legend.position = "bottom", 
      axis.text.x = element_text(angle = 0,vjust = 0.4)) + 
    geom_bar(stat = "identity", width = 0.7, position = position_dodge(width = 0.7)) + 
    geom_text(aes(x = Var1, y = max(value), label = round(value, 2), fill = Var2), 
       angle = 0, position = position_dodge(width = 0.7), size = 4.2) 

p + labs(fill = "") 

次に、次のコードを追加しました。 geomを変更するドットをstat_summaryのpointに追加することができます。ラベルの場合は、ggplot_build()からデータを取得し、fooというデータフレームを作成しました。 (私は同じ仕事をする他の方法があると思う。)fooを使って、最後に注釈を追加した。完璧に動作

p2 <- p + 
     stat_summary(fun.y = mean, color = "red", geom = "line", aes(group = 1)) + 
     stat_summary(fun.y = mean, color = "black", geom ="point", aes(group = 1), size = 5, 
        show.legend = FALSE) 

# This is the data for your dots in the graph 
foo <- as.data.frame(ggplot_build(p2)$data[[4]]) 

p2 + 
annotate("text", x = foo$x, y = foo$y + 0.5, color = "black", label = foo$y) 

enter image description here

+0

。 – Miha

+0

@ミハ助けてくれてありがとう。 – jazzurro

関連する問題