[R] ggplot2 plot with symbols and then add line

hadley wickham h.wickham at gmail.com
Tue Oct 14 14:11:19 CEST 2008


On Tue, Oct 14, 2008 at 6:46 AM, stephen sefick <ssefick at gmail.com> wrote:
> r <-(structure(list(TSS = c(2.8, 8.4, 11, 1.3, 4.2, 2, 3.4, 14, 8.2,
> 3.1, 1.4, 0.9, 0.5, 6.1, 9.2, 0.6, 1, 11, 2.4, 1.2, 1.3, 1.3,
> 0, 1.8, 8, 11, 11, 8.5, 8.5, 1.8, 13, 4.4, 1.4, 2.1, 0.5, 25,
> 25, 9.3, 6.1, 1.6, 1.5, 19, 19, 24, 9.6, 1.8, 1.4, 1), GPP = c(1.213695235,
> 3.817313822, 1.267930498, 10.45692825, 3.268295623, 3.505286001,
> 4.468225245, 0.915653726, 1.635617261, 3.726133898, 1.363453706,
> 13.99650967, 1.581182762, 0.417618143, 0.741080504, 1.366790205,
> 0.969326797, 0.412440872, 1.780215366, 3.515743675, 8.248491445,
> 0.125726306, 13.95880794, 4.850627229, -0.438311644, 1.537773727,
> 1.537773727, 3.249103284, 3.249103284, 2.470317011, 0.768531626,
> 2.633107621, 3.113199095, 0.773824094, 3.208461305, 0.680150068,
> 0.680150068, 0.026385752, 0.369310858, 8.049276658, 7.487378383,
> 0.950072035, 0.950072035, 0.763580377, 0.333244629, 5.475999014,
> 9.235631398, 2.587682905), RiverMile = c(148L, 179L, 185L, 202L,
> 179L, 185L, 190L, 119L, 148L, 179L, 185L, 202L, 215L, 119L, 202L,
> 198L, 215L, 148L, 198L, 190L, 202L, 215L, 198L, 198L, 215L, 148L,
> 148L, 202L, 202L, 215L, 119L, 179L, 185L, 190L, 198L, 61L, 61L,
> 119L, 148L, 202L, 202L, 61L, 61L, 119L, 148L, 185L, 202L, 215L
> )), .Names = c("TSS", "GPP", "RiverMile"), class = "data.frame",
> row.names = c(12L,
> 13L, 14L, 19L, 25L, 26L, 28L, 34L, 35L, 36L, 37L, 42L, 44L, 58L,
> 75L, 85L, 88L, 91L, 97L, 107L, 142L, 144L, 155L, 166L, 169L,
> 185L, 186L, 201L, 202L, 207L, 219L, 221L, 222L, 224L, 226L, 230L,
> 231L, 250L, 251L, 258L, 283L, 287L, 288L, 303L, 304L, 306L, 311L,
> 313L)))
>
> library(lattice)
> library(ggplot2)
> z <- nls(GPP~(a/(TSS+b)), start=c(a=0.01, b=0.01), data=r)
> f <- seq(-1, 29, length=100)
> y <- coef(z)["a"]/(f+coef(z)["b"])
> pred <- data.frame(GPP = f, TSS = y)
>
> #this is what I can do
> qplot(TSS, GPP, data=r, colour=as.factor(RiverMile)) +
> geom_line(data=pred, colour ="black")
>
> #I would like to do this and then add the nls line
> qplot(TSS, GPP, data=r,
> shape=as.factor(RiverMile))+scale_shape_manual(values=1:9)
>
> #how do you undo the mapping of shape to add a line is my question
> #thanks

+ geom_line(aes(shape = NULL))

Alternatively you can use the full ggplot specification, and only
supply shape to the points:

ggplot(r, aes(TSS, GPP)) +
  geom_point(aes(shape = factor(RiverMile)) +
  geom_line(data = pred) +
  scale_shape_manual(values = 1:9)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list