ggplot2 geom_bar position =“dodge”不闪避 [英] ggplot2 geom_bar position = "dodge" does not dodge

查看:505
本文介绍了ggplot2 geom_bar position =“dodge”不闪避的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的数据框p3:

 测试结果
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

我正在尝试使用代码制作闪回结果的条形图:

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

但它根本不起作用。我不明白为什么它不起作用,因为我以前使用过相同的代码并且工作。

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')

调整变量:


$ b $

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

除了颜色(轮廓)应该是相同的。但它足够接近。

I have the dataframe p3 below:

         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.

解决方案

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.

这篇关于ggplot2 geom_bar position =“dodge”不闪避的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆