6
votes

I am trying to generate a barplot such that the x-axes is by patient with each patient having multiple samples. So for instance (using the mtcars data as a template of what the data would look like):

library("ggplot2")
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear))) +
   geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
   xlab("Patient") +
   ylab("Number of Mutations per Patient Sample")

This would produce something like this:

enter image description here

With each barplot representing a sample in each patient.

I want to add additional information about each patient sample by using colors to fill the barplots (e.g. different types of mutations in each patient sample). I was thinking I could specify the fill parameter like this:

ggplot(mtcars, aes(x = factor(cyl), group = factor(gear), fill = factor(vs))) +
   geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
   xlab("Patient") +
   ylab("Number of Mutations per Patient Sample")

But this doesn't produce "stacked barplots" for each patient sample barplot. I am assuming this is because the position_dodge() is set. Is there anyway to get around this? Basically, what I want is:

ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
   geom_bar() +
   xlab("Patient") +
   ylab("Number of Mutations per Patient Sample")

enter image description here

But with these colors available in the first plot I listed. Is this possible with ggplot2?

4
Possible duplicateaosmith
@aosmith You are right. This is pretty much the same question except I am making using of the "group" parameter. But the problem listed with the inability to use both stack and dodge at the same time applies to both questions.TinyHeero

4 Answers

9
votes

I think facets are the closest approximation to what you seem to be looking for:

ggplot(mtcars, aes(x = factor(gear), fill = factor(vs))) +
    geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
    xlab("Patient") +
    ylab("Number of Mutations per Patient Sample") +
    facet_wrap(~cyl)

result of plot

I haven't found anything related in the issue tracker of ggplot2.

1
votes

If I understand your question correctly, you want to pass in aes() into your geom_bar layer. This will allow you to pass a fill aesthetic. You can then place your bars as "dodge" or "fill" depending on how you want to display the data.

A short example is listed here:

   ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
      geom_bar(aes(fill = factor(vs)), position = "dodge", binwidth = 25) +
      xlab("Patient") +
      ylab("Number of Mutations per Patient Sample")

With the resulting plot: http://imgur.com/ApUJ4p2 (sorry S/O won't let me post images yet)

Hope that helps!

1
votes

I have hacked around this a few times by layering multiple geom_cols on top of each other in the order I prefer. For example, the code

    ggplot(data, aes(x=cert, y=pct, fill=Party, group=Treatment, shape=Treatment)) +
      geom_col(aes(x=cert, y=1), position=position_dodge(width=.9), fill="gray90") +
      geom_col(position=position_dodge(width=.9)) +
      scale_fill_manual(values=c("gray90", "gray60"))

Allowed me to produce the feature you're looking for without faceting. Notice how I set the background layer's y value to 1. To add more layers, you can just cumulatively sum your variables.

Image of the plot:

plot

0
votes

I guess, my answer in this post will help you to build the chart with multiple stacked vertical bars for each patient ...

Layered axes in ggplot?