Suppose that I'm working on Hadley's diamond dataset and I want to review the relationship between price, colour and carat. I might run the following: library(ggplot2) #plot scatter and add some hex binning q<-qplot(carat,price,data=diamonds, geom=c("hex"), main="Variability of Diamond Prices by Carat and Colour") #facet to get one scatter for each colour, plus overlay a black coloured loess smoothed line showing the trends in the data q + facet_wrap(~color,ncol=2)+geom_smooth(aes(group=1),colour=I("black")) Nice picture, but how do I extract the values of the smoothed line? Many thanks, dM/
On 09/30/2011 04:39 PM, dM/ wrote:> Suppose that I'm working on Hadley's diamond dataset and I want to > review the relationship between price, colour and carat. > > I might run the following: > > library(ggplot2) > > #plot scatter and add some hex binning > q<-qplot(carat,price,data=diamonds, geom=c("hex"), > main="Variability of Diamond Prices by Carat and Colour") > > #facet to get one scatter for each colour, plus overlay a black > coloured loess smoothed line showing the trends in the data > > q + > facet_wrap(~color,ncol=2)+geom_smooth(aes(group=1),colour=I("black")) > > Nice picture, but how do I extract the values of the smoothed line? > > Many thanks, dM/ > > ______________________________________________ > 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.Hi, geom_smooth uses R functions to calculate the smooth line. Check out ?stat_smooth for more details. You can run these command outside ggplot to the values of the smoothed line. e.g.: library(ggplot2) # Make the plot ggplot(aes(x = speed, y = dist), data = cars) + geom_point() + stat_smooth(method = "loess") # Get the values smooth_vals = predict(loess(dist~speed,cars), cars$speed) Getting the values for other smoothing functions follows this same recipe. good luck, Paul -- Paul Hiemstra, Ph.D. Global Climate Division Royal Netherlands Meteorological Institute (KNMI) Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39 P.O. Box 201 | 3730 AE | De Bilt tel: +31 30 2206 494 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
On 09/30/2011 04:39 PM, dM/ wrote:> Suppose that I'm working on Hadley's diamond dataset and I want to > review the relationship between price, colour and carat. > > I might run the following: > > library(ggplot2) > > #plot scatter and add some hex binning > q<-qplot(carat,price,data=diamonds, geom=c("hex"), > main="Variability of Diamond Prices by Carat and Colour") > > #facet to get one scatter for each colour, plus overlay a black > coloured loess smoothed line showing the trends in the data > > q + > facet_wrap(~color,ncol=2)+geom_smooth(aes(group=1),colour=I("black")) > > Nice picture, but how do I extract the values of the smoothed line? > > Many thanks, dM/ > > ______________________________________________ > 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....and if you want to extract the smoothed lines per factor (as when using facet_wrap) use ddply. good luck, Paul -- Paul Hiemstra, Ph.D. Global Climate Division Royal Netherlands Meteorological Institute (KNMI) Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39 P.O. Box 201 | 3730 AE | De Bilt tel: +31 30 2206 494 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770