简体   繁体   English

ggplot2 geom_bar position =“dodge”不闪避

[英]ggplot2 geom_bar position = “dodge” does not dodge

I have the dataframe p3 below: 我有以下数据框p3:

         test     result
    1      1    26.87778
    2      1    24.52598
    3      1    24.02202
    4      1    20.32632
    5      1    22.00618
    6      2    19.84013
    7      2    19.68983
    8      2    19.84013
    9      2    19.23892
    10     2    19.23892
    11     3    34.36430
    12     3    33.28196
    13     3    33.82313
    14     3    33.82313
    15     3    32.47020
    16     4    25.55169
    17     4    26.90442
    18     4    25.40138
    19     4    24.19895
    20     4    25.85230
    21     4    25.70199
    22     4    24.95047
    23     5    18.64646
    24     5    18.64646
    25     5    17.80653
    26     5    18.64646
    27     5    18.31049

I am trying to make a barchart with dodged results using the code: 我正在尝试使用代码制作带有躲闪结果的条形图:

    ggplot(p3, aes(x = test, y = result))+ geom_bar(position="dodge", stat="identity")

but it doesn't work at all. 但它根本不起作用。 I don't understand why it is not working since I used the same code before and it worked. 我不明白为什么它不起作用,因为我以前使用相同的代码并且它起作用。

ggplot(p3, aes(x = test, y = result, group = result)) + 
    geom_bar(position="dodge", stat="identity")

you can see what is happening if you change the group argument to color . 如果将group参数更改为color则可以看到发生的情况。

ggplot(p3, aes(x = test, y = result, color = result)) + 
    geom_bar(position="dodge", stat="identity")

Edited for comments: 编辑评论:

It looks like there are odd numbers of groups because there are. 看起来有奇数组,因为有。 Group 4 has 7 elements in it in the data you supplied. 第4组在您提供的数据中包含7个元素。 group 3 has 5 but 2 of them are identical. 第3组有5个,但其中2个是相同的。 The plot shows the height correctly and is grouping like elements together. 该图正确显示高度,并将元素组合在一起。 its like you called unique on each group. 就像你在每个小组中称之为unique

I think plotting: 我认为密谋:

ggplot(p3, aes(x=test, y=result, group=result, color=result)) + 
  geom_bar(position='dodge', stat='identity')

displays this quite well. 很好地展示了这一点。 As far as each group having 5 elements, that isn't the case. 至于每个组有5个元素,情况并非如此。 Group 4 has 7. To see what you're describing you could do something like: 第4组有7.要查看您所描述的内容,您可以执行以下操作:

ggplot(p3, aes(x=as.integer(row.names(p3)), y=result, fill=factor(test))) +   
  geom_bar(position='dodge', stat='identity')

This was answered by Dennis Murphy: 丹尼斯墨菲回答了这个问题:

p3$test <- factor(p3$test)
p3$fac <- factor(unlist(sapply(as.vector(table(p3$test)), seq_len)))
ggplot(p3, aes(x = test, y = result, fill = fac)) + 
      geom_bar(position = 'dodge', stat = 'identity')

Adjusting the variables: 调整变量:

ggplot(p3, aes(x = test, y = result, color = fac, fill = test)) +
    geom_bar(position = 'dodge', stat = 'identity', linetype = 0)

I almost got what I wanted, except that the color (outline) should be the same. 我几乎得到了我想要的东西,除了颜色(轮廓)应该是相同的。 but it was close enough. 但它足够接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM