Hi,
I am combining multiple plots using "cowplot". I want to build one legend. Here is a sample code:
plot_t = function(df,colors){
p = ggplot(df,aes(s,tr,col=strategy),size=1)+
geom_line(size=1)+
scale_x_continuous(breaks = scales::pretty_breaks(n = 5)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
labs(x='x',y='y')+
theme(legend.position = "none",
legend.title = element_text(size=18),
legend.text = element_text(size=18),
axis.title = element_text(size=18),
axis.text = element_text(size=16,color="black"),
#axis.text.x = element_text(angle = 90),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour = "gray50", linetype="dashed"),
panel.background = element_blank(),
axis.line = element_line(colour = "black")
)+
scale_fill_manual(values = colors)+
scale_color_manual(values = colors)
return(p)
}
rplots <- plot_grid(
plot_t,ploy_i,ploy_c,
align = 'vh',
labels = "AUTO",
hjust = -1,
nrow = 1
)
legend_b = get_legend(
plot_t +
guides(color = guide_legend(nrow = 1))+
theme(legend.position = "bottom"))
plots = plot_grid(rplots, legend_b, ncol = 1, rel_heights = c(1, .1))
It worked, but I just want to set the legend title manually. I tried to add the title in labs. Two legends were created. I would appreciate any suggestion.
Thanks