Stacked overlay bar with unmodified data

I want to make a stacked data column unchanged. I mean, I've already calculated the percentages for the plot. According to the ggplot2 manual, "geom_col uses stat_identity: it leaves the data as is." However, it doesn't seem to work as the percentages of the plot differ from the percentages of the sampled data.

Download sample data from here .

The code looks like this:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
        scale_fill_manual(values=c("#005588", "#E69F00")) +                                                             
        #stat_identity(geom="bar", width=0.5) +                                                                                                                    
        geom_col(width=0.5) +
        #geom_bar(stat="identity", width=0.5) +
        facet_grid(. ~ Timepoint) +
        coord_flip() +
        theme_bw()

      

geom_col changes the order of data and rows

On the other hand, if I use the stat_identity option, the data stays the same (compare the percentages from both images with the sample data), but the line plots no longer fit.

stat_identity does not touch data, but loses stacked columns.

Is the "geom_col" parameter not working or am I doing something wrong? Should I use a different plotting method? Any help is appreciated.

dput:

structure(list(Pathway = c("Antigen Presentation Pathway", "Graft-versus-  Host Disease Signaling", 
"T Helper Cell Differentiation", "Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
 "Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells"), Timepoint = c("15DPI", 
"15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI", "30DPI", 
"30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI", "45DPI", 
"15DPI", "15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI", 
"30DPI", "30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI", 
"45DPI"), variable = c("Targets", "Targets", "Targets", "Targets", 
"Targets", "Targets", "Targets", "Targets", "Targets", "Targets", 
"Targets", "Targets", "Targets", "Targets", "Targets", "DEGs", 
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", 
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs"), value = c(2.63157894736842, 
4.16666666666667, 1.36986301369863, 3.125, 1.12359550561798, 
7.89473684210526, 18.75, 8.21917808219178, 18.75, 7.86516853932584, 
15.7894736842105, 16.6666666666667, 10.958904109589, 9.375, 8.98876404494382, 
44.7368421052632, 35.4166666666667, 43.8356164383562, 37.5, 31.4606741573034, 
47.3684210526316, 43.75, 42.4657534246575, 37.5, 33.7078651685393, 
52.6315789473684, 39.5833333333333, 39.7260273972603, 31.25, 31.4606741573034)), .Names = c("Pathway", "Timepoint", "variable", 
"value"), class = "data.frame", row.names = c(NA, -30L))

      

+3


source to share


1 answer


Given the discussion by you and Gregor in the comments above, it looks like you don't want the plots to stack on top of each other, but rather overlap. I believe this should work for you:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
  scale_fill_manual(values=c("#005588", "#E69F00")) +                                                             
  geom_col(width = 0.5, alpha = 0.5, position = "identity") +
  facet_grid(. ~ Timepoint) +
  coord_flip() +
  theme_bw()

      

enter image description here

I use position = "identity"

to make sure the columns don't add up. I also had to make transparent stripes with the help alpha = 0.5

so you can see them.




Another option, if you want them to be built side by side instead of stacking, is to use position = "dodge"

:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) +
  scale_fill_manual(values=c("#005588", "#E69F00")) +                                                             
  geom_col(width=0.5, position = "dodge") +
  facet_grid(. ~ Timepoint) +
  coord_flip() +
  theme_bw()

      

enter image description here

+4


source







All Articles