So I have two systems executing two benchmarks from which I collect two metrics.
df1 <- data.frame(Benchmark = c("Benchmark1", "Benchmark2"),
Metric1 = c(120, 200),
Metric2 = c(200, 150))
df2 <- data.frame(Benchmark = c("Benchmark1", "Benchmark2"),
Metric1 = c(100, 150),
Metric2 = c(200, 180))
Now I prepare this dataframe for plotting with ggplot
df <- left_join(df1, df2, by = "Benchmark") %>%
gather(Metric,Value,2:5) %>%
mutate(System = ifelse(grepl(".x", Metric), "System1", "System2"),
Metric = ifelse(grepl("1" , Metric), "Metric1", "Metric2"))
And I can get a nice chart like this
ggplot(df %>% filter(Metric == "Metric1"), aes(x = Benchmark, y = Value, fill = System)) +
geom_col(position = "dodge")
Now I want to add a new set of bars with the geomean of those metrics, for each of those systems.
My dataframe needs to contain 2 x 2 = 4 new rows for each (System, Metric) combination containing the geomean of the values of the benchmarks for each (System, Metric) combination.
I know I can use base R to select data frame columns matching a criteria, getting the mean and then manually entering new rows using bind_rows. Is there a more automated way to accomplish this using dplyr? Perhaps with some combination of group_by() with other function?
Thanks in advance.