2

I want to make either a 2x2 or 4x1 plot with various water quality info by site. My site names are long. I'm using ggarrange for 4 plots. But! If I remove the labels, the resulting figure doesn't equalize the spaces for the plots themselves.

Image I want:

Image I want

sitenameso<- c("Honolua", "Fleming N", "Oneloa", "Kapalua Bay", "Napili S", 
"Ka'opala", "Kahana Village", "Pohaku", "Kaanapali Shores", "Airport Beach", 
"Canoe Beach", "Wahikuli", "505 Front St", "Lindsey Hale", "Lahaina Town", 
"Puamana", "Launiupoko", "Olowalu shore", "PM Hale", "Camp Olowalu", 
"Mile Marker 14", "Ukumehame BP", "Papalaua", "Papalaua Pali"
)

library(ggpubr)
library(colorspace)
col<-rainbow_hcl(4)

p1<- ggplot(hui_geomean, aes(x=factor(SiteNum), y=Nitrate))+
  geom_bar(stat="identity", position="dodge", fill=col[1])+
  ylab("ug/L")+labs(title="Nitrate + Nitrite")+
  scale_x_discrete(name=NULL, labels=sitenameso)+
  geom_hline(yintercept=3.5, linetype="dashed", color = "red")+
  theme_classic2()+
  theme(axis.text.x = element_text(angle=-90,hjust=0),
        #plot.margin = unit(c(2, 1, 1, 1), "lines"),
        plot.title = element_text(hjust = 0.5))

p2<- ggplot(hui_geomean, aes(x=factor(SiteNum), y=TP))+
  geom_bar(stat="identity", position="dodge", fill="blue")+
  ylab("ug/L")+labs(title="Total P")+
  scale_x_discrete(name=NULL, labels=sitenameso)+
  geom_hline(yintercept=16, linetype="dashed", color = "red")+
  theme_classic2()+
  theme(axis.text.x = element_text(angle=-90,hjust=0),
        #plot.margin = unit(c(2, 1, 1, 1), "lines"),
        plot.title = element_text(hjust = 0.5))

p3<- ggplot(hui_geomean, aes(x=factor(SiteNum), y=NH4))+
  geom_bar(stat="identity", position="dodge", fill=col[3])+
  ylab("ug/L")+labs(title="Ammonia")+
  scale_x_discrete(name=NULL, labels=sitenameso)+
  geom_hline(yintercept=2.0, linetype="dashed", color = "red")+
  theme_classic2()+
  theme(axis.text.x = element_text(angle=-90,hjust=0),
        #plot.margin = unit(c(2, 1, 1, 1), "lines"),
        plot.title = element_text(hjust = 0.5))

p4<- ggplot(hui_geomean, aes(x=factor(SiteNum), y=Turb))+
  geom_bar(stat="identity", position="dodge", fill=col[4])+
  #ylab("NTU")+labs(title="Turbidity")+
  scale_x_discrete(name=NULL, labels=sitenameso)+
  geom_hline(yintercept=0.2, linetype="dashed", color = "red")+
  theme_classic2()+
  theme(axis.text.x = element_text(angle=-90,hjust=0),
        #plot.margin = unit(c(2, 1, 1, 1), "lines"),
        plot.title = element_text(hjust = 0.5))

#multiplot(p1, p2, p3, p4, cols=2)
tiff(filename= paste("barplots2_", Sys.Date(), ".tif", sep=""), 
     width = 4, height = 6, units = 'in', res=300)
ggarrange(p1+rremove("x.text"), p2+rremove("x.text"), p3+rremove("x.text"), p4, 
          labels = c("A", "B", "C", ""),
          font.label=list(size=12),
          #heights = c(1, 1),
          ncol = 1, nrow = 4)
dev.off()

Here is the data (header). Hopefully this helps.

structure(list(SiteName = structure(c(6L, 5L, 18L, 10L, 16L, 
7L), .Label = c("505 Front Street", "Airport Beach", "Camp Olowalu", 
"Canoe Beach", "DT Beach (Fleming N)", "Honolua", "Ka'opala", 
"Kaanapali Shores", "Kahana Village", "Kapalua Bay (Fleming S)", 
"Lahaina Town", "Launiupoko", "Lindsey Hale", "Makila Point", 
"Mile Marker 14", "Napili", "Olowalu Shore Front", "Oneloa", 
"Papalaua", "Papalaua Pali", "Peter Martin Hale", "Pohaku", "Ukumehame Beach", 
"Wahikuli"), class = "factor"), lat = c(21.013058, 21.005, 21.004056, 
20.998924, 20.994222, 20.982074), long = c(-156.63834, -156.65084, 
-156.65894, -156.666746, -156.667417, -156.673398), Session = c(23, 
23, 23, 23, 17, 23), TP = c(12.51, 11.78, 12.35, 19.27, 14.44, 
15.64), Phosphate = c(7.93, 5.93, 4.77, 14.18, 9.3, 11.29), NH4 = c(5.29, 
2.37, 2.06, 4.48, 5.64, 3.36), Nitrate = c(5.23, 9.63, 4.72, 
79.76, 24.65, 55.87), TN = c(86, 77.72, 73.86, 163.06, 119.23, 
121.65), Silica = c(408.75, 221.42, 95.67, 810.38, 410.42, 497.56
), Temp = c(24.56, 25.33, 25.63, 25.96, 25.09, 25.73), Sal = c(33.13, 
34.37, 34.69, 33.25, 33.99, 33.92), DO = c(6.35, 6.81, 6.85, 
6.79, 6.8, 6.6), DOper = c(91.88, 100.56, 102.09, 100.88, 99.73, 
98.03), pH = c(8.12, 8.16, 8.18, 8.13, 8.12, 8.12), Turb = c(9.62, 
2.17, 0.74, 1.49, 1.32, 8.49), SiteNum = c(1, 2, 3, 4, 5, 6), 
    siteno = c(1, 2, 3, 4, 5, 6)), row.names = c(NA, 6L), class = "data.frame")
  • 2
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Oct 16 '18 at 01:17
  • @Tung Stack limited me added the data. Here it is: – Kim Falinski Oct 16 '18 at 02:07

1 Answers1

2

You can use ggarrange() function but from the egg package instead

p11 <- p1 + theme(axis.text.x = element_blank())
p12 <- p2 + theme(axis.text.x = element_blank())
p13 <- p3 + theme(axis.text.x = element_blank())
p14 <- p4 + theme(axis.text.x = element_text(vjust = 0.5))

# install.packages("devtools")
# devtools::install_github("baptiste/egg")
library(egg)
ggarrange(p11,
          p12,
          p13,
          p14,
          labels = c("A", "B", "C", ""),
          label.args = list(gp = grid::gpar(font = 4, cex = 1.2)),
          nrow = 4)

patchwork will also work

# devtools::install_github("thomasp85/patchwork")
library(patchwork)
p11 + p12 + p13 + p14 + 
  plot_layout(nrow = 4) + 
  plot_annotation(tag_levels = 'A', tag_suffix = ')')

Created on 2018-10-15 by the reprex package (v0.2.1.9000)

Tung
  • 26,371
  • 7
  • 91
  • 115