我的代码如下所示,它表示如何使variable 1
的直方图具有密度。我想根据每个箱子中variable 2
的平均值在每个箱子中放置颜色。还有一件事,我如何处理NA
将variable 1
的平均值作为直方图中的一行?
library(plyr)
library(ggplot2)
mu <- ddply(chem, "Event", summarise, grp.mean=mean(variable1))
head(mu)
ggplot(chem, aes(x=variable1, color=Event, fill=Event)) +
geom_histogram(aes(y=..density..), position="identity", alpha=0.5)+
geom_density(alpha=0.6)+
geom_vline(data=mu, aes(xintercept=grp.mean, color=Event),
linetype="dashed")+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
labs(title="histogram plot",x="variable1", y = "Density")+
theme_classic()
我的数据如下。
structure(list(Event = c("Non_event", "Event", "Non_event", "Non_event",
"Event", "Non_event", "Non_event", "Non_event", "Non_event",
"Event", "Non_event", "Non_event", "Event", "Non_event", "Non_event",
"Non_event", "Non_event", "Non_event", "Non_event", "Non_event",
"Non_event", "Non_event", "Non_event", "Non_event", "Non_event",
"Non_event", "Non_event", "Non_event", "Non_event", "Non_event",
"Non_event", "Non_event", "Event", "Non_event", "Non_event",
"Non_event", "Non_event", "Non_event", "Event", "Event", "Event",
"Non_event", "Non_event", "Non_event", "Non_event", "Event",
"Non_event", "Non_event", "Event", "Non_event", "Non_event",
"Event", "Non_event", "Non_event", "Non_event"), `variable 1` = c(NA,
NA, NA, 1.609312712, 1.382101169, 1.597452592, 1.402661704, 1.607392603,
1.718794128, 1.565779105, 1.489225013, 1.630354881, 1.343661534,
1.500549747, 1.467466543, 1.442623884, 1.478242485, 1.464427916,
1.353169276, 1.359483363, 1.28417476, 1.531701496, 1.464046406,
1.282512799, 1.304118661, 1.317079464, 1.430901299, 1.323954382,
1.25212797, 1.489035987, 1.415237217, 1.274081249, 1.492800731,
1.494991417, 1.551739503, 1.544744326, 1.558564028, 1.559592069,
1.423763007, 1.409964664, 1.408246799, 1.345383381, 1.361731784,
1.300218087, 1.245485182, 1.330140019, 1.392247923, 1.326278573,
1.196095946, 1.608671905, 1.308027761, 1.288879968, 0.690813114,
1.231076737, 1.210319269), `variable 2` = c(NA, NA, NA, 3.228552923,
4.40426877, 4.228771612, 7.681871908, 3.26514454, 3.72120454,
3.826889586, 3.976917058, 3.512514737, 5.029035249, 3.436619098,
4.089232991, 3.810386552, 4.444591698, 4.076661889, 3.234084965,
4.10900919, 4.35422338, 2.715945911, 3.577286664, 3.225137774,
4.231270184, 4.752692743, 6.28163417, 4.047511471, 5.252623079,
4.004799307, 3.129999179, 4.338912973, 5.595145507, 1.527469861,
1.1456549, 1.231217487, 2.865777484, 1.890864079, 3.093993139,
11.25080407, 3.308300543, 3.437145531, 1.236358711, 3.454223824,
1.851932104, 7.357054038, 10.13511755, 2.355285012, 3.629293259,
1.301526677, 2.344958268, 4.079552316, 1.724442458, 4.68651158,
2.883467842)), .Names = c("Event", "variable1", "variable2"
), class = "data.frame", row.names = c(NA, -55L)) -> chem