0

I am attempting to colour the raw data points, but I can't seem to find the information on how to do that, when I am already defining colours by group.

Data:

data = read.csv('TotMaxSize.csv')
structure(list(Column1 = 1:6, yrblock15 = c(2004L, 2004L, 2004L, 
2004L, 2004L, 2004L), circleID = 1:6, ThreeYearRain = c(748.9863518, 
744.4805429, 748.6081666, 747.5941999, 746.3382951, 740.9514718
), time = c(5.270172597, 4.270172617, 3.348596103, 3.019112219, 
2.905252281, 2.773856447), claylake = c(0, 0, 0, 0, 0.01, 0), 
    spinsandplain = c(99.53, 90.39, 50.7, 63.8, 73.65, 82.73), 
    TotMaxSize = c(2058.592458, 936.2305886, 1652.692998, 2162.200459, 
    1062.143104, 1863.051545)), row.names = c(NA, 6L), class = "data.frame")

Loaded packages:

library(ggplot2);library(lme4);library(ggeffects);library(dplyr) 

Model:

fit <- lmer(TotMaxSize~log(time)+spinsandplain+ThreeYearRain+claylake+ThreeYearRain*log(time)+(1|circleID),na.action=na.fail,data=data,REML=FALSE)

The plot:

mydf <- ggpredict(fit, terms = c("time[exp]","ThreeYearRain[647.6448,1198.882,1934.894]")) 
mydf1 <- rename(mydf, "time" = x, "TotMaxSize" = predicted)


ggplot(mydf1, aes(x = time, y = TotMaxSize, colour = group)) +
  geom_point(data = data, colour = "black")+
  stat_smooth(method = "lm", se = FALSE)+
  scale_colour_manual(values=c("skyblue2","dodgerblue3","darkblue"))+
  xlim(0,10)+ylim(0,7000)+
  xlab("time")+ylab("Max fire size")

It ends up looking like this:

enter image description here

Is there a way to colour geom_point (in a gradient) by the variable "ThreeYearRain"? I keep getting a similar error to other threads that I have read:

"Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale."
  • 2
    A bit of a hack ... but you could try by changing the shape of your points and making use of the `fill` aes, i.e. `geom_point(data = data, aes(fill = ThreeYearRain), shape = 21, colour = "black")`. Otherwise have a look at the `ggnewscale` package. – stefan Mar 30 '21 at 06:29
  • Amazing, thank you! This works perfectly – Leanne Greenwild Mar 31 '21 at 22:25
  • It's difficult with reproducible example, but have you tried `plot(mydf)`? – Daniel Apr 25 '21 at 07:25

1 Answers1

0

You have defined the colour twice: once in aes(), and again in geom_point().

justinian482
  • 845
  • 2
  • 10
  • 18