Stacked bar plot in R with ratio line overplot

假如想象 提交于 2020-02-08 02:54:48

问题


I have data with one observation per row:

rm(list = ls(all = TRUE))
mydf <- data.frame(kind = sample(c("good", "bad"), 100, replace = TRUE), var1 = sample(c("yes", "no", "yes"), 100, replace = TRUE), var2 = sample(c("yes", "no"), 100, replace = TRUE), var3 = sample(c( "yes", "no"), 100, replace = TRUE), var4 = sample(c( "yes", "no", "yes", "no", "NA"), 100, replace = TRUE), var5 = sample(c( "yes", "no", "yes", "no", "NA"), 100, replace = TRUE), var6 = sample(c( "yes", "no", "yes", "no", "NA"), 100, replace = TRUE))

I need to: make a stacked bar chart with side-by-side bar pairs, one bar for each kind (good vs bad), showing the count of how many of each kind have 0 "yes" vars, how many have 1 "yes" var, etc., up to "yes" for all 6 vars. Y-axis = count, X-axis = the seven categories (0 yes vars, 1 yes var, etc). Each bar should be a stacked bar color-coded showing the contribution of each var to the total height of the bar. NAs are treated as "no". Also, overplot line showing the ratio of count(good)/count(bad) for each of the seven X-axis categories


回答1:


Based on your description, here's what I understand what you're trying to achieve. It consists of three steps:

  1. Replace all NA's with "no".
  2. Add up all the "yes" in a row-wise manner.
  3. Actually plotting the graph.

So address each point.

Lets assume that your data is as follows:

mydf <- data.frame(kind = sample(c("good", "bad"), 100, replace = TRUE), 
                   var1 = sample(c("yes", "no", "yes"), 100, replace = TRUE), 
                   var2 = sample(c("yes", "no"), 100, replace = TRUE), 
                   var3 = sample(c( "yes", "no"), 100, replace = TRUE), 
                   var4 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE), 
                   var5 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE), 
                   var6 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE))

1

To replace all NA's with "no" would simply be:

mydf[is.na(mydf)] <- "no"

here we are searching through the data.frame and replace all na with no's using the assignment operator.

2

To add everything in a row-wise manner I used the apply function. Within the apply function you can use ?apply to determine the arguments, but in a nutshell, you (1st arg) simply specify the data.frame, (2nd arg) specify the direction, 1, for row-wise and 2 for column-wise, (3rd arg) specify the function you wish to apply to the direction.

mydf$total.yes <- apply(mydf, 1, function(x) {
  return(length(x[x=="yes"]))
})

3

Lastly the plot. The easiest and aesthetic way to produce plot is to use ggplot. Install it by typeing install.packages("ggplot2"). For the bar plots I will refer to this [documentation](here: http://docs.ggplot2.org/0.9.3.1/geom_bar.html), otherwise the code would look like the following.

library(ggplot2)

ggplot(mydf, aes(total.yes, fill=kind)) +
  geom_bar(position="dodge")

which will produce the plot below:

I hope this answers the questions you were after. The full code is as follows:

mydf <- data.frame(kind = sample(c("good", "bad"), 100, replace = TRUE), 
                   var1 = sample(c("yes", "no", "yes"), 100, replace = TRUE), 
                   var2 = sample(c("yes", "no"), 100, replace = TRUE), 
                   var3 = sample(c( "yes", "no"), 100, replace = TRUE), 
                   var4 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE), 
                   var5 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE), 
                   var6 = sample(c( "yes", "no", "yes", "no", NA), 100, replace = TRUE))

library(ggplot2)

# replace all NA values to no, this step seems redundant because you're only 
# counting yes's
mydf[is.na(mydf)] <- "no"

# for each row figure out how many "yes" there are...
mydf$total.yes <- apply(mydf, 1, function(x) {
  return(length(x[x=="yes"]))
})

# see example here: http://docs.ggplot2.org/0.9.3.1/geom_bar.html
#using your data


ggplot(mydf, aes(total.yes, fill=kind)) +
  geom_bar(position="dodge")

geom_bar is actually stacked by default, (see [documentation](here: http://docs.ggplot2.org/0.9.3.1/geom_bar.html), if it is stacked it will look something like the following:

ggplot(mydf, aes(total.yes, fill=kind)) +
  geom_bar()



来源:https://stackoverflow.com/questions/30282990/stacked-bar-plot-in-r-with-ratio-line-overplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!