Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

r - Order and color of bars in ggplot2 barplot

I have a very annoying problem with a stacked bar plot created using ggplot2. There are a couple of similar questions previously asked but after going through the example code I cannot figure out what I am doing wrong.

I would like to make the graph so that the bars are stacked in the following order based on their Biogeographic.affinity: (Top to Bottom= Bassian,Widespread,Torresian, and Eyrean). The colors to the bars should be: (Bassian=drakgrey, Widespread=lightgrey, Torresian=white, and Eyrean=black).

This is what the dataset looks like:

  biogeo
       Site Biogeographic.affinity Rank Number.of.species Total.Species    Percent
    1     A                Bassian    1                 1           121  0.8264463
    2     A                 Eyrean    4                39           121 32.2314050
    3     A              Torresian    3                62           121 51.2396694
    4     A             Widespread    2                19           121 15.7024793
    5    DD                Bassian    1                 1           128  0.7812500
    6    DD                 Eyrean    4                46           128 35.9375000
    7    DD              Torresian    3                63           128 49.2187500
    8    DD             Widespread    2                18           128 14.0625000
    9   E_W                Bassian    1                 1           136  0.7352941
    10  E_W                 Eyrean    4                54           136 39.7058824
    11  E_W              Torresian    3                65           136 47.7941176
    12  E_W             Widespread    2                16           136 11.7647059
    13   KS                Bassian    1                 2           145  1.3793103
    14   KS                 Eyrean    4                63           145 43.4482759
    15   KS              Torresian    3                62           145 42.7586207
    16   KS             Widespread    2                18           145 12.4137931
    17 Z_Ka                Bassian    1                 1           110  0.9090909
    18 Z_Ka                 Eyrean    4                64           110 58.1818182
    19 Z_Ka              Torresian    3                31           110 28.1818182
    20 Z_Ka             Widespread    2                14           110 12.7272727

This is the code I have written so far (including some of my failed attempts to correct the problem).

 ggplot(data=biogeo, aes(x=Site, y=Percent, fill=Biogeographic.affinity)) + geom_bar(stat="identity", colour="black")+ 
  scale_fill_grey() + ylab("Percent") + xlab("Location") +       
  theme_bw()+ theme(panel.grid.minor = element_blank()) 

This gives the basic graph but the colors and order are still wrong. To correct the order I tried, but that did not change anything (FRUSTRATED)!:

newone <- transform(biogeo, Biogeographic.affinity  = factor(Biogeographic.affinity ), Rank = factor(Rank, levels = 1:4))

As for color changing I have tried and seems to work but it all looks like the order is still wrong!

cols<- c("Bassian"="darkgrey","Widespread"="lightgrey", "Torresian"="white", "Eyrean"="black") #designates the colors of the bars 
ggplot(data=newone, aes(x=Site, y=Percent, fill=Biogeographic.affinity)) + geom_bar(stat="identity", colour="black")+ 
  scale_fill_manual(values = cols) + ylab("Percent") + xlab("Location") +       
  theme_bw()+ theme(panel.grid.minor = element_blank()) 

please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The order that bars are drawn (bottom to top) in a stacked barplot in ggplot2 is based on the ordering of the factor which defines the groups. So the Biogeographic.affinity factor must be reordered. Generally we use reorder(if we want to order the factor according to a continuous levels) but here I will just create a new ordered factor similar to what you tried to do.

 biogeo <- transform(biogeo, 
                Biog.aff.ord  = factor(
                     Biogeographic.affinity ,
                     levels=c( 'Bassian','Widespread','Torresian', 'Eyrean'),
                     ordered =TRUE))

Now if you fill your barplot using Biog.aff.ord rather than the original factor and overriding the default grouping order by defining aes_group_order as Biog.aff.ord order you get the expected result:

cols <- c(Bassian="darkgrey",Widespread="lightgrey", 
            Torresian="white", Eyrean="black") 
ggplot(data=biogeo, aes(x=Site, y=Percent,
       order=Biog.aff.ord)) +   ##!! aes_group_order
  geom_bar(stat="identity", colour="black",
        aes(fill=Biog.aff.ord)) +
  scale_fill_manual(values = cols) 

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...