How to insert point and line legend in ggplot?

2

How do I insert the points into the caption? The purple dot on the caption "base1" and the red dot on the caption "base2"?

lines = 'Mes    Lg1 total1  Lg2 total2
        Jan base1   1450    base2   89
        Fev base1   1700    base2   86
        Mar base1   1500    base2   87
        Abr base1   1850    base2   61
        Mai base1   1490    base2   80
        Jun base1   1500    base2   87
        Jul base1   1600    base2   82
        Ago base1   1750    base2   64
        Set base1   1800    base2   86
        Out base1   1450    base2   73
        Nov base1   1600    base2   61
        Dez base1   1400    base2   92'

# import data
dados <-read.table(textConnection(lines),h=T) 

# show first dadoservations
head(dados)
levels(dados$Mes)
dados$Mes = factor(dados$Mes, levels=c("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", 
                                   "Jul", "Ago", "Set", "Out", "Nov", "Dez"))
library(ggplot2) 
ggplot(dados, aes(x = Mes)) +
  geom_line(aes(y = total1, color="base1"), size=1, group = 1, linetype=1) +
  geom_line(aes(y = total2*14, color="base2"), size=1, group = 2, linetype=4) +

  geom_point(aes(y = total1), color="purple", size=4, group = 1, shape=18) +
  geom_point(aes(y = total2*14), color="red", size=4, group = 2) +

  scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total de base2")) +
  labs(y = "Total de base1", x = "Tempo (meses)") + 
  scale_color_manual(name="Legenda", values=c("#35978f", "#003c30"), 
                     guide = guide_legend(override.aes=aes(fill=group))) +
  theme(legend.position=c(0.1, 0.1))

    
asked by anonymous 02.07.2018 / 00:05

1 answer

2

An alternative:

library(reshape)
dados1=melt(dados,id=c(1),c("total1","total2"))

dados1$value[dados1$variable=="total2"]=dados1$value[dados1$variable=="total2"]*14
 names(dados1)[2]="Legenda"
ggplot(dados1, aes(x=Mes, y=value, group = Legenda)) +
  geom_line(aes(linetype=Legenda,colour=Legenda), # Line type 
            size = 1.5) +       # Thicker line
  geom_point(aes(shape=Legenda),   
             size = 4) +        # Large points
  scale_shape_manual(values=c(18,19)) +                  # Change shapes
  scale_linetype_manual(values=c(1, 4))+ # Change linetypes
  scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total de base2")) +
  labs(y = "Total de base1", x = "Tempo (meses)")+
  scale_color_manual(values = c("total1" = "#35978f", "total2" = "#f03f31")) +
    theme(legend.position=c(0.1, 0.1))

    
03.07.2018 / 01:25