简体   繁体   中英

Grouped bar chart turns into stacked bar chart ggplot

I have an issue where a grouped bar chart made with ggplot unintentionally transforms into a stacked bar chart after minor changes in my dataset. I have the following initial dataset and bar chart:

> DataT
    Treatment Round N  Initial        sd        se        ci
 1         EE     1 3 3000.000    0.0000   0.00000    0.0000
 2         EE     2 3 2524.444  463.8167 267.78469 1152.1845  
 3         EE     3 3 1621.481 1297.3331 749.01563 3222.7542
 4         EE     4 1 2720.000        NA        NA       NaN
 5         EU     1 3 3000.000    0.0000   0.00000    0.0000
 6         EU     2 3 2922.222  134.7151  77.77778  334.6508
 7         EU     3 3 2582.963  514.6560 297.13677 1278.4763
 8         EU     4 3 2163.951  547.9230 316.34352 1361.1163
 9         UE     1 3 3000.000    0.0000   0.00000    0.0000
 10        UE     2 3 2280.000  624.4286 360.51404 1551.1667
 11        UE     3 3 1244.444 1520.4596 877.83779 3777.0311
 12        UE     4 1 2686.667        NA        NA       NaN
 13        UU     1 3 3000.000    0.0000   0.00000    0.0000
 14        UU     2 3 2626.667  283.7840 163.84274  704.9584
 15        UU     3 3 1835.556  474.8957 274.18116 1179.7063
 16        UU     4 3 1287.407 1022.8653 590.55158 2540.9384
 > p <-ggplot(DataT, aes(x=Treatment, y=Initial))
 > p +geom_bar(position = position_dodge(),stat = "identity", aes(fill = 
 Round))

第一条形图

However, this dataset had a few errors. After changing the dataset to the following, my bar chart style changed from a grouped to a stacked chart.

> DataNew
   Treatment Round N   Initial        sd        se        ci
1         EE     1 3 3000.0000    0.0000   0.00000    0.0000
2         EE     2 3 2524.4444  463.8167 267.78469 1152.1845
3         EE     3 3 1621.4815 1297.3331 749.01563 3222.7542
4         EE     4 3  906.6667 1570.3927 906.66667 3901.0718
5         EU     1 3 3000.0000    0.0000   0.00000    0.0000
6         EU     2 3 2922.2222  134.7151  77.77778  334.6508
7         EU     3 3 2582.9630  514.6560 297.13677 1278.4763
8         EU     4 3 2163.9506  547.9230 316.34352 1361.1163
9         UE     1 3 3000.0000    0.0000   0.00000    0.0000
10        UE     2 3 2280.0000  624.4286 360.51404 1551.1667
11        UE     3 3 1244.4444 1520.4596 877.83779 3777.0311
12        UE     4 3  895.5556 1551.1477 895.55556 3853.2646
13        UU     1 3 3000.0000    0.0000   0.00000    0.0000
14        UU     2 3 2626.6667  283.7840 163.84274  704.9584
15        UU     3 3 1835.5556  474.8957 274.18116 1179.7063
16        UU     4 3 1287.4074 1022.8653 590.55158 2540.9384
> p <-ggplot(DataNew, aes(x=Treatment, y=Initial))
> p +geom_bar(position = position_dodge(),stat = "identity", aes(fill = 
Round))

第二条形图

How do I keep the second chart as a grouped bar chart? My thanks in advance!

I'm going to guess that Round is not a factor in your data:

ggplot(DataNew, aes(x=Treatment, y=Initial)) +
  geom_bar(position="dodge",stat="identity",aes(fill=factor(Round)))

This works by wrapping Round with factor .

More completely, you should go back and change DataNew$Round <- as.factor(DataNew$Round) - this removes the need for factor in the ggplot call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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