0

I have a two graphs that have the same legend and therefore I only need one of them. But, I also want to attach a table next to my graphs to provide more information. Is there a way to extract the colors from the legend and put them in the table?

Or could I add the "Best Model" and "Individual" column into my legend and therefore I wouldn't have to worry about combining the graphs with a table?

Here is what my full graph and table output looks like: enter image description here

The code to make that looks like this:

p2 <-ggplot(data = graph_HR_together,
       aes(x = Hour, y = Heart_rate, color = Turtle)) +
  stat_summary(fun = mean, geom = "line", aes(group = Turtle)) +
  labs(y = "Heart Rate", x = "Hour") +
  scale_color_brewer(palette = "Paired")+
  theme_gray() +
  theme(panel.grid.major = element_blank(), 
        axis.line = element_line(colour = "grey50"), 
       legend.position = "bottom", 
       legend.box = "horizontal")+
  facet_wrap(~Cluster) +  
  labs(title = "Best Model Coefficients")


### individual coefficients model 

# by the hour

p3 <- ggplot(data = graph_HR_individual,
       aes(x = Hour, y = Heart_rate, color = Turtle)) +
  stat_summary(fun = mean, geom = "line", aes(group = Turtle)) +
  labs(y = "Heart Rate", x = "Hour") +
  scale_color_brewer(palette = "Paired")+
  theme_gray() +
  theme(panel.grid.major = element_blank(), 
        axis.line = element_line(colour = "grey50"), 
        legend.position = "none")+
  facet_wrap(~Cluster) +
  labs(title = "Individual coefficients model")

library(patchwork)

my_table_together <- as.data.frame(table(graph_HR_together$Cluster, graph_HR_together$Turtle)) %>%
  filter(Freq != 0) %>%
  dplyr::select(-Freq)

my_table_individual <- as.data.frame(table(graph_HR_individual$Cluster, graph_HR_together$Turtle)) %>%
  filter(Freq != 0) %>%
  dplyr::select(-Freq)

my_table <- merge(my_table_together, my_table_individual, by = "Var2") %>%
  rename("Turtle" = "Var2", "Best Model" = "Var1.x", "Individual" = "Var1.y")

library("ggpmisc")

ggp_table <- ggplot() + 
  theme_void() +
  annotate(geom = "table",
           x = 1,
           y = 1,
           label = list(my_table)) 

p1 <- (p2 / p3 )

cowplot::plot_grid(p1, ggp_table, nrow = 1, ncol = 2, rel_widths = c(3, 1))

My two data sets that are being used look like this. They're big so I can't paste the whole thing...

I can't paste the data due to privacy and the fact that it's way to much and wouldn't even fit in this post. Basically, there are 11 different turtles that were categorized by different Clusters in graph_HR_individiual and graph_HR_together. Those clusters are what I want displayed in the table.

Let me know if you have questions as this might be confusing. But, I can't post the data, and even if I did just post 100 rows, it would only give the first turtle anyway.

Cassidy
  • 395
  • 1
  • 11
  • I've taken the liberty to close this question - I would be surprised if the many answers in that thread should not help you. If you struggle, give me a shout – tjebo Jan 26 '23 at 15:44

0 Answers0