0
votes

I am trying to create a plot using ggplot2 that includes both geom_smooth and geom_point. Since I want to see the slope of the line and how it changes over time, I would like color the geom_smooth line one color (Timepoint) and the points a different color (by plot). I have tried various different things, but they all fail. Including some of the options that I have found here on stackoverflow.

My code is as follows:

##Edited code to show as of 7/28
ggplot(data, aes(x = InitialAshDepth, y = S.obs, color =TimePoint)) +
geom_point(size = 3.5, aes(fill = Plot))+
geom_smooth(method = "lm", alpha=0.15, aes(color = TimePoint)) +
#geom_smooth( method=lm, alpha=0.15,aes(group = TimePoint, 
color=TimePoint))+
scale_color_manual(values = TP) + 
  theme_bw()

Can someone please let me know how I can go about achieving this and or if this is even possible?

Please note that I have tried grouping by and just color by or a combination of both (when I do a combination of both, I get an error stating I need 700+ colors)??

This is the graph that I have created but the points are not being colored differently from the geom_smooth lines. Sorry, I am not allowed to embed pictures, but please see the link.

enter image description here

Here is an example of my data with the columns I am working with. Here I only show three timepoints just for the sake of saving space but I actually have 9-time points. Please see image as the data gets distorted when I copy and paste.

Sample data:

Plot    TimePoint   Treatment   SpeciesRichness
1   TP1 Burned  88
1   TP2 Burned  66
1   TP3 Burned  60
2   TP1 Burned  119
2   TP2 Burned  55
2   TP3 Burned  44
3   TP1 Burned  35
3   TP2 Burned  34
3   TP3 Burned  22
4   TP1 Burned  63
4   TP2 Burned  49
4   TP3 Burned  9
5   TP1 Burned  26
5   TP2 Burned  35
5   TP3 Burned  66
6   TP1 Burned  21
6   TP2 Burned  85
6   TP3 Burned  65
7   TP1 Unburned    57
7   TP2 Unburned    169
7   TP3 Unburned    110
8   TP1 Unburned    166
8   TP2 Unburned    180
8   TP3 Unburned    114
9   TP1 Unburned    270
9   TP2 Unburned    194
9   TP3 Unburned    328
1
Can you add full code? Where is your ggplot(df, aes(x, y)) part?Anakin Skywalker
Without the actual data it is hard give you proper help, please refer to stackoverflow.com/questions/5963269/… for a guide for a great reproducible example. Then we can help you. Right now your data and the full code is missing.Mojoesque
Fake data would help tooAnakin Skywalker
Hi, I just added an example of my data to my current post to see if it helps.Fabipc
Hi, Looks like I just figured it out, thanks a milliion for your helpFabipc

1 Answers

0
votes

I think I might have just figured it out. Here is the code if anyone else needs it. I do need to update the question but I have no idea how to do it. It is geom_smooth and not geom_line but seems like the code works for both.

#Create the color scheme you want ........................................

colorTP<-c("#65000b","#a50F15","#EF3B2C","#FC9272","#FCBBA1","#fac2e3","#001499","#0a2bff","#9acfe4")

colorPlot<-c("#40271f","#5e4839","#947f70","#c49f68","#bfb3aa","#ebddc7","#235978","#45877f","#aed4ca")

#Code to create plot--- where MetaRare is the data you are using ------

  geom_smooth(method = "lm", alpha = .15, aes(color=TimePoint)) +
  geom_point(aes(fill=factor(Plot)), size=4, shape=21, stroke=0) +
  scale_fill_manual(values=colorPlot) +
  scale_colour_manual(values=colorTP) +
  theme_classic() +
  labs(fill="Plot", colour="Time Point ")```