How to insert caption in graph with two axes and in r?

4

How do I insert the legend in this chart, what kind of bar would be the gross total and the line the total net?

lines = 'Mes Acid   Obt
Jan 1450    102
Fev 1447    86
Mar 1461    87
Abr 1356    61
Mai 1398    80
Jun 1115    87
Jul 1211    82
Ago 1089    64
Set 1246    86
Out 1128    73
Nov 1204    61
Dez 1435    92'

# Importando dados
obs <-read.table(textConnection(lines),h=T) 

# Visualizando a tabela criada
head(obs)

# Niveis
levels(obs$Mes)

# Ordenando niveis
obs$Mes = factor(obs$Mes, levels=c("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", 
                                   "Jul", "Ago", "Set", "Out", "Nov", "Dez"))
#Gráfico
library(ggplot2) 
ggplot(obs, aes(x = Mes)) +
   geom_bar(aes(y = Acid), fill="darkblue", stat = "identity", alpha=0.4) +
   geom_point(aes(y = Obt*14), color="#a50026", size=2, group = 1) +
   geom_line(aes(y = Obt*14), color="#a50026", size=1, group = 1) +
   scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total líquido")) +
   labs(y = "Total bruto", x = "Mês")

    
asked by anonymous 19.06.2018 / 17:54

2 answers

3

Because the bars use fill and the rows / dots use color , you can specify separate scales for each. Enter the name you want in the legend within the respective aesthetics and colors in the manual scale options. In this way, the legend keys will match the type of geometry you are using.

ggplot(obs, aes(x = Mes)) +
  geom_bar(aes(y = Acid, fill="bruto"), stat = "identity", alpha=0.4) +
  geom_point(aes(y = Obt*14, color="líquido"), size=2, group = 1) +
  geom_line(aes(y = Obt*14, color="líquido"), size=1, group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total líquido")) +
  scale_fill_manual("Totais:      ", values = "darkblue") +
  scale_color_manual(NULL, values = "#a50026") +
  labs(y = "Total bruto", x = "Mês") +
  theme(legend.position = "bottom") +
  guides(fill = guide_legend(order = 1), color = guide_legend(order = 2))

    
07.07.2018 / 18:52
3

I think the code below solves the problem in a satisfactory way, at least according to my criteria.

ggplot(obs, aes(x = Mes)) +
  geom_bar(aes(y = Acid, color="Bruto"), fill="darkblue", stat = "identity", 
    alpha=0.4) +
  geom_point(aes(y = Obt*14), color="#a50026", size=2, group = 1) +
  geom_line(aes(y = Obt*14, color="Líquido") , size=1, group = 1, linetype=1) +
  scale_y_continuous(sec.axis = sec_axis(~./14, name = "Total líquido")) +
  labs(y = "Total bruto", x = "Mês") +
  scale_color_manual(name="Totais", values=c("#9c9ec5", "#a50026"), 
    guide = guide_legend(override.aes=aes(fill=NA)))

The key is to map the colors of the bars and the line within their respective aes . Note that I have already called these colors Gross and Liquid , respectively. Oh, and I set a color for the contour of the bar, because on the initial chart only the fill was set (not that this is a problem).

Next, you just created a scale_color_manual , set the caption name and color values. The problem I encountered was setting the blue color correctly. Originally a darkblue , but since a alpha=0.4 was applied, the color we perceive is a little clearer. So, I had to detect exactly what the blue tone of the bar was with the alpha applied. In this case, it gave # 9c9ec5.

Unfortunately, I do not know a way to combine darkblue (or any other color whatsoever) with alpha in the caption automatically to generate the desired color.

    
19.06.2018 / 19:54