Skip to content

stacked and dodged bar graph ggplot

4 messages · Robert Lynch, Richard M. Heiberger, Ulrik Stervbo +1 more

#
I have some census data with race and ethnicity for various towns.  I am
trying to make a stacked bar graph where all the race data is in one
stacked bar, and all the ethnicity data is in another.

Below is a minimal reproducible sample.

library("ggplot2")

Demog <-
data.frame(source=c(rep("Davis",4),rep("Dixon",4),rep("Winters",4)),
            group =c("Asian / Pacific Islander","Caucasian","Lantinx","Not
Latinx","African American", "Native American", "Latinx", "Not
Latinx","Mixed race","Other","Latinx", "Not Latinx"),
            number =c(14491, 42571, 8172, 57450, 562, 184, 7426, 10952,
332, 1488, 3469, 3155),
            field = rep(c(rep("race",2),rep("ethnicity",2)),3))

Demog$race <- factor(Demog$group, levels=c("Asian / Pacific Islander",
"Caucasian", "African American", "Native American / Alaska Native",  "mixed
race",  "other"))
Demog$ethn <- factor(Demog$group, levels=c("Latinx","not latinx"))
Demog$location <- factor(Demog$source, levels=c( "Dixon",
"Winters","Davis"))
Demog.bar1 <-ggplot(data = Demog, aes(x = location, y = number, fill =
race))+theme_bw() +geom_bar(stat = "identity",position = "stack") +
coord_flip()

Demog.bar2 <-ggplot(data = Demog, aes(x = location, y = number, fill =
ethn))+theme_bw() +geom_bar(stat = "identity",position = "stack") +
coord_flip()

show(Demog.bar1)
show(Demog.bar2)




Much thanks,
Robert
#
I reproduced your graphs, but I don't understand what you want instead.

There are several problems.
  one group is spelled "Lantinx".
  your factor statements mostly lead to NA values.
  The two panels of the plot do not use the same number of inches of
the plotting window, due to different widths of the legends.
On Fri, Dec 30, 2016 at 1:30 PM, Robert Lynch <robert.b.lynch at gmail.com> wrote:
#
You can use the tidyr package to combine race and enth into one column and
fill by that. Then you get one colour for race and one for enth.

HTH
Ulrik
On Fri, 30 Dec 2016, 20:12 Richard M. Heiberger, <rmh at temple.edu> wrote:

            

  
  
#
Hi Robert,
I realize that this isn't in ggplot, but are you looking for something
like this?

Demog <-
data.frame(source=c(rep("Davis",4),rep("Dixon",4),rep("Winters",4)),
 group =c("Asian / Pacific Islander","Caucasian","Latinx",
 "Not Latinx","African American", "Native American", "Latinx",
 "Not Latinx","Mixed race","Other","Latinx", "Not Latinx"),
 number =c(14491, 42571, 8172, 57450, 562, 184, 7426, 10952,
 332, 1488, 3469, 3155),
 field = rep(c(rep("race",2),rep("ethnicity",2)),3))
library(plotrix)
barpos<-barp(matrix(Demog$number,ncol=3),
 col=matrix(c("lightsalmon","ivory","brown","blanchedalmond","chocolate",
 "orangered","brown","blanchedalmond","khaki","purple","brown",
 "blanchedalmond"),ncol=3),ylab="Frequency",
 names.arg=c("Davis","Dixon","Winters"))
legend(2,50000,c("Asian/Pacific Is.","Caucasian","Latinx","NonLatinx",
 "African American","Native American","Mixed race","Other"),
 fill=c("lightsalmon","ivory","brown","blanchedalmond","chocolate",
 "orangered","khaki","purple"))
mtext(rep(c("race","ethnicity"),3),side=1,line=0.3,
 at=barpos$x[c(1,4),])

Jim
On Sat, Dec 31, 2016 at 5:30 AM, Robert Lynch <robert.b.lynch at gmail.com> wrote: