1

I am trying to create a for loop function to make multiple ggplot line graphs, where only the x variable changes. However, the resulting plots show the variable name as title, and in the hover labels in the ggplotly, 'x' is shown instead of the variable name. I would like to use the variable labels that are already build in in the dataset, instead of the variable names. Can anyone help me with changing the function in such a way that the variable labels are used in the ggplotly instead of the variable names (column names of the df) and the variable label instead of just a 'x' in the hover label?

Image of Plotly object, showing hover labels

Image DF with the build in variable labels

Below the code:

library(ggplot2)
library(cbsodataR)
library(plotly)
library(sjlabelled)
library(expss)
library(tidyr)
library(tidyverse)
library(sf)
library(dplyr)
library(stringr)

##Get data from dutch public data for one specific area
bevolking <- cbs_get_data("70072NED", 
                          RegioS = "GM0736")
#Create other values for variable $Perioden
bevolking$Perioden <- 1995:2020
#Only show data as from 2011
bevolking <- bevolking %>%
  filter(bevolking$Perioden>=2011) 
#Select variables from dataset
bevolking <- bevolking %>%
  select(c("Perioden", "Mannen_2", "Vrouwen_3")) 

#Create for loop function
plots <- list()
for(nm in names(bevolking)) {
  plots[[nm]] <- ggplot(bevolking, aes_string(y = nm, x = bevolking$Perioden)) +
    geom_line(color = "#10A593", size = 1) + 
    theme_bw() + 
    theme(panel.border = element_blank(), 
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line = element_line(colour = "black"), 
          axis.title.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.ticks.x=element_blank(),
          axis.line.y=element_blank()) +
    geom_point() +
    labs(title = (paste(nm)))+
    scale_x_continuous("Perioden",
                       labels = as.character(bevolking$Perioden), 
                       breaks = bevolking$Perioden)
}

#Display one plot as a plotly from the above function
ggplotly(plots[["Mannen_2"]]) 
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Which are the variable labels you want to use? Where are they specified? Can you give an example? additionally: what exactly do you want to see in the hover? additionally2, is your only goal to get a plotly plot? if so, did you consider to use native plotly syntax. With this, we have more control over the plot behavior – mnist May 01 '20 at 06:54
  • @mnist, thanks for your fast reply. I added links in the original post to images to show what I mean. I want to use the variable labels as shows on the image that shows the dataframe! – Anne Vlaanderen May 01 '20 at 07:02
  • thanks for the pictures. it is even better to include them directly rather than with links. in the text input field, the sixth item from the left enables this quite easily – mnist May 01 '20 at 07:17
  • An additional thought: what do you want in the hover? only the y-value or the x value (year) as well? – mnist May 01 '20 at 07:25
  • Welcome to SO! Please help us help you, and provide a minimal and reproducible example. [See here how to do this](https://stackoverflow.com/help/mcve). You can find [some tips how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [how to make some good example data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – tjebo May 01 '20 at 08:47
  • @mnist I would like to have the Y-value in the hover, no additional information ! – Anne Vlaanderen May 01 '20 at 09:56

1 Answers1

0

use_labels is not very suited for usage inside loops. But if I correctly understand what do you need:

library(ggplot2)
library(cbsodataR)
library(plotly)
library(sjlabelled)
library(expss)
library(tidyr)
library(tidyverse)
library(dplyr)
library(stringr)

##Get data from dutch public data for one specific area
bevolking <- cbs_get_data("70072NED", 
                          RegioS = "GM0736")
#Create other values for variable $Perioden
bevolking$Perioden <- 1995:2020
#Only show data as from 2011
bevolking <- bevolking %>%
    filter(bevolking$Perioden>=2011) 
#Select variables from dataset
bevolking <- bevolking %>%
    select(c("Perioden", "Mannen_2", "Vrouwen_3")) 

var_lab(bevolking$Perioden) = "Perioden"

#Create for loop function
plots <- list()
for(nm in names(bevolking)) {
    plots[[nm]] <- use_labels(bevolking, ggplot(..data, aes_string(y = var_lab(bevolking[[nm]]), x = Perioden)) +
        geom_line(color = "#10A593", size = 1) + 
        theme_bw() + 
        theme(panel.border = element_blank(), 
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              axis.line = element_line(colour = "black"), 
              axis.title.y=element_blank(),
              axis.ticks.y=element_blank(),
              axis.ticks.x=element_blank(),
              axis.line.y=element_blank()) +
        geom_point() +
        labs(title = var_lab(bevolking[[nm]]))+
        scale_x_continuous("Perioden",
                           labels = as.character(Perioden), 
                           breaks = Perioden)
    )
}

#Display one plot as a plotly from the above function

ggplotly(plots[["Mannen_2"]], tooltip=c("y")) 

UPDATE: 'aes_string' is not perfect when argumnets are not good variable names. Quote from help for aes_string :

# You can't easily mimic these calls with aes_string
aes(`$100`, colour = "smooth")
aes_(~ `$100`, colour = "smooth")
# Ok, you can, but it requires a _lot_ of quotes
aes_string("`$100`", colour = '"smooth"')

So, you need to paste backticks around var_lab(...). Or, better approach is to use aes_: aes_(y = as.name(var_lab(bevolking[[nm]])), x = Perioden)

Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • @GregoryDenim THANK YOU SOO SOO MUCH! I have been stuck on this for days, this works perfectly! – Anne Vlaanderen May 01 '20 at 18:54
  • I am still figuring out some stuff with respect to this post. The answer of Gregory Denim works perfectly, however, when using variables that contain spaces in their variable labels, I got an error: Error in parse(text = x) : :1:10: unexpected symbol Probably because R doesn't allow spaces in variable names and/or variable labels. Is there a work around? So I can nevertheless use the variable names with spaces? – Anne Vlaanderen May 02 '20 at 09:59
  • @GregoryDenim You are great! Thanks for your help, fast reply and clear answers :) – Anne Vlaanderen May 02 '20 at 18:13