来自g_splot2()中stat_summary()的数据点的x轴位置

时间:2016-01-11 13:52:28

标签: r ggplot2

我想将子组的方法放在我的箱图上方,但是找不到在x轴上正确定位它们的方法。使用我当前的代码,平均值的符号都放在顶级组的x轴位置。

这是我的数据,以及下面的ggplot2()代码:

cc <- structure(list(Individuum = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 76L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 
73L, 74L, 75L, 77L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 76L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 
62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 
75L, 77L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 76L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L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0.538461538461538, 0.461538461538462, 
    0.769230769230769, 0.153846153846154, 0.230769230769231, 
    0.461538461538462, 0, 0.230769230769231, 0.153846153846154, 
    0, 0.769230769230769, 0.461538461538462, 0.692307692307692, 
    0, 0.230769230769231, 0.307692307692308, 0.692307692307692, 
    0.307692307692308, 0.230769230769231, 0.615384615384615, 
    0.615384615384615, 0.538461538461538, 0, 0.461538461538462, 
    0, 0.538461538461538, 0.538461538461538, 0.538461538461538, 
    0, 0.538461538461538, 0.0769230769230769, 0.692307692307692, 
    0.384615384615385, 0, 0.384615384615385, 0.461538461538462, 
    0.923076923076923, 0.384615384615385, 0.307692307692308, 
    0, 1, 0.461538461538462, 0.307692307692308, 0.153846153846154, 
    0.230769230769231, 0.692307692307692, 0, 0, 0, 0.615384615384615, 
    0.384615384615385, 0, 0.384615384615385, 0.384615384615385, 
    0.384615384615385, 0.461538461538462, 0.307692307692308, 
    0.384615384615385, 0.384615384615385, 0.153846153846154, 
    0.538461538461538, 0.153846153846154, 0.307692307692308, 
    0, 0.461538461538462, 0.615384615384615, 0, 0, 0.307692307692308, 
    0.307692307692308, 0.307692307692308, 0, 0, 0.538461538461538, 
    0.307692307692308, 0.214285714285714, 0.142857142857143, 
    0.357142857142857, 0.214285714285714, 0.785714285714286, 
    0.0714285714285714, 0.0714285714285714, 0.142857142857143, 
    0, 0, 0, 0, 0.5, 0, 0.571428571428571, 0, 0, 0.285714285714286, 
    0.142857142857143, 0.357142857142857, 0.0714285714285714, 
    0.357142857142857, 0.285714285714286, 0.142857142857143, 
    0, 0.357142857142857, 0, 0.285714285714286, 0.428571428571429, 
    0.357142857142857, 0, 0, 0.142857142857143, 0, 0.571428571428571, 
    0, 0.214285714285714, 0.357142857142857, 0.928571428571429, 
    0.214285714285714, 0.285714285714286, 0, 1, 0.285714285714286, 
    0.285714285714286, 0.0714285714285714, 0.214285714285714, 
    0.214285714285714, 0, 0, 0, 0.285714285714286, 0, 0, 0.357142857142857, 
    0.285714285714286, 0, 0.571428571428571, 0.428571428571429, 
    0.357142857142857, 0, 0.0714285714285714, 0.428571428571429, 
    0, 0.285714285714286, 0, 0.428571428571429, 0.714285714285714, 
    0, 0, 0.285714285714286, 0.214285714285714, 0.142857142857143, 
    0, 0, 0.5, 0.142857142857143, 0.2, 0.3, 0.4, 0.7, 0.7, 0.2, 
    0.2, 0.1, 0, 0.1, 0.1, 0, 0.5, 0, 0.4, 0, 0.3, 0.1, 0.4, 
    0.3, 0, 0.5, 0.7, 0, 0, 0.3, 0, 0.2, 0.4, 0.5, 0, 0.2, 0.1, 
    0, 0.3, 0, 0.3, 0, 0.7, 0.3, 0.2, 0, 1, 0.5, 0.3, 0, 0.2, 
    0.4, 0, 0, 0, 0.7, 0, 0, 0.4, 0.1, 0, 0.3, 0.3, 0.5, 0.2, 
    0.2, 0.4, 0, 0.3, 0, 0.5, 0.5, 0, 0, 0.4, 0, 0.1, 0, 0, 0.8, 
    0)), .Names = c("Individuum", "Fachgruppe", "Kategorie", 
"Antwort"), row.names = c(NA, -231L), class = "data.frame")

代码:

p_cc <- ggplot(cc, aes(x = Fachgruppe, y = Antwort, fill = Kategorie)) +
           geom_boxplot(outlier.size=0) + 
         stat_summary(fun.y=mean, colour="darkred", geom="point", 
           shape=16, size=2) + 
           labs(y = "Mittlerer Anteil\nbekannter Themen")

1 个答案:

答案 0 :(得分:1)

通过@MLavoie整理评论:使用position=position_dodge(0.75)

p_cc <- ggplot(cc, aes(x = Fachgruppe, y = Antwort, fill = Kategorie)) +
  geom_boxplot(outlier.size=0) + 
  stat_summary(fun.y=mean, colour="darkred", geom="point", 
           shape=16, size=2, position=position_dodge(0.75)) + 
  labs(y = "Mittlerer Anteil\nbekannter Themen")

enter image description here