Automatic scaling on subplots & only one row of subplots

Hey,

I would like to do two things with my following plot.

  1. only have one row of subplots. Using plot_layout(nrow = 1) does not solve my issue, as the nine subplots seems to be considered as one. If I set plot_layout(nrow = 2) then the nine subplots are squished in the top half of the plotting area.
  2. have automatic x axis scales for each subplot. Since Ligand A and F have very high Freq value, it is not possible to make out the difference between the Freq of C, D , G ,I ...

Thanks!

71ef16df-347d-481b-a424-b3677591d6bc

library(ggplot2)
library(tidyr)
library(dplyr)
library(stringr)
library(tidyverse)
library(patchwork)

Ligand<-c("A","A","A","B","B","B","C","C","C","D","D","D","E","E","E","F","F","F","G","G","G","H","H","H","I","I","I")
Element<-c("24","25","26","24","25","26","24","25","26","24","25","26","24","25","26","24","25","26","24","25","26","24","25","26","24","25","26")
Freq<-c(54300,21025,4026,0,0,0,105,548,64,0,45,97,0,0,0,51097,4166,154,12,224,548,0,0,0,45,20,8)
df1<-data.frame(Ligand,Element,Freq)

ggplot(df1, aes(x=Element, y=Freq))+
  geom_bar(fill="black",stat = "identity")+
  coord_flip()+
  facet_wrap(~Ligand)+
  geom_text(data=subset(df1,Freq != 0),aes(label = Freq), vjust= 0.5, hjust = "left",size = 2.5, colour = "black")+
  plot_layout(nrow = 1)+
theme(
  panel.background=element_blank(),
  panel.grid.major.x = element_blank(),
  panel.grid.major.y = element_blank(),
  axis.title.x=element_blank(),
  axis.text.x=element_blank(),
  axis.ticks.x=element_blank(),
  )

It seems possible to do this. See ggplot2 control number of panels per row when using facet?

To be honest, I think it is probably easier to subset your data, create 9 plots and glue them together with {patchwork}.

I believe the reason you are getting the squished plot when you use plot_layout(nrow = 2) is because it is a single plot.

Indeed it solves both issues. I could probably have done it with a for loop or something but its fine like this. However the bar labels with high values are hidden even after I added clip = "off" inside coord_flip(). Is there something else to do so that they show up?

library(ggplot2)
library(tidyr)
library(dplyr)
library(stringr)
library(tidyverse)
library(patchwork)

df1<-read.delim("all.csv", sep=";",header = TRUE)
df2<-gather(df1,"ligand", "element", 1:9)
df4<-table(df2)
df4<-as.data.frame(df4)
width=0.7
vjust=0.3
hjust="left"
size=2.3
col1="black"
limits=c(
    c(102:89),
    "","",
    c(70:57),
    "","",
    c(80:71),c(48:39),c(30:21),
    "","",
    "84","83","82","81","51","50","49","32","31","13",
    "","",
    "56","38","20","12","4",
    "","",
    "55","37","19","11","3")
blk=element_blank()
theme=theme(panel.background=blk,panel.grid.major.x = blk,panel.grid.major.y = blk,
            axis.title.x=blk,axis.title.y=blk,axis.text.x=blk,axis.text.y=blk,axis.ticks.x=blk,axis.ticks.y=blk)

carboxylate1<-df4[which(df4$ligand=='carboxylate'),]
carboxylate<-ggplot(df4[which(df4$ligand=='carboxylate'),], aes(x=element, y=Freq))+
  geom_bar(width=width,fill=col1,stat="identity")+coord_flip(clip = "off")+
  geom_text(data=subset(carboxylate1,Freq != 0),aes(label=Freq),vjust=vjust,hjust=hjust,size=size,colour=col1)+
  scale_x_discrete(limits=limits)+theme

pyridine1<-df4[which(df4$ligand=='pyridine'),]
pyridine<-ggplot(pyridine1, aes(x=element, y=Freq))+
  geom_bar(width=width,fill=col1,stat="identity")+coord_flip(clip = "off")+
  geom_text(data=subset(pyridine1,Freq != 0),aes(label=Freq),vjust=vjust,hjust=hjust,size=size,colour=col1)+
  scale_x_discrete(limits=limits)+theme

pyrimidine1<-df4[which(df4$ligand=='pyrimidine'),]
pyrimidine<-ggplot(pyrimidine1, aes(x=element, y=Freq))+
  geom_bar(width=width,fill=col1,stat="identity")+coord_flip(clip = "off")+
  geom_text(data=subset(pyrimidine1,Freq != 0),aes(label=Freq),vjust=vjust,hjust=hjust,size=size,colour=col1)+
  scale_x_discrete(limits=limits)+theme

water1<-df4[which(df4$ligand=='water'),]
water<-ggplot(water1, aes(x=element, y=Freq))+
  geom_bar(width=width,fill=col1,stat="identity")+coord_flip(clip = "off")+
  geom_text(data=subset(water1,Freq != 0),aes(label=Freq),vjust=vjust,hjust=hjust,size=size,colour=col1)+
  scale_x_discrete(limits=limits)+theme

carboxylate|pyridine|pyrimidine|water

not using patchwork, but adding more arguments to facet_wrap can give you this:

  facet_wrap(~Ligand,nrow = 1,scales = "free_x")+

That works well too, thanks! And the "free_x" solves the scaling issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.