zahra abdi
zahra abdi

Reputation: 337

How to change length and width size of multiple bar plot in ggplot2?

I want to change the length size and width size of the multiple bar plot to have wider and shorter bars in ggplot2, but I don't know how to change the size of this plot, because in the original version of my data, there are 8 genes.

Here is my data.

  dput(df)
 structure(list(Gene.name = c("Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene3", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", "Gene4", 
"Gene4", "Gene4", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene6", 
"Gene6", "Gene6", "Gene6", "Gene6", "Gene6", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", "Gene5", 
"Gene5"), Cancer.Study = c("Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Esophageal Carcinoma", "Esophageal Carcinoma", "Esophageal Carcinoma", 
"Esophageal Carcinoma", "Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Stomach Adenocarcinoma", "Stomach Adenocarcinoma", "Stomach Adenocarcinoma", 
"Stomach Adenocarcinoma", "Breast Invasive Carcinoma", "Breast Invasive Carcinoma", 
"Breast Invasive Carcinoma", "Breast Invasive Carcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Lung Adenocarcinoma", "Lung Adenocarcinoma", 
"Lung Adenocarcinoma", "Lung Adenocarcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Glioblastoma Multiforme", 
"Glioblastoma Multiforme", "Glioblastoma Multiforme", "Glioblastoma Multiforme", 
"Prostate Adenocarcinoma", "Prostate Adenocarcinoma", "Prostate Adenocarcinoma", 
"Prostate Adenocarcinoma", "Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", 
"Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", "Thyroid carcinoma", 
"Thyroid carcinoma", "Thyroid carcinoma", "Thyroid carcinoma", 
"Esophageal Carcinoma", "Esophageal Carcinoma", "Esophageal Carcinoma", 
"Esophageal Carcinoma", "Liver Hepatocellular Carcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Liver Hepatocellular Carcinoma", 
"Stomach Adenocarcinoma", "Stomach Adenocarcinoma", "Stomach Adenocarcinoma", 
"Stomach Adenocarcinoma", "Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Lung Adenocarcinoma", "Lung Adenocarcinoma", "Lung Adenocarcinoma", 
"Lung Adenocarcinoma", "Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Breast Invasive Carcinoma", "Breast Invasive Carcinoma", "Breast Invasive Carcinoma", 
"Breast Invasive Carcinoma", "Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", 
"Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Glioblastoma Multiforme", 
"Glioblastoma Multiforme", "Glioblastoma Multiforme", "Glioblastoma Multiforme", 
"Prostate Adenocarcinoma", "Prostate Adenocarcinoma", "Prostate Adenocarcinoma", 
"Prostate Adenocarcinoma", "Thyroid carcinoma", "Thyroid carcinoma", 
"Thyroid carcinoma", "Thyroid carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Esophageal Carcinoma", 
"Esophageal Carcinoma", "Esophageal Carcinoma", "Esophageal Carcinoma", 
"Stomach Adenocarcinoma", "Stomach Adenocarcinoma", "Stomach Adenocarcinoma", 
"Stomach Adenocarcinoma", "Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Lung Adenocarcinoma", "Lung Adenocarcinoma", "Lung Adenocarcinoma", 
"Lung Adenocarcinoma", "Prostate Adenocarcinoma", "Prostate Adenocarcinoma", 
"Prostate Adenocarcinoma", "Prostate Adenocarcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Breast Invasive Carcinoma", 
"Breast Invasive Carcinoma", "Breast Invasive Carcinoma", "Breast Invasive Carcinoma", 
"Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", 
"Colorectal Adenocarcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Glioblastoma Multiforme", 
"Glioblastoma Multiforme", "Glioblastoma Multiforme", "Glioblastoma Multiforme", 
"Thyroid carcinoma", "Thyroid carcinoma", "Thyroid carcinoma", 
"Thyroid carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Uterine Corpus Endometrial Carcinoma", 
"Uterine Corpus Endometrial Carcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Lung Squamous Cell Carcinoma", 
"Lung Squamous Cell Carcinoma", "Esophageal Carcinoma", "Esophageal Carcinoma", 
"Esophageal Carcinoma", "Esophageal Carcinoma", "Bladder Urothelial Carcinoma", 
"Bladder Urothelial Carcinoma", "Bladder Urothelial Carcinoma", 
"Bladder Urothelial Carcinoma", "Lung Adenocarcinoma", "Lung Adenocarcinoma", 
"Lung Adenocarcinoma", "Lung Adenocarcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Liver Hepatocellular Carcinoma", 
"Liver Hepatocellular Carcinoma", "Prostate Adenocarcinoma", 
"Prostate Adenocarcinoma", "Prostate Adenocarcinoma", "Prostate Adenocarcinoma", 
"Stomach Adenocarcinoma", "Stomach Adenocarcinoma", "Stomach Adenocarcinoma", 
"Stomach Adenocarcinoma", "Breast Invasive Carcinoma", "Breast Invasive Carcinoma", 
"Breast Invasive Carcinoma", "Breast Invasive Carcinoma", "Colorectal Adenocarcinoma", 
"Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", "Colorectal Adenocarcinoma", 
"Glioblastoma Multiforme", "Glioblastoma Multiforme", "Glioblastoma Multiforme", 
"Glioblastoma Multiforme", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Kidney Renal Clear Cell Carcinoma", 
"Kidney Renal Clear Cell Carcinoma", "Thyroid Carcinoma", "Thyroid Carcinoma", 
"Thyroid Carcinoma", "Thyroid Carcinoma"), Alteration.Frequency = c(0, 
0, 6.986027944, 0.399201597, 0, 0, 1.621621622, 2.162162162, 
0, 0, 2.919708029, 0.729927007, 0, 0, 0.917431193, 2.201834862, 
0, 0, 0.836820084, 2.092050209, 0, 0, 1.364877161, 1.000909918, 
0, 0.265251989, 0.265251989, 1.326259947, 0, 0.19379845, 0.581395349, 
0.581395349, 0, 0, 0, 0.941619586, 0, 0, 0, 0, 0, 0.400801603, 
0.200400802, 0, 0, 0, 0, 0.323101777, 0, 0, 0, 0, 0, 0, 10.27027027, 
1.081081081, 0.265251989, 0, 5.835543767, 0.795755968, 0, 0, 
6.485355649, 0, 0, 0, 2.919708029, 0, 0, 0, 2.713178295, 0, 0, 
0, 2.594810379, 0, 0, 0, 0.917431193, 1.28440367, 0, 0.272975432, 
1.910828025, 0, 0, 0, 0.969305331, 0.161550889, 0, 0, 0.376647834, 
0.564971751, 0, 0, 0.841750842, 0, 0, 0.200400802, 0, 0.400801603, 
0, 0, 0, 0, 0, 0, 1.467889908, 0.917431193, 0, 0.540540541, 1.081081081, 
0.540540541, 0, 0.418410042, 0.627615063, 1.046025105, 0, 0, 
0.97323601, 0.486618005, 0, 0, 0.968992248, 0.387596899, 0, 0, 
1.002004008, 0.200400802, 0, 0, 0.598802395, 0.399201597, 0, 
0, 0, 0.941619586, 0, 0, 0.363967243, 0.454959054, 0, 0, 0.323101777, 
0.484652666, 0, 0, 0, 0.530503979, 0, 0, 0.168350168, 0.168350168, 
0, 0, 0, 0, 0, 0.183486239, 0.550458716, 4.220183486, 0, 0.199600798, 
2.395209581, 2.195608782, 0, 0.540540541, 0, 3.243243243, 0, 
1.216545012, 0.486618005, 1.216545012, 0, 0.775193798, 0.387596899, 
1.356589147, 0, 0.265251989, 0.265251989, 1.856763926, 0, 1.603206413, 
0.400801603, 0.200400802, 0, 0, 0, 2.092050209, 0, 0.454959054, 
0.727934486, 0.727934486, 0, 0.484652666, 0, 1.13085622, 0, 0.505050505, 
0.168350168, 0.673400673, 0, 0.188323917, 0, 0.564971751, 0, 
0, 0, 0.594059406), Alteration.Type = c("Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation", "Multiple alteration", 
"Deep deletion", "Amplification", "Point mutation")), row.names = c(NA, 
-208L), class = "data.frame")

and here is my script for plotting with ggplot2

ggplot(df,aes(y=Alteration.Frequency,x=Gene.name, fill=Gene.name)) + coord_flip()+
  geom_bar(position="dodge",stat = 'identity') +
  facet_grid(Cancer.Study ~ Alteration.Type, scales = "free_x", switch = 'y') + theme_bw() +
  theme(strip.text.y.left = element_text(angle = 0),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

This plot is the result.

enter image description here

Thanks for any help.

Upvotes: 0

Views: 67

Answers (1)

stefan
stefan

Reputation: 124213

If I understand you correctly you want the width of the bars to be set such that they fill the whole panel. This could be achieved by default via position_dodge which however requires to drop the obs with zero values from your data and for dodging to actually take place map a constant value on x e.g. factor(1).

However, I have not clue what you mean by shorter, perhaps simply drop scales="free_x"?

library(ggplot2)
library(dplyr)

df |> 
  filter(Alteration.Frequency > 0) |> 
  ggplot(aes(y = Alteration.Frequency, x = factor(1), fill = Gene.name)) +
  geom_col(position = position_dodge(preserve = "total")) +
  facet_grid(Cancer.Study ~ Alteration.Type, switch = "y", scales="free_x") +
  theme_bw() +
  theme(
    strip.text.y.left = element_text(angle = 0),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  coord_flip()

enter image description here

Upvotes: 1

Related Questions