'I cannot add source to this chart, nor can I remove legend title

I have created a simple interactive chart, of Brazil bank NPLs (no longer interactive as saved as a png) using the below code, but need to refine the aesthetics a bit more to exclude the legend title (series.name) and also the source (caption) does not show at the bottom of the chart. I am relatively new to R, but I suspect the reason is that I am using the ggplot2 and plotly packages in combination? These functions work in my other charts which are all static. I would really appreciate some guidance here.

library(GetBCBData)
library(tidyverse)
library(plotly)
library(ggplot2)
id.series <- c(Total_NPL = 21082,
             Corps_NPL = 21083,
             Indivs_NPL = 21084)
first.date = '2011-09-01'
# get series from bcb
 df_cred <- gbcbd_get_series(id = id.series,
                             first.date = first.date,
                             last.date = Sys.Date(),
                             use.memoise = FALSE)
  # check output
   glimpse(df_cred)
 p <- ggplot(df_cred, aes(x =ref.date, y = value, colour = series.name)) +
   geom_line() + 
   scale_colour_manual(values = c("chartreuse3","deepskyblue3", "darkorange2"))+
   labs(y = "% of total loans",
          x = '')+
   labs(y = "% of total loans",
                   x = '',
                   title = "NPL Ratios Brazil % loans ",
                   caption = "Source: Source: SGS - BCB (by GetBCBData)") +
   scale_x_date(date_breaks = "1 year", date_labels = "%b %y")+
   theme(panel.background = element_rect(fill = "azure1"))+
   theme(panel.grid.major = element_line(colour = "grey", linetype = "dotted"))+
   theme(legend.position="top") +
   theme(legend.title = element_blank())+
   theme(legend.text = element_text(size = 12))+
   theme(plot.title = element_text(face = "bold", size = 18))+
   geom_text(show.legend = FALSE,aes(label = value),
                   data = df_cred %>% filter(ref.date == max(ref.date)),
                   nudge_x = 70,
                   nudge_y = 0.08,
                   size = 4.5)+
   scale_y_continuous(limits = c(0,NA), expand = c(0,0))+
   geom_hline(yintercept=0)
   ggplotly(p) 


Solution 1:[1]

In your ggplot call I did a few things differently. In scale_colour_manual, I added name = "". This removed the legend title.

You called labs two times, I commented one set out. You called element_blank() for layout(legend.title... but without a name, it wasn't needed. (Removing the name through scale removed it from plotly, as well.)

After creating the ggplotly object, I adjusted the margins so that the legend and caption would be visible. I also grayed out the gridlines. FYI, dotted gridlines are currently not an option in plotly. Although, it is a current documented feature request.

One last thing-- you had "source: source:..." In the ggplotly call, I removed one of the "source:".

Your slightly modified ggplot call:

p <- ggplot(df_cred, aes(x =ref.date, y = value, colour = series.name)) +
  geom_line() + 
  scale_colour_manual(values = c("chartreuse3","deepskyblue3", "darkorange2"),
                      name = "") +
  # labs(y = "% of total loans",
  #      x = '')+
  labs(y = "% of total loans",
       x = '',
       title = "NPL Ratios Brazil % loans ",
       caption = "Source: Source: SGS - BCB (by GetBCBData)") +
  scale_x_date(date_breaks = "1 year", date_labels = "%b %y") +
  theme(panel.background = element_rect(fill = "azure1"))+
  theme(panel.grid.major = element_line(colour = "grey", linetype = "dotted"))+
  theme(legend.position="top") +
  # theme(legend.title = element_blank())+  # this is not doing anything
  theme(legend.text = element_text(size = 12))+
  theme(plot.title = element_text(face = "bold", size = 18))+
  geom_text(show.legend = FALSE,aes(label = value),
            data = df_cred %>% filter(ref.date == max(ref.date)),
            nudge_x = 70,
            nudge_y = 0.08,
            size = 4.5)+
  scale_y_continuous(limits = c(0,NA), expand = c(0,0)) +
  geom_hline(yintercept=0)

Updates post ggplotly:

ggplotly(p) %>% 
  layout(
    margin = list(b = 50, t = 75),  # make space to see the legend/caption
    legend = list(                  # legend placement
      orientation = "h",
      y = 1.06, x = .5, xanchor = "center",
      font = list(size = 12)),
    annotations = list(             # caption and placement
      align = "right", showarrow = F, y = -.08, x = 1,
      # x and y values uses the domain [0,1]
      font = list(size = 10), 
      text = "Source: SGS - BCB (by GetBCBData)",
      xref = "paper", yref = "paper"),        # allow off the plot
    xaxis = list(gridcolor = "lightgrey"),    # gridlines
    yaxis = list(gridcolor = "lightgrey")     # gridlines
  )

ggplot and ggplotly

ggplot plotly

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kat