Ggplot2: geom_bar with group, position_dodge and fill

I am trying to create a bar bar so that the x-axis is patients with each patient having multiple samples. So, for example, (using mtcars data as a template for how the data will look):

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 will create something like this:

enter image description here

With each line drawing representing the sample of each patient.

I want to add additional information about each patient sample, using colors to fill the haircuts (for example, different types of mutations in each patient sample). I thought I could specify the fill parameter as follows:

 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 does not create "stacked haircuts" for each stroke pattern of the patient. I assume this is due to setting position_dodge (). Is there any way around this? Basically, I want:

 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 these colors are available in the first plot that I listed. Is this possible with ggplot2?

+4
source share
4 answers

I think the edges 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) 

plot result

I did not find anything related to the ggplot2 tracker.

+5
source

I hacked this several times, breaking several geometers into 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")) 

Let me create the function you are looking for without cutting. Notice how I set the background layer y to 1. To add more layers, you can simply sum the sums of the variables.

Graphic Image:

plot

+1
source

If I understand your question correctly, you want to go to aes () in your geom_bar layer. This will allow you to go through the aesthetics of filling. Then you can place your bars as β€œdodge” or β€œfill” depending on how you want to display the data.

The following is a short example:

  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 received schedule: http://imgur.com/ApUJ4p2 (sorry, S / O does not allow me to send images yet)

Hope this helps!

0
source

I think my answer in this post will help you build a chart with several glass vertical bars for each patient ...

Layered axes in ggplot?

0
source

All Articles