简体   繁体   中英

how to make a stacked bar plot with one bar for many variables

my data "chir" has some variables look like this:

  • N1_re N2_re N3_re
  • yes no no
  • no yes no
  • na ye yes
  • no na no

all the variables contain like 100 values like yes, no and na, now I need a stacked bar plot with only one bar to show how many yes, no and na the variables have together, without to differentiate them. I tried to put them together first like this:

vars=c("N1_re", "N2_re", "N3_re")   

and then

barplot(matrix(c(table(chir$vars), ncol=1)))

which didn't work, and then I tried to melt these to variables first like this:

melt1=melt(chir, measure.vars=c("N1_re", "N2_re", "N3_re"), var="zpd") 

but it says " arguments imply differing number of rows: 98, 196"

maybe there is another way to make the plot without to melt?

thanks for any help!

Using your data for chir (correcting for typos):

data <- rbind(chir,chir,chir,chir)      # just to have a bigger dataset

library(ggplot2)
library(reshape2)

ggdata = melt(chir,measure.vars=1:3)
ggplot(ggdata, aes(x=variable)) + stat_bin(aes(fill=value))

gives:

ggplot(ggdata, aes(x=1)) + stat_bin(aes(fill=value))

gives:

Is this what you want??

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