I have a dataframe (wi_hist_df) that contains three columns (below I have printed a part of it):
weights Frequency Criterion
1 0.2425 2 Criterion_1
2 0.2475 15 Criterion_1
3 0.2525 80 Criterion_1
4 0.2575 314 Criterion_1
5 0.2625 759 Criterion_1
6 0.2675 1296 Criterion_1
...............................
103 0.0465 29 Criterion_7
104 0.0475 18 Criterion_7
105 0.0485 6 Criterion_7
106 0.0495 2 Criterion_7
107 0.0505 2 Criterion_7
108 0.0515 1 Criterion_7
and I want to plot all Criteria together in a single plot using ggplot:
weights_ggplot = ggplot(data = wi_hist_df, aes(x = weights, y = Frequency, color = Criterion)) +
geom_line(size = 1.5) + theme_bw() +
theme(axis.text.x = element_text(size = 30),axis.text.y = element_text(size = 30) ) +
theme(axis.title.x = element_text(size = 30),axis.title.y = element_text(size = 30) ) +
theme( legend.title=element_text(size=10), legend.text=element_text(size=10) )
weights_ggplot # need to split into columns the legend
which prints the following plot
However, I would like to split the legend column into multiple column. I have tried using guides function, without any success. Any help will be appreciated.
Like this? With guides(color = guide_legend(ncol = 2))
.
library(tidyverse)
df <- tribble(
~weights, ~Frequency, ~Criterion,
0.2425, 2, "Criterion_1",
0.2475, 15, "Criterion_1",
0.2525, 80, "Criterion_1",
0.2575, 314, "Criterion_2",
0.2625, 759, "Criterion_2",
0.2675, 1296, "Criterion_2",
0.0465, 29, "Criterion_3",
0.0475, 18, "Criterion_3",
0.0485, 6, "Criterion_3",
0.0495, 2, "Criterion_4",
0.0505, 2, "Criterion_4",
0.0515, 1, "Criterion_4"
)
df |>
ggplot(aes(weights, Frequency, color = Criterion)) +
geom_line(linewidth = 1.5) +
theme_bw() +
theme(axis.text.x = element_text(size = 30),
axis.text.y = element_text(size = 30),
axis.title.x = element_text(size = 30),
axis.title.y = element_text(size = 30),
legend.title = element_text(size = 10),
legend.text = element_text(size = 10)
) +
guides(color = guide_legend(ncol = 2))
Created on 2024-04-20 with reprex v2.1.0