只是想添加一个data.table
也可以处理分组数据的解决方案:
library(ggplot2)
library(data.table)
# Creates data from the Weibull distribution
weib_dt <- function(x = seq(0, 4.0, 0.01), w_shape = 1, w_scale = 1) {
y = dweibull(x, shape = w_shape, scale = w_scale)
data.table("shape" = as.factor(w_shape), "scale" = as.factor(w_scale), "x" = x, "y" = y)
}
dt_a <- weib_dt(w_shape = 0.5)
dt_b <- weib_dt(w_shape = 1.0)
dt_c <- weib_dt(w_shape = 2.0)
# Bind multiple Weibull samples together, created from different parametrizations
dt_merged <- rbindlist(list(dt_a, dt_b, dt_c))
# Create the plot, using all the points for the lines, and only 9 points per group for the points.
ggplot(dt_merged, aes(x, y, group=shape, color=shape)) +
coord_cartesian(ylim = c(0, 1.5)) +
geom_line() +
geom_point(data=dt_merged[, .SD[floor(seq(1, .N, length=9))], by=shape],
aes(x, y, group = shape, color = shape, shape = shape))
这里的技巧是使用seq
as 和上面建议的解决方案,但这次它是在组内完成的(使用.SD
)。请注意,当前性能可能会很差,如果速度较慢.SD
,您可以使用更详细的。dt[dt[, ..., by =shape]$V1]
这将创建以下输出: