I have a problem occuring in my ggplot that I am unable to find the problem. I have a data frame looking somewhat as follows and whish to plot it color coded by regime:
df <- data.frame(
CO = c(0, 4, 5, 23, 25, 28, 31, 2),
time = structure(c(1670585739, 1670586039, 1670586339, 1670586639, 1670586939, 1670587239, 1670587539, 1670587839), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
regime = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L), levels = c("LA", "PAn", "So"), class = "factor")
)
df
#> CO time regime
#> 1 0 2022-12-09 11:35:39 PAn
#> 2 4 2022-12-09 11:40:39 PAn
#> 3 5 2022-12-09 11:45:39 PAn
#> 4 23 2022-12-09 11:50:39 LA
#> 5 25 2022-12-09 11:55:39 LA
#> 6 28 2022-12-09 12:00:39 LA
#> 7 31 2022-12-09 12:05:39 LA
#> 8 2 2022-12-09 12:10:39 So
the data looks as follows:
> sapply(df,class)
$CO
[1] "integer"
$time
[1] "POSIXct" "POSIXt"
$regime
[1] "factor"
> summary(df)
CO time regime
Min. : 0.0 Min. :2022-12-09 11:35:39.00 LA : 431
1st Qu.:124.0 1st Qu.:2022-12-10 17:59:09.00 PAn:2384
Median :142.0 Median :2022-12-11 23:41:39.00 SoL: 37
Mean :134.5 Mean :2022-12-11 23:31:39.06
3rd Qu.:149.0 3rd Qu.:2022-12-13 05:24:09.00
Max. :176.0 3rd Qu.:2022-12-13 05:24:09.00
Max. :2022-12-14 11:04:09.00
however using ggplot the color is not shown due to regime:
ggplot(df, aes(x=time, y=CO, color = factor(regime))) +
geom_point() +
theme(legend.position = "bottom")
gives me
I am lost here since the factors of "regime" are shown in different colors at the bottom, but not color coded in the plot itself despite the fact that the class is correctly "factor" and there are no error messages whatsoever. What am I missing?
I already tried recreating the problem in an artificial data set but could not reproduce it, also using + geom_point(color=factor(regime)
did not solve the problem.