Reputation: 115
So, I'm having trouble with the legend of a multiple line/point/bar plot. Just the lines appears and I couldn't change its names neither add the bar element to it, here's the code I´m running:
df.rain %>%
ggplot(aes(x = date))+
labs(y = "Rain (mm); Temperature (°C)")+
geom_bar(aes(y = P_mm),
fill = "honeydew4",
stat = "identity",
width = 5)+
geom_point(aes(y = T_max), shape = 2)+
geom_line(aes(y = T_max, linetype = "dashed"))+
geom_point(aes(y = T_min), shape = 3)+
geom_line(aes(y = T_min, linetype = "solid"))+
geom_point(aes(y = DPV_d*15), shape = 1)+
geom_line(aes(y = DPV_d*15, linetype = "dotted"))+
scale_y_continuous(sec.axis = sec_axis(trans = ~ . /15 ,
name = "DPV (kPa)"))+
scale_x_date(date_breaks = "2 month",
date_minor_breaks = "1 month",
date_labels = "%b %Y") +
theme_bw() +
theme(axis.title.x = element_blank())
And the results:
Upvotes: 0
Views: 1735
Reputation: 3948
Here is a possible solution, with mock data :
EDIT: : I added two lines to control legend labels :
mutate(variable = factor(fct_relevel(variable, "T_min", "T_max", "DPV_d", "P_mm")))
: where you define the ordergeom_blank()
: correct ordering with a blank geom at first.PLOT:
Code:
library(tidyverse)
w_dat <- tibble(date = seq(as.Date("2017-01-01"),
as.Date("2017-12-01"),
by = "month"),
P_mm = sample(20:150, 12, replace = TRUE),
T_max = sample(15:30, 12, replace = TRUE),
T_min = T_max * sample(seq(0.5, 0.9, 0.05), 12, replace = TRUE),
DPV_d = sample(seq(0.5, 2, 0.1), 12, replace = TRUE))
w_dat %>%
mutate(DPV_d = DPV_d * 15) %>%
gather(variable, value, -date) %>%
mutate(variable = factor(fct_relevel(variable, "T_min", "T_max", "DPV_d", "P_mm"))) %>%
ggplot(data = ., aes(x = date, y = value, shape = variable, linetype = variable, fill = variable)) +
geom_blank() +
geom_bar(data = . %>% filter(variable == "P_mm"),
stat = "identity",
width = 5) +
geom_point(data = . %>% filter(variable != "P_mm")) +
geom_line(data = . %>% filter(variable != "P_mm")) +
labs(y = "Rain (mm); Temperature (°C)") +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . /15 ,
name = "DPV (kPa)")) +
scale_x_date(date_breaks = "2 month",
date_minor_breaks = "1 month",
date_labels = "%b %Y") +
theme_bw() +
theme(axis.title.x = element_blank()) +
scale_shape_manual("The legend you want",
values = c("T_min" = 3,
"T_max" = 2,
"DPV_d" = 1,
"P_mm" = NA)) +
scale_linetype_manual("The legend you want",
values = c("T_min" = "solid",
"T_max" = "dashed",
"DPV_d" = "dotted",
"P_mm" = "blank")) +
scale_fill_manual("The legend you want",
values = c("T_min" = "white",
"T_max" = "white",
"DPV_d" = "white",
"P_mm" = "honeydew4"))
Upvotes: 1
Reputation: 115
Not exactly what I was looking for, couldn't merge point and linetpe, but manage to produce a meaningful legend
df.rain %>%
ggplot(mapping = aes(x = date))+
labs(y = "Rain (mm); Temperature (°C)")+
geom_bar(aes(y = P_mm, fill = "Chuva"),
stat = "identity",
width = 5)+
geom_point(aes(y = T_max, shape = "tmax"))+
geom_line(aes(y = T_max), linetype = "dashed")+
geom_point(aes(y = T_min, shape = "tmin"))+
geom_line(aes(y = T_min), linetype = "solid")+
geom_point(aes(y = DPV_d*45, shape = "dpv"))+
geom_line(aes(y = DPV_d*45), linetype = "dotted")+
scale_y_continuous(sec.axis = sec_axis(trans = ~ . /45 ,
name = "DPV (kPa)"))+
scale_x_date(date_breaks = "2 month",
date_minor_breaks = "1 month",
date_labels = "%b %Y") +
scale_fill_manual(name = "", values = c("Chuva" = "honeydew4")) +
scale_shape_manual(name = "", values = c("tmax" = 2, "tmin" = 3, "dpv" = 1), labels = c("T° max", "T° min", "DPV")) +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 1,
size = 9, hjust = 1),
legend.position = "bottom",
legend.text = element_text(size = 8),
legend.box.background = element_rect(),
legend.box.margin = margin(0.6, 0.6, 0.6, 0.6))
Upvotes: 0