1

I am really new to R (week 3). I am trying to plot a data set that consists of 10 sites, up to 16 plots per site, at 3 different soil depths. My sample results are high, medium and low for 2 electron receptors DO and NO3. I want to plot the results per site/plot for each soil depth. Can be done separately but if there is a way to stack multiple plots that would be awesome! (Probably super advanced though.) Here is a sample of my data set.

Site Plot DO(1m) DO(3m) DO(5m) 
A     1     H     H      H    
A     2     H     H      M 
A     3     H     L      H 
A     4     H     H      L 
A     5     M     H      H
A     6     L     H      M

So far I have tried:

plot(factor(Plot),Site) 
plot(Plot~factor(Site),DO 1m 

My thought was to plot as if on a map with site and plot as my coordinates. I would prefer ggplot if possible.

dc37
  • 15,840
  • 4
  • 15
  • 32
  • 2
    In which kind of variable did you store you data. Can you please follow : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Chelmy88 Jan 30 '20 at 14:41

1 Answers1

0

Based on your data, I think a nice way to present it will be as a "heatmap". First, we can pivot your data into a longer format (I used pivot_longer from tidyr to do that), then, we can use geom_tile from ggplot to create a heatmap of the score of each DO based on their plot.

The advantage of this representation is as we are attributing the Score as the filling value, it will automatically create a color scheme in ggplot2. So, we don't need to convert character format in a numerical format. If you want to modify the color pattern, you can use scale_fill_manual.

Altogether, you can write something like that:

library(tidyr)
library(ggplot2)
library(dplyr)
df %>% pivot_longer(.,-c(Site, Plot), names_to = "DO",values_to = "Score") %>%
  ggplot(aes(x = as.factor(Plot), y = DO, fill = Score))+
  geom_tile(color = "black")+
  geom_text(aes(label = Score))+
  scale_y_discrete(labels = c("1m","2m","5m"))+
  labs(x = "Plot", title = "Site A")

enter image description here

Does it looks what you are expecting ?

Example Data

structure(list(Site = c("A", "A", "A", "A", "A", "A"), Plot = 1:6, 
    `DO(1m)` = c("H", "H", "H", "H", "M", "L"), `DO(3m)` = c("H", 
    "H", "L", "H", "H", "H"), `DO(5m)` = c("H", "M", "H", "L", 
    "H", "M")), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x56276b4f1350>)
dc37
  • 15,840
  • 4
  • 15
  • 32