plotly / plotly/plotly.R

Give custom name to legend items in ggplotly object

Ouverte
#1,324 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

ggplotly
Langage dominant
R
Étoiles
2.7k
Forks
641
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Hi,

I want to make the legend items to be displayed by the choice of inputs and having an "and" between two variables.
I added my sample shiny app, which demonstrates the need for this.
I can change the legend items in ggplot itself. But ggplotly object doesn't reflect this change.

If anybody help me on this, I would be appreciated.

Thank you.
Mustafa

library(data.table)
library(shiny)
library(plotly)
library(shinycssloaders)

global.R file:

df <- data.frame(gender=rep(c("Male", "Female", "Both gender"), each=12, times=3),
age.group=rep(c("15 years and over", "15 to 19 years", "15 to 64 years"), each=36, times=1),
rate=rnorm(108, mean=5, sd=1),
date=rep(seq(as.Date('1976-01-01'),as.Date('1977-01-01'),by = 31), times=9))

df <- as.data.table(df)

mycolor12 = c("#bdc9e1","#55aadb","#016c59", "#FF8C00", "#EE3B3B", "#EEC591", "#00FFFF",
"#8B7500", "#AB82FF", "#FFFFE0", "#FFE1FF", "#8B7E66")

ui.R file:

navbarPage("Sample app",
navbarMenu('First dataset',
tabPanel("Second navigation bar",
fluidRow(
column(3,
wellPanel(
checkboxGroupInput("gender", label = "Gender",
choices = c(
"Both gender",
"Male",
"Female"),
selected = "Both gender"
),
checkboxGroupInput("agegroup", label = "Age group",
choices = c(
"15 years and over",
"15 to 19 years",
"15 to 64 years"),
selected = "15 years and over"
)
),
width=4,
height=110
),
hr(),
column(9,
plotlyOutput('plot_interactive', width=1000, height =450) %>%
withSpinner(color="#0dc5c1"))
)
)
)
)

server.R file:

function(input, output,session){

df_interactive <- reactive({

df[age.group %in% input$agegroup &
     gender %in% input$gender] 

})

target_plot <- reactive({
p <- ggplot(df_interactive(), aes(x=date, y=rate, colour = interaction(age.group, gender)))+
geom_line(aes(color=interaction(age.group, gender)), size=0.5)+
labs(y = "", x = "") +
scale_color_manual(values = mycolor12, name = '',
labels = paste(input$agegroup, input$gender, sep=" and "),
guide = guide_legend(
title="",
direction = "vertical",
label.position = "right",
label.hjust = 0.5,
label.vjust = 1
))+
scale_y_continuous(labels = scales::percent) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y")

p <- ggplotly(p) %>% config(displayModeBar = F) %>%
  layout(legend = list(orientation = "v", x = 1, y=0.3, font = list(size = 10, bgcolor = 'rgb(251)')))

})

output$plot_interactive <- renderPlotly({
target_plot()
})
}

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Exécutez l’application Shiny d’exemple à partir de ui.R, server.R et global.R, puis inspectez target_plot, où ggplotly convertit la légende ggplot. Comparez les libellés demandés par scale_color_manual avec la sortie de la légende interactive ; c’est terminé lorsque la légende ggplotly affiche les entrées sélectionnées reliées par "and".

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
r
Domaine
data-visualization
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.