Home > Net >  Suppress particular text and replace it in popup
Suppress particular text and replace it in popup

Time:10-13

Based on the code and data below, how can I suppress forcat::age_group and change it to Age Group in the interactive popup?

Data (pop_hisp_df):

structure(list(age_group = c("<  5 years", "5 - 14", "15  -  24", 
"25  -  34", "35  -  44", "45  -  54", "55  -  64", "65  -  74", 
"75  -  84", "85  ", "<  5 years", "5 - 14", "15  -  24", "25  -  34", 
"35  -  44", "45  -  54", "55  -  64", "65  -  74", "75  -  84", 
"85  "), Type = c("Males", "Males", "Males", "Males", "Males", 
"Males", "Males", "Males", "Males", "Males", "Females", "Females", 
"Females", "Females", "Females", "Females", "Females", "Females", 
"Females", "Females"), Value = c(-6, -13, -13, -15, -17, -15, 
-11, -6, -3, -1, 6, 12, 12, 14, 16, 15, 12, 7, 4, 2)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

Code:

# Plot

library(tidyverse)
library(plotly)

        gg_pop_gen = ggplot(pop_gen_df, aes(x = forcats::as_factor(age_group), y = Value, fill = Type))   
          geom_bar(data = subset(pop_gen_df, Type == "Females"), stat = "identity")   
          geom_bar(data = subset(pop_gen_df, Type == "Males"), stat = "identity")   
          #geom_text(aes(label = paste0(abs(Value), "%")))  
          #scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # CHANGE
          scale_y_continuous(
            limits=c(-20,20),
            breaks=c(-15,-10,0,10,15),
            labels=paste0(c(15,10,0,10,15),"%")
          )   
          scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males"))  
          ggtitle("FIGURE 4: TOTAL POPULATION BY AGE AND GENDER")  
          labs(x = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender")  
          theme_minimal()  
          theme(legend.position="bottom")  
          coord_flip()
        
        # Interactive
        ggplotly(gg_pop_gen) %>% 
          layout(
            legend = list(
              orientation = 'h', x = 0.3, y = -0.3, 
              title = list(text = '')
            )
          ) %>%
          reverse_legend_labels() %>% 
          layout(margin = list(l = 50, r = 50, b = 100, t = 50),
                 annotations = list(x = 0.3, y = -0.3, text = "<b>Figure 3 & 4</b>: Data for Figure 3 and 4 was retrieved from: \n U.S. Census Bureau, American Community 5-Year Estimates",
                                    xref='paper', yref='paper', showarrow = F, 
                                    xanchor='right', yanchor='auto', xshift=0, yshift=0,
                                    font = list(size = 10)))

CodePudding user response:

You could create a text in your aes with paste and the data. After that, you can specify tooltip = 'text' in your ggplotly call to show the hover text like this:

library(ggplot2)
library(plotly)
library(dplyr)

gg_pop_gen = ggplot(pop_hisp_df, aes(x = forcats::as_factor(age_group), y = Value, fill = Type,
                                     text = paste('Age group:', age_group, '\nValue:', abs(Value), '\nType:', Type)))   
  geom_bar(data = subset(pop_hisp_df, Type == "Females"), stat = "identity")   
  geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity")   
  #geom_text(aes(label = paste0(abs(Value), "%")))  
  #scale_y_continuous(labels = function(z) paste0(abs(z), "%"))            # CHANGE
  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  )   
  scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males"))  
  ggtitle("FIGURE 4: TOTAL POPULATION BY AGE AND GENDER")  
  labs(x = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender")  
  theme_minimal()  
  theme(legend.position="bottom")  
  coord_flip()

# Interactive
ggplotly(gg_pop_gen, tooltip = 'text') %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.3, 
      title = list(text = '')
    )
  ) %>%
  #reverse_legend_labels() %>% 
  layout(margin = list(l = 50, r = 50, b = 100, t = 50),
         annotations = list(x = 0.3, y = -0.3, text = "<b>Figure 3 & 4</b>: Data for Figure 3 and 4 was retrieved from: \n U.S. Census Bureau, American Community 5-Year Estimates",
                            xref='paper', yref='paper', showarrow = F, 
                            xanchor='right', yanchor='auto', xshift=0, yshift=0,
                            font = list(size = 10)))

enter image description here

Created on 2022-10-12 with reprex v2.0.2

  • Related