[R] ggplot2, density barplot and geom_point layer
Johannes Graumann
johannes_graumann at web.de
Wed Apr 7 12:11:41 CEST 2010
Hi,
Please consider the example below. How can I manage to overlay the points
the way I want in the second case?
Thanks, Joh
library(ggplot2)
# Modify data to match "real" case
myDiamonds <- diamonds
myDiamonds[["clarity"]] <- as.character(myDiamonds[["clarity"]])
myDiamonds[myDiamonds[["clarity"]]=="I1","clarity"] <- 1
myDiamonds[myDiamonds[["clarity"]]=="SI2","clarity"] <- 2
myDiamonds[myDiamonds[["clarity"]]=="SI1","clarity"] <- 3
myDiamonds[myDiamonds[["clarity"]]=="VS2","clarity"] <- 4
myDiamonds[myDiamonds[["clarity"]]=="VS1","clarity"] <- 5
myDiamonds[myDiamonds[["clarity"]]=="VVS2","clarity"] <- 6
myDiamonds[myDiamonds[["clarity"]]=="VVS1","clarity"] <- 7
myDiamonds[myDiamonds[["clarity"]]=="IF","clarity"] <- 8
myDiamonds[["clarity"]] <- as.numeric(myDiamonds[["clarity"]])
# Calculate medians
medians <- ddply(
myDiamonds,
.(cut),
summarize,
med=median(clarity, na.rm=TRUE)
)
# Works
myPlot <- qplot(
factor(clarity),
data=myDiamonds,
fill=cut,
geom="bar",
position="dodge"
)
myPlot +
geom_point(
data=medians,
aes(x=med,shape=cut),
y=0,
size=2.5,
)
# Doesn't work - I want density rather than count
myPlot <-qplot(
factor(clarity),
y=..count../sum(..count..),
data=myDiamonds,
fill=cut,
geom="bar",
position="dodge"
)
myPlot +
geom_point(
data=medians,
aes(x=med,shape=cut),
y=0,
size=2.5,
)
More information about the R-help
mailing list