Alice Hobbs
Alice Hobbs

Reputation: 1215

Multiple side-by-side barplot comprising of 2 y-axis (ggplot or base R)

Description of the multiple side-by-side barplot with 2 y-axis

I would like to produce a barplot (see below) using the data provided below with 2 y axis for two variables:-

  1. one for T.max.open which goes with the column Open_Site
  2. one for T.max.canopy, which goes with the column Under_Tree
  3. The x-axis should be named Topography (i.e Artificial Surface, Bare Soil, Grass, Litter and Undergrowth, found in the two columns for Open_Site and Under_Tree)

Each topography group should have 2 bars next to each other, representing the temperature ranges that different vegetation types experience in open sites (Open_Site) and under the tree canopy (Under_Tree).

I cannot use one y-axis because I would like to show the reltionship between the temperature underneath the tree canopy (y-axis 1 - T.max.canopy) and in open sites (y-axis 2 - T.max.open) to see whether the temperature effects the magnitude and diversity of plant communities in both microclimates

The barplot should look similar to the Barplot (below) that I produced for a different question. I just cannot figure out how to resolve this issue.

Is this possible with base R (which is preferred)?

Thank you, if anyone can help.

Issues

I have tried unsuccessfully to format my data using functions such as melt() and cast() into the correct format, and I have also searched online from which I found several solutions but appear to not be applicable to my case. For instance, two stack overflow questions Stack Overflow 1 and Stack Overflow 2.

enter image description here

Data

    structure(list(Under_Tree = structure(c(2L, 2L, 2L, 3L, 1L, 3L, 
    2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L), .Label =      c("Artificial_Surface", 
    "Litter", "Undergrowth"), class = "factor"), Open_Site =      structure(c(2L, 
     3L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 3L, 2L, 
     2L, 3L, 1L), .Label = c("Artificial_Surface", "Litter",      "Undergrowth"
     ), class = "factor"), T.max.canopy = c(10, 14, 11.4, 7.7, 23.8, 
     12.9, 8, 15, 18.3, 9, 22.7, 12, 24.9, 11.2, 7.9, 4.5, 5.6, 13.7, 
     15.9, 16.4), T.max.open = c(8, 15, 18.3, 9, 22.7, 12, 17.4, 13, 
     17.9, 16.6, 14.6, 18.3, 27.5, 14.1, 17.5, 15.4, 13, 15.1, 15.1, 
      19.7)), .Names = c("Under_Tree", "Open_Site", "T.max.canopy", 
     "T.max.open"), class = "data.frame", row.names = c(NA, -20L))

Upvotes: 1

Views: 243

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70643

Sorry to rain on your canopy, but I feel the barplots are not the best representation of your beautiful data. In my opinion, they would be best represented by a scatter plot and combined with facetting and colors, the story could be told quite efficiently. See if you agree:

xy <- structure(list(Under_Tree = structure(c(2L, 2L, 2L, 3L, 1L, 3L, 
                                              2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L), .Label =      c("Artificial_Surface", 
                                                                                                                       "Litter", "Undergrowth"), class = "factor"), Open_Site =      structure(c(2L, 
                                                                                                                                                                                                 3L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 3L, 2L, 
                                                                                                                                                                                                 2L, 3L, 1L), .Label = c("Artificial_Surface", "Litter",      "Undergrowth"
                                                                                                                                                                                                 ), class = "factor"), T.max.canopy = c(10, 14, 11.4, 7.7, 23.8, 
                                                                                                                                                                                                                                        12.9, 8, 15, 18.3, 9, 22.7, 12, 24.9, 11.2, 7.9, 4.5, 5.6, 13.7, 
                                                                                                                                                                                                                                        15.9, 16.4), T.max.open = c(8, 15, 18.3, 9, 22.7, 12, 17.4, 13, 
                                                                                                                                                                                                                                                                    17.9, 16.6, 14.6, 18.3, 27.5, 14.1, 17.5, 15.4, 13, 15.1, 15.1, 
                                                                                                                                                                                                                                                                    19.7)), .Names = c("Under_Tree", "Open_Site", "T.max.canopy", 
                                                                                                                                                                                                                                                                                       "T.max.open"), class = "data.frame", row.names = c(NA, -20L))

library(ggplot2)
library(tidyr)
library(Hmisc)

xyg <- gather(xy, key = tree, value = microlocation, -T.max.canopy, -T.max.open)
xyg <- gather(xyg, key = canopyside, value = temperature, -tree, -microlocation)

ggplot(xyg, aes(x = microlocation, y = temperature, color = canopyside)) +
  theme_bw() +
  geom_point(alpha = 0.75) +
  scale_color_brewer(palette = "Set1") +
  stat_summary(fun.data = "mean_cl_boot", color = "black", size = 1, alpha = 0.5) +
  facet_wrap(~ tree)

enter image description here

Upvotes: 2

Related Questions