Using a repeating color map for faceted plot

I have a data set that looks roughly like this:
Station | Group | Date | Value | Parameter
1 | St1 | A | 2015-03-03 | 25 | N
2 | St2 | A | 2015-04-03 | 25 | N
3 | St3 | B | 2015-03-05 | 25 | N
4 | St4 | C | 2015-06-03 | 25 | N

I am creating a plot that is faceted by group. X axis is the date, Y the value. I am trying to color code the Stations (unique color per station) by facet. Right now I can make all the stations have a unique color, but over the entire data set there are a ton of stations so its very hard to see the differences. Each group though only has about 5-8 stations each.

What I'm trying to accomplish is a way to use the same 5-8 colors in each facet, even though they will correspond to different stations. I'm totally stumped on this, any help is appreciated!!

Welcome to the community!

Have you seen this thread?

I think if you create a dummy variable by converting Station to factor for each Group, and then set that as the colour variable in ggplot, you'll get the desired results.

Like this:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

dataset <- tibble(Station = c("St1", "St2", "St3", "St4"),
                  Group = c("A", "A", "B", "C"),
                  Date = c("3.3.15", "3.4.15", "5.3.15", "3.6.15"),
                  Value = c(25, 25, 25, 25),
                  Parameter = c("N", "N", "N", "N"))

dataset %>%
  group_by(Group) %>%
  mutate(dummy_var = as.character(x = factor(x = Station,
                                             labels = seq_len(length.out = n_distinct(x = Station))))) %>%
  ungroup() %>%
  ggplot(mapping = aes(x = Date,
                       y = Value)) +
  geom_point(mapping = aes(colour = dummy_var)) +
  facet_wrap(facets = (~ Group)) +
  scale_color_discrete(guide = FALSE)

Created on 2019-08-22 by the reprex package (v0.3.0)

Hope this helps.

Yes I think this did it! I'll need to add the legends to the side of each facet to be sure but looks like its working as intended.

Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.