GGPLOT-多个图例的顺序(geom_line和geom_line加上geom_point)

时间:2020-01-10 17:38:13

标签: r ggplot2 graph graphics

我搜索了许多已经回答的问题,但是没有一个可以帮助我。

这是我的代码:

ggplot(z, aes(x=`Fecha de muestreo`, y = Valor, color = `Nombre de la estación`)) + 
  geom_line(size = 0.4) + 
  geom_point(size=0.5) + 
  scale_x_date(date_labels = "%b", breaks = "1 month", limits = c(as.Date("2019-1-1"), as.Date("2019-12-20"))) + 
  scale_y_continuous("pH (unidades de pH)", limits = c(3, 11), breaks = c(3:11)) +
  geom_hline(aes(yintercept = 6.5, linetype = "ECA cat.3 inf D1 y D2 : 6.5"), colour = "Blue", size = 0.4)+
  geom_hline(aes(yintercept = 8.4, linetype = "ECA cat.3 sup. D1 : 8.4"), colour = "Green", size = 0.4)+
  geom_hline(aes(yintercept = 8.5, linetype = "ECA cat.3 sup. D2 : 8.5"), colour = "red", size = 0.4)+
  scale_linetype_manual(name = NULL, values = c(1, 1, 1), # values = tipo de lineas
                        guide = guide_legend(override.aes = list(color = c("blue", "Green", "Red")))) +
  theme(axis.text=element_text(size=6), 
        legend.margin = unit(-0.2, "cm"),
        axis.title=element_text(size=7,face="bold")) +
  theme(legend.text=element_text(size=6), 
        legend.title = element_blank(), 
        legend.spacing.x = unit(.2, 'cm')) + 
  theme(legend.key = element_rect(fill = "white")) +
  theme(panel.background = element_rect(fill = "white")) +
  theme(panel.grid = element_line(colour= "gray"))

这是图形:

enter image description here

我需要更改图例的顺序:“ ECAS”应位于底部。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

使用order的{​​{1}}一词应该可以实现。例如:

guides()

enter image description here

要颠倒顺序,我们可以添加:(注意,顺序似乎是从下至上)

library(ggplot2)
a <- ggplot(mtcars, aes(wt, mpg, 
                   color = as.character(cyl), 
                   fill = as.character(gear))) +
  geom_point(size = 5, shape = 21, stroke = 2) +
  scale_color_discrete(name = "cyl as color") +
  scale_fill_discrete(name = "gear as fill")
a  

enter image description here

相关问题