r - How to use facet_grid correctly in ggplot2? -
i'm trying generate 1 chart per profile following code, keep getting "at least 1 layer must contain variables used facetting." errors. spent last few hours trying make work couldn't.
i believe anwser must simple, can help?
d = structure(list(category = structure(c(2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 3l, 3l, 3l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l), .label = c("4x4", "hatch", "sedan"), class = "factor"), profile = structure(c(1l, 1l, 1l, 1l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l), .label = c("fixed", "free", "mobile"), class = "factor"), value = c(6440.32, 6287.22, 9324, 7532, 7287.63, 6827.27, 6880.48, 7795.15, 7042.51, 2708.41, 1373.69, 6742.87, 7692.65, 7692.65, 8116.56, 7692.65, 7692.65, 7692.65, 7962.65, 8116.56, 5691.12, 2434, 8343, 7727.73, 7692.65, 7721.15, 1944.38, 6044.23, 8633.65, 7692.65, 7692.65, 8151.65, 7692.65, 7692.65, 2708.41, 3271.45, 3333.82, 1257.48, 6223.13, 7692.65, 6955.46, 7115.46, 7115.46, 7115.46, 7115.46, 6955.46, 7615.46, 2621.21, 2621.21, 445.61)), .names = c("category", "profile", "value" ), class = "data.frame", row.names = c(na, -50l)) library(ggplot2) p = ggplot(d, aes(x=d$value, fill=d$category)) + geom_density(alpha=.3) p + facet_grid(d$profile ~ .)
your problem comes referring variables explicitly (i.e. d$profile
), not respect data
argument in call ggplot
. there no need d$
anywhere.
when faceting
using facet_grid
or facet_wrap
, need so. practice in calls aes
p = ggplot(d, aes(x=value, fill=category)) + geom_density(alpha=.3) p + facet_grid(profile ~ .)
Comments
Post a Comment