[英]ggplot legend of multiples lines/points/bar plot
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:
仅出现几行,我无法更改其名称,也无法在其中添加bar元素,这是我正在运行的代码:
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: 结果:
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 order mutate(variable = factor(fct_relevel(variable, "T_min", "T_max", "DPV_d", "P_mm")))
:定义顺序的位置 geom_blank()
: correct ordering with a blank geom at first. geom_blank()
:首先使用空白geom进行正确排序。 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"))
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.