2

I want to save the colours that automatically ggplot function assing to each station in a plot. I want to save the colour assigned to each station in a palette that I can reuse again in others plot:

ggplot(DSF_moments, aes(x=year, y=max, group = station, colour = station)) + 
  geom_line(size = 1)  + 
  geom_point(size=1.5, shape=21, fill="white") + 
  labs(y ="Annual max flow [m3/s]", x = "year", title = "Annual Maximum Streamflow", size = 50) +
  theme(plot.title = element_text(size=16), axis.text.y = element_text(size=11), axis.text.x = element_text(angle = 90, size=11)) + scale_x_continuous (breaks=seq(min(DSF_moments$year),max(DSF_moments$year),by=2)) +
  scale_y_continuous (breaks=seq(min(DSF_moments$max),max(DSF_moments$max),by=5000))
dev.copy(png,"Plot_Max_Annual_RawData.png",width=22,height=11,units="in",res=100)
dev.off()

Using the colour function in the code above, ggplot assign a colour to each station, I do not want to change the colours, I only want to know which colour is assigned to each station. The idea is to generate after a plot separately for each station but maintaining the colours previously assigned in the first common plot with all the stations.

for (i in 1:length(listDF2)) 
{
  df1 <- as.data.frame(listDF2[[i]])
  df1[is.na(df1)] <- 0
  temp_plot <- ggplot(df1, aes(x = day, y = DailyMeanStreamflow, colour=Station[i])) +
  geom_line(size = 1)  + 
  geom_point(size=1.5, shape=21, fill="white") + 
  facet_wrap(~ month, ncol = 3) +
  labs(title = "Daily Mean Streamflow",
       subtitle = "Data plotted by month",
       y = "Daily Mean Streamflow [m3/s]", x="Days") + 
  scale_x_continuous (breaks=seq(1,max(df1$day),by=1)) + theme(axis.text.x = element_text(size=9))

  print(temp_plot)

  name4<- paste("DailyStreamflow_byMonth","_", siteNumber[i], ".png", sep="")
  ggsave(temp_plot,filename = name4,width=22,height=11,units="in",dpi=500)
  dev.off()
}

I want to assign now to each graph the colour assigned previously. How can I save the assigned default colours by ggplot to each station?

Stations are in format chr: "094985005","09498501","09489500"

Sss
  • 427
  • 2
  • 8
  • Create your own palette or use function from above link to re-generate ggplots default colours. – zx8754 Sep 24 '20 at 11:21
  • 1
    No, this not answer, because I do not want to emulate, I want to save them – Sss Sep 24 '20 at 11:31
  • Please have a look at this answer from the link - https://stackoverflow.com/a/34241551/680068 Let me know if it doesn't work, I will re-open. – zx8754 Sep 24 '20 at 11:46
  • Also, provide reproducible data. – zx8754 Sep 24 '20 at 11:48
  • This answer says how to create a palette but not how to assign each colour to each station. I will modify the question to explain it better – Sss Sep 24 '20 at 12:02
  • "station" is sorted then assigned default colours. If you know how they will be sorted, then we can easily match to default colors orders. In any case, provide example data, I will re-open. – zx8754 Sep 24 '20 at 12:05
  • I modified it, I hope it is now more clear what I want – Sss Sep 24 '20 at 13:14

2 Answers2

1

The answers linked in the comments have a ton of great information, what I show here is based off of that.

# Generate the colors 
stations = unique(DSF_moments$station)
station_cols = scales::hue_pal()(length(stations))
# Assign them alphabetically (ggplot's default, which you don't seem to modify) names(station_cols) = sort(stations)

# use these colors for (some) of these stations in a plot with 
scale_color_manual(values = station_cols)

Since you haven't shared any data, this is untested, but it should get you at least very close. If you need more help, please share a reproducible example.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
1

Another way is to keep factor levels same for all dataframes, see example:

# example data
listdf <- list(
  data.frame(x = 1:1, y = 1:2, station = c("094985005","09498501")),
  data.frame(x = 1:1, y = 2:3, station = c("09498501","09489500"))
)

#fix levels
allStations <- sort(unique(unlist(lapply(listdf, "[[", "station"))))
listdf[[1]]$station <- factor(listdf[[1]]$station, levels = allStations)
listdf[[2]]$station <- factor(listdf[[2]]$station, levels = allStations)

#plot side by side to illustrate the same levels
cowplot::plot_grid(
  ggplot(listdf[[1]], aes(x, y, col = station)) +
    geom_point(size = 5) +
    scale_color_discrete(drop = FALSE) +
    ylim(0, 3),
  
  ggplot(listdf[[2]], aes(x, y, col = station)) +
    geom_point(size = 5) +
    scale_color_discrete(drop = FALSE) +
    ylim(0, 3)
)

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    My answer does exactly what OP asks for. This answer shows a better method - the one I would use in my own code to accomplish OP's goal. (Unless they want to save the colors for use outside of `ggplot`...) – Gregor Thomas Sep 24 '20 at 14:00
  • @GregorThomas yes, OP wants to plot multiple dataframes, where levels are different but colours should match to make plots comparable. So, we just need to be sure every station gets assigned the exact same colour. Fixing levels is fine, in my own codes I'd assign custom colours for each station, and use scale_color_identity. – zx8754 Sep 24 '20 at 14:07