-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot21.R
30 lines (24 loc) · 1.06 KB
/
ggplot21.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
29
#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:
library(ggplot2)
library(tidyr)
# Create example data
data1 <- data.frame(
Chemical = rep(paste0("Chemical ", 1:4), each = 9),
ExposureArea = rep(paste0("Area ", 1:3), 12),
Depth1 = runif(36, 10, 60),
Depth2 = runif(36, 15, 65),
Depth3 = runif(36, 20, 70)
)
# Reshape data from wide to long format
data_long1 <- pivot_longer(data1,
cols = starts_with("Depth"),
names_to = 'Depth',
values_to = 'Concentration')
# Create plot
ggplot(data_long1,aes(x=Chemical,y=Concentration)) +
geom_violin() +
facet_grid(Depth ~ ExposureArea) +
labs(title="Soil Sample Concentration Data",
#x="Chemical",
y="Concentration")
#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.