Changing loess line type

82 views
Skip to first unread message

Kirch

unread,
Jun 9, 2015, 2:41:37 PM6/9/15
to ggp...@googlegroups.com
I would like to have different types of loess lines for the two months in the attached data set.  The graph produced by the ggplot script given below is nearly exactly what I want, but the same type of line (I set it to 2, a dash) is used for both August and November.
 
How can the type of loess line (solid vs. dash, for example) be made to depend on a factor, in this case Month?

data<- read.csv("testdata.csv", header=T)

df <- data.frame (Year=factor(data$Year), Month=factor(data$Month),
                  Salinity=data$Salinity, Chl=data$Chl)

library(ggplot2)

ggplot(df, aes (x=Salinity, y=Chl, group=Month, fill=Month))+
  geom_point(size = 1, aes(fill = NULL)) +
  geom_point(size = 1, aes(alpha = Month)) +
  scale_shape_manual(values = c(21, 22, 23)) +
  scale_color_manual(values = c("black", "black")) +
  scale_fill_manual(values = c("black", "white")) +
  scale_alpha_manual(values = c(1, 0))+
  geom_point(aes(shape=Year))+
  stat_smooth (method="loess", se=F, color="black",linetype=2, show_guide=F)+
  xlab("Salinity") +
  ylab( expression(paste("Chlorophyll a (", mu,"g ", L^{-1},")")))+
  scale_y_continuous (limits=c(0, 25))+
  coord_fixed(ratio=1)+  
  guides(alpha = guide_legend(override.aes = list(shape = 21, fill = c("black", NA), alpha =c(1,1))))+
  theme_classic(base_size = 14, base_family = "")



testdata.csv

John Rauser

unread,
Jun 9, 2015, 4:59:41 PM6/9/15
to Kirch, ggplot2 mailing list
Each layer can have it's own aesthetic mapping.  You want to map linetype to the grouping variable.

Try: 

  stat_smooth (aes(linetype=Month), method="loess", se=F, color="black", show_guide=F)+

-J

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kirch

unread,
Jun 10, 2015, 10:04:39 AM6/10/15
to ggp...@googlegroups.com, kirch...@gmail.com
Great, this worked. Thanks much.
Reply all
Reply to author
Forward
0 new messages