Possible to match geom_text colors to fill colors (RColorBrewer)?

I’ve been hunting around for a way to do this, if even possible. In the plot below is there a way the geom_text annotations such as Loggerhead Total: 407 matches the color for “Cc” which is part of “species” used to specify fill color for the bar plots..

Thanks,
Jeff

library(tidyverse)
library("lubridate")
#> Loading required package: timechange
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library("RColorBrewer")  
library("reprex")
start_date <- ymd("2022-04-15")
end_date <- ymd("2022-10-31")
position_date <- ymd("2022-05-05") #place on x-axis
last_report_date <- ymd("2022-10-31")

date_length <-days(start_date %--% end_date )
position_date_len <- days(start_date %--% position_date)
relative_date <- position_date_len/date_length
rel_y <- 13/18
coeff <- 25 

turtle_activity_gtm <-  read_csv("https://www.dropbox.com/s/7ubvhajvkhx53kc/mpt_act_rep_cum_fc_cum_nest.csv?dl=1")
#> Rows: 765 Columns: 66
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (22): beach, county, activity, ref_no, activity_comments, encountered?,...
#> dbl  (35): uid, activity_no, fcrawl, fcrawl_cum, clutch, clutch_cum, nest_no...
#> lgl   (6): final_treatment, light_management, relocation_reason, lost_nest, ...
#> date  (3): activity_date, emerge_date, inventory_date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Above file has cumulative false crawls and cumulative nests added along with proper date format.
cc_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Cc'))
dc_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Dc'))
cm_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Cm'))
lk_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Lk'))

#suppressWarnings(print(
ggplot(data = (turtle_activity_gtm |> filter(activity=="N"))) + 
  scale_fill_brewer(palette = "Dark2") +
  scale_color_brewer(palette = "Dark2") +
  geom_bar(aes(x = emerge_date, alpha=.5),
           na.rm = TRUE,
           position = position_dodge2(preserve = "total")) +
  geom_bar(aes(x = activity_date, fill = species),
           na.rm = TRUE,
           position = position_dodge2(preserve = "single")) +
  scale_y_continuous(breaks = 0:18, expand = c(0,0), 
                     sec.axis = sec_axis(~.*coeff, name="Total Nests/False Crawls",
                                         breaks = seq(0,450,25))) +
  expand_limits(y = c(-.1, 18)) +
  scale_x_date(breaks = c(seq.Date((start_date), (end_date), 
                                   by = '5 days'), end_date), 
               date_labels = "%m/%d",
               date_minor_breaks = "1 day",
               limits = c( start_date, end_date),
               expand = c(0,0))   +
  labs(x="",y="Number of Nests", title=paste("2022 Daily Sea Turtle Nest Count GTM S & N as of",month(last_report_date),"/",mday(last_report_date)), 
       caption='JeffF') +
  theme(axis.text.y = element_text(face = "bold",  
                                   size = 10, angle = 0),
        axis.text.x = element_text(face = "bold",  
                                   size = 10, angle = -80),
        legend.position=c(x=relative_date, y=rel_y),
        legend.justification = c(0, 1), #upper left
        legend.title = element_blank(),
        axis.title = element_text(size = 12, face = "bold"),
        plot.caption = element_text(size = 15)) +
  #theme(legend.position = "none") +
  geom_smooth(data = turtle_activity_gtm, method = 'loess', formula = 'y ~ x',
              aes(x=activity_date, y=fcrawl_cum/coeff),linetype = "111111",
              linewidth = 1.5, color = '#2F5597',  span = 0.08, se = FALSE) +
  geom_smooth(data = turtle_activity_gtm, method = 'loess', formula = 'y ~ x', 
              aes(x=activity_date, y=clutch_cum/coeff), linewidth = 1.5, 
              color ='#ED7D31', span = 0.08, se = FALSE)  +
  geom_text(x=ymd(max(turtle_activity_gtm$activity_date+1,na.rm = TRUE)), 
            y=18*(max(turtle_activity_gtm$fcrawl_cum/450)),
            hjust = "left",label=paste(max(turtle_activity_gtm$fcrawl_cum), 
                                       'False Crawls'  )) +
  geom_text(x=ymd(max(turtle_activity_gtm$activity_date+1,na.rm = TRUE)), 
            y=18*(max(turtle_activity_gtm$clutch_cum/450)), 
            hjust = "left",label=paste(max(turtle_activity_gtm$clutch_cum),
                                       'Nests')) +
  geom_text(x=ymd("2022-05-20"), y=17,  hjust = "right", 
            label=paste("Loggerhead Total: ")) +
  geom_text(x=ymd("2022-05-20")+days(4), y=17,  hjust = "right",
            label=paste(cc_total)) + 
  geom_text(x=ymd("2022-05-20"), y=16.5,  hjust = "right",
            label=paste("Leatherback Total: ")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=16.5,  hjust = "right",
            label=paste(dc_total)) + 
  geom_text(x=ymd("2022-05-20"), y=16,  hjust = "right",
            label=paste("Green Total: ")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=16,  hjust = "right",
            label=paste(cm_total)) + 
  geom_text(x=ymd("2022-05-20"), y=15.5,  hjust = "right",
            label=paste("Kemps Ridley Total: ")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=15.5,  hjust = "right",
            label=paste(lk_total))
#> Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
#> Removed 1 rows containing non-finite values (`stat_smooth()`).

Created on 2023-01-18 with reprex v2.0.2

Yes.

library(ggplot2)
the_pal <- RColorBrewer::brewer.pal(n = 6,"Dark2")

p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))

p + geom_text(color = the_pal[3])  + theme_minimal()

image

Thanks. Gave me the guidance to achieve what I wanted although in the reprex below, the annotations get squished together.

library(tidyverse)
library("lubridate")
#> Loading required package: timechange
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library("RColorBrewer")  
library("reprex")
start_date <- ymd("2022-04-15")
end_date <- ymd("2022-10-31")
position_date <- ymd("2022-05-05") #place on x-axis
last_report_date <- ymd("2022-10-31")

date_length <-days(start_date %--% end_date )
position_date_len <- days(start_date %--% position_date)
relative_date <- position_date_len/date_length
rel_y <- 13/18
coeff <- 25 

the_pal <- RColorBrewer::brewer.pal(n = 8,"Dark2")

turtle_activity_gtm <-  read_csv("https://www.dropbox.com/s/7ubvhajvkhx53kc/mpt_act_rep_cum_fc_cum_nest.csv?dl=1")
#> Rows: 765 Columns: 66
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (22): beach, county, activity, ref_no, activity_comments, encountered?,...
#> dbl  (35): uid, activity_no, fcrawl, fcrawl_cum, clutch, clutch_cum, nest_no...
#> lgl   (6): final_treatment, light_management, relocation_reason, lost_nest, ...
#> date  (3): activity_date, emerge_date, inventory_date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Above file has cumulative false crawls and cumulative nests added along with proper date format.
cc_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Cc'))
dc_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Dc'))
cm_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Cm'))
lk_total <- count(turtle_activity_gtm |> filter(activity=="N") |> filter(species =='Lk'))

last_activity_date <- max(turtle_activity_gtm$activity_date,na.rm = TRUE )
last_emerge_date <- max(turtle_activity_gtm$emerge_date,na.rm = TRUE )

if (last_activity_date > last_emerge_date) {
  last_report_date = last_activity_date
} else {
  last_report_date = last_emerge_date 
}

#suppressWarnings(print(
myplot <- ggplot(data = (turtle_activity_gtm |> filter(activity=="N"))) +
  geom_bar(aes(x = emerge_date,  fill = activity, alpha = 0.5),
           na.rm = TRUE, 
           position = position_dodge2(preserve = "total")) + 
  scale_fill_brewer(palette = "Dark2") +
  scale_color_brewer(palette = "Dark2") +
  geom_bar(aes(x = activity_date, fill = species),
           na.rm = TRUE,
           position = position_dodge2(preserve = "single")) +
  scale_y_continuous(breaks = 0:18, expand = c(0,0), 
                     sec.axis = sec_axis(~.*coeff, name="Total Nests/False Crawls",
                                         breaks = seq(0,450,25))) +
  expand_limits(y = c(-.1, 18)) +
  scale_x_date(breaks = c(seq.Date((start_date), (end_date), 
                                   by = '5 days'), end_date), 
               date_labels = "%m/%d",
               date_minor_breaks = "1 day",
               limits = c( start_date, end_date),
               expand = c(0,0)) +
  theme_bw() +
  labs(x="",y="Number of Nests/Emergences", 
       title=paste("2022 Daily Sea Turtle Activity GTM S & N as of",
                   month(last_report_date),"/",mday(last_report_date)), 
                               caption='JeffF')  +
  theme(axis.text.y = element_text(face = "bold",  
                                   size = 12, angle = 0),
        axis.text.x = element_text(face = "bold",  
                                   size = 12, angle = 0),
        legend.position=c(x=relative_date, y=rel_y),
        legend.justification = c(0, 1), #upper left
        legend.title = element_blank(),
        axis.title = element_text(size = 15, face = "bold"),
        title = element_text(size = 20, face = "bold"),
        plot.title = element_text(hjust = 0.5),
        plot.caption = element_text(size = 15))  +
  theme(legend.position = "none") +
  geom_smooth(data = turtle_activity_gtm, method = 'loess', formula = 'y ~ x',
              aes(x=activity_date, y=fcrawl_cum/coeff),linetype = "111111",
              linewidth = 1.5, color = the_pal[6],  span = 0.08, se = FALSE) +
  geom_smooth(data = turtle_activity_gtm, method = 'loess', formula = 'y ~ x', 
              aes(x=activity_date, y=clutch_cum/coeff), linewidth = 1.5, 
              color = the_pal[7], span = 0.08, se = FALSE)  +
  geom_text(x=ymd(max(turtle_activity_gtm$activity_date+1,na.rm = TRUE)), 
            y=18*(max(turtle_activity_gtm$fcrawl_cum/450)), size = 5,
            hjust = "left",label=paste(max(turtle_activity_gtm$fcrawl_cum), 
                                       'False Crawls'  )) +
  geom_text(x=ymd(max(turtle_activity_gtm$activity_date+1,na.rm = TRUE)), 
            y=18*(max(turtle_activity_gtm$clutch_cum/450)), size = 5,
            hjust = "left",label=paste(max(turtle_activity_gtm$clutch_cum),
                                       'Nests')) +
 # geom_text(color = the_pal[3]) +
  geom_text(x=ymd("2022-05-20"), y=17, size = 6,  fontface = "bold",  
            hjust = "right", color = the_pal[1],
            label=paste("Loggerhead Total:")) +
  geom_text(x=ymd("2022-05-20")+days(4), y=17, size = 6,  fontface = "bold",  
            hjust = "right", color = the_pal[1],
            label=paste(cc_total)) + 
  geom_text(x=ymd("2022-05-20"), y=16, size = 6,  fontface = "bold", 
            hjust = "right", color = the_pal[2],
            label=paste("Green Total:")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=16, size = 6, fontface = "bold", 
            hjust = "right", color = the_pal[2],
            label=paste(" ",cm_total))  + 
  geom_text(x=ymd("2022-05-20"), y=15, size = 6,  fontface = "bold",  
            hjust = "right", color = the_pal[3],
            label=paste("Leatherback Total:")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=15, size = 6,  fontface = "bold",  
            hjust = "right", color = the_pal[3],
            label=paste("  ",dc_total)) + 
  geom_text(x=ymd("2022-05-20"), y=14, size = 6,  fontface = "bold",
            hjust = "right", color = the_pal[4],
            label=paste("Kemps Ridley Total:")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=14, size = 6,  fontface = "bold",  
            hjust = "right", color = the_pal[4],
            label=paste("  ",lk_total))  + 
  geom_text(x=ymd("2022-05-20")+days(4), y=14, size = 6,  fontface = "bold",  
            hjust = "right", 
            label=paste("___")) + 
  geom_text(x=ymd("2022-05-20")+days(4), y=13, size = 6,  fontface = "bold",  
            hjust = "right", 
            label=max(turtle_activity_gtm$clutch_cum)) +  
  geom_text(x=ymd("2022-08-20"), y=11,  size = 6, hjust = "right", color = the_pal[5],
            label=paste("Emergences"))

myplot
#> Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
#> Removed 1 rows containing non-finite values (`stat_smooth()`).

Created on 2023-01-19 with reprex v2.0.2

You can use hjust to move the upper left block, add a ’\n` to the labels for the axes and touch up the scales, if needed. New post if help needed.

Yes, thanks. I had made some adjustments and then figured out how to save a file to show what the final output looks like. I may ask in a separate post about sizing and positioning annotations such the annotations are largely the same whether in RStudio or some output file.

However, I have achieved this:

RStudio screenshot:

After:

ggsave("myplot.jpeg", dpi = 300, width=60, height=15, units = "cm")

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.