Re: [R] ggplot2, density barplot and geom_point layer
Thanks so much. Solved. Joh hadley wickham wrote: > 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 > 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@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. >> > > > __ R-help@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.
Re: [R] ggplot2, density barplot and geom_point layer
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 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@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/ __ R-help@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.