-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot2.R
28 lines (24 loc) · 1.09 KB
/
ggplot2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Here is an example of how you might create a violin plot in R of soil sample concentration data using ggplot2 for multiple chemicals grouped by exposure area and sample depth:
# This code creates a violin plot using ggplot2 with the x-axis representing the exposure area and the y-axis representing the concentration. The plot is faceted by depth and chemical.
library(ggplot2)
library(tidyr)
# Create example data
data <- data.frame(
Chemical = rep(paste0("Chemical ", 1:9), each = 9),
ExposureArea = rep(paste0("Area ", 1:3), 27),
SurfaceSoil = runif(81, 10, 60),
TotalSoil = runif(81, 15, 65),
DeepSoil = runif(81, 20, 70)
)
# Reshape data from wide to long format
data_long <- pivot_longer(data,
cols = c("SurfaceSoil", "TotalSoil", "DeepSoil"),
names_to = 'Depth',
values_to = 'Concentration')
# Create plot
ggplot(data_long,aes(x=ExposureArea,y=Concentration)) +
geom_violin() +
facet_grid(Depth ~ Chemical) +
labs(title="Soil Sample Concentration Data",
x="Exposure Area",
y="Concentration")