1

I have a device that generates an excel spreadsheet for a number of parameters (e.g. height, biomass, NDVI, density etc.). These independent variables are included in the header names, and all have an assigned numeric value. I would like to generate multiple box plots (one for each variable) that uses the "treatment" column to generate multiple x values.

For example: To create a boxplot with y axis height, and x axis treatment, y axis biomass + x axis treatment and so on.

I know how to do this normally with ggplot2 and manually input this information, but I'd like advice on how to generate it so that y = column name, x= treatment.

Any help at all would be greatly appreciated :) Thanks in advance!

MatthewR
  • 2,660
  • 5
  • 26
  • 37
  • You can visit this https://stackoverflow.com/q/14604439/6123824 – UseR10085 Oct 28 '19 at 18:59
  • Hi, Welcome to SO. This is not really a concise coding question. Please see this link on how to pose questions https://stackoverflow.com/help/minimal-reproducible-example – MatthewR Oct 28 '19 at 19:18

1 Answers1

1

If you want them all in one figure, but with a separate panel for each column, then melt those columns (see ?tidyr::gather) and use ggplot2::facet_wrap(). In other words, reshape the data long so that you instead have one column for "variable" and one column for "values", then set aes(y = values) and use facet_wrap(~ variable).

Below is an example with the iris data set, using the column Species as treatment, and the rest as parameters.

library(tidyr)
library(ggplot2)

data(iris)

iris %>%
  gather(var, val, -Species) %>%
  ggplot(aes(x = Species, y = val)) +
  facet_wrap(~ var, scales = "free_y") +
  geom_boxplot() +
  labs(
    x = "Treatment",
    y = "Parameter") +
  theme_bw()