简体   繁体   中英

Use labels instead of variable names for loop ggplot2

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?

Plotly 对象的图像,显示悬停标签

带有内置变量标签的图像 DF

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"]]) 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM