library(lattice)
data(sp1)
# 1. plotting mechanism for step-functions derived from soil profile data
xyplot(
cbind(top, bottom) ~ prop,
data = sp1,
id = sp1$id,
panel = panel.depth_function,
ylim = c(250, -10),
scales = list(y = list(tick.number = 10)),
xlab = 'Property',
ylab = 'Depth (cm)',
main = 'panel.depth_function() demo'
)
# 1.1 include groups argument to leverage lattice styling framework
sp1$group <- factor(sp1$group, labels = c('Group 1', 'Group2'))
xyplot(
cbind(top, bottom) ~ prop,
groups = group,
data = sp1,
id = sp1$id,
panel = panel.depth_function,
ylim = c(250, -10),
scales = list(y = list(tick.number = 10)),
xlab = 'Property',
ylab = 'Depth (cm)',
main = 'panel.depth_function() demo',
auto.key = list(
columns = 2,
points = FALSE,
lines = TRUE
),
par.settings = list(superpose.line = list(col = c(
'Orange', 'RoyalBlue'
)))
)
# more complex examples, using step functions with grouped data
# better looking figures with less customization via tactile package
if(requireNamespace('tactile')) {
library(data.table)
library(lattice)
library(tactile)
# example data
data(sp6)
# a single profile
x <- sp6[1:5, ]
# wide -> long format
x.long <- data.table::melt(
data.table::data.table(x),
id.vars = c('id', 'top', 'bottom'),
measure.vars = c('sand', 'silt', 'clay')
)
# (optional) convert back to data.frame
x.long <- as.data.frame(x.long)
# three variables sharing a common axis
# factor levels set by melt()
xyplot(
cbind(top, bottom) ~ value | id,
groups = variable,
data = x.long,
id = x.long$id,
ylim = c(200, -5), xlim = c(10, 60),
scales = list(alternating = 1, y = list(tick.number = 10)),
par.settings = tactile.theme(superpose.line = list(lwd = 2)),
xlab = 'Sand, Silt, Clay (%)',
ylab = 'Depth (cm)',
panel = panel.depth_function,
auto.key = list(columns = 3, lines = TRUE, points = FALSE),
asp = 1.5
)
# all profiles
x <- sp6
# wide -> long format
x.long <- data.table::melt(
data.table::data.table(x),
id.vars = c('id', 'top', 'bottom'),
measure.vars = c('sand', 'silt', 'clay')
)
# (optional) convert back to data.frame
x.long <- as.data.frame(x.long)
# three variables sharing a common axis
# factor levels set by melt()
xyplot(
cbind(top, bottom) ~ value | id,
groups = variable,
data = x.long,
id = x.long$id,
ylim = c(200, -5), xlim = c(0, 70),
scales = list(alternating = 1, y = list(tick.number = 10)),
par.settings = tactile.theme(superpose.line = list(lwd = 2)),
xlab = 'Sand, Silt, Clay (%)',
ylab = 'Depth (cm)',
panel = panel.depth_function,
auto.key = list(columns = 3, lines = TRUE, points = FALSE),
as.table = TRUE
)
xyplot(
cbind(top, bottom) ~ value,
groups = variable,
data = x.long,
id = x.long$id,
ylim = c(200, -5), xlim = c(0, 70),
scales = list(alternating = 1, y = list(tick.number = 10)),
par.settings = tactile.theme(superpose.line = list(lwd = 2)),
xlab = 'Sand, Silt, Clay (%)',
ylab = 'Depth (cm)',
panel = panel.depth_function,
auto.key = list(columns = 3, lines = TRUE, points = FALSE),
as.table = TRUE
)
xyplot(
cbind(top, bottom) ~ value | variable,
groups = variable,
data = x.long,
id = x.long$id,
ylim = c(200, -5), xlim = c(0, 70),
scales = list(alternating = 1, y = list(tick.number = 10)),
par.settings = tactile.theme(superpose.line = list(lwd = 2)),
xlab = 'Sand, Silt, Clay (%)',
ylab = 'Depth (cm)',
panel = panel.depth_function,
auto.key = list(columns = 3, lines = TRUE, points = FALSE),
as.table = TRUE
)
xyplot(
cbind(top, bottom) ~ value | variable,
data = x.long,
id = x.long$id,
ylim = c(200, -5), xlim = c(0, 70),
scales = list(alternating = 1, y = list(tick.number = 10)),
par.settings = tactile.theme(superpose.line = list(lwd = 2)),
xlab = 'Sand, Silt, Clay (%)',
ylab = 'Depth (cm)',
panel = panel.depth_function,
auto.key = list(columns = 3, lines = TRUE, points = FALSE),
as.table = TRUE
)
}
Run the code above in your browser using DataLab