[R] ggplot2, density barplot and geom_point layer
hadley wickham
h.wickham at gmail.com
Wed Apr 7 18:03:19 CEST 2010
Because of the way you've constructed the plot with qplot, you need to use:
myPlot + geom_point(
data=medians,
aes(x=med,shape=cut, y=0),
size=2.5,
)
Hadley
On Wed, Apr 7, 2010 at 5:11 AM, Johannes Graumann
<johannes_graumann at web.de> wrote:
> 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,
> )
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
More information about the R-help
mailing list