plotly / plotly/plotly.R

ggplotly: geom_histogram(aes(fill = ...)) + facet_*() results in tooltip NAs

Offen
#1,383 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

ggplotly
Vorherrschende Sprache
R
Sterne
2.7k
Forks
641
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I noticed a bug when trying to use ggplotly on a geom_histogram with fill in the aes and facet_*. I was able to recreate it on a simple example using mtcars. (I am using ggplot2 v 3.0.0 and plotly v 4.8.0.) I looked through the issues and it seems similar to #693.

This is the plot I am trying to use ggplotly on:

library(ggplot2)
library(dplyr)
library(plotly)
tp_na <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb, fill = cyl), binwidth = 1) + 
  facet_grid(.~vs)
tp_na

static plot

Using ggplotly, the values of the tooltip for the x-axis variable are all NA:

ggplotly(tp_na)

tooltip NA

(There is one exception to the NA in this reprex. The first red bar in the left facet has the correct value. The rest are NA. But, the same is not true for the plot in which I first encountered this problem.)

My workaround is:

tp <- mtcars %>% mutate(cyl = as.factor(cyl), carb= as.factor(carb)) %>%
  ggplot() + 
  geom_bar(aes(x = carb, fill = cyl))  +
  facet_grid(.~vs)
ggplotly(tp)

tooltip is fine

But, I would prefer to use geom_histogram(binwitdth = 1) for because the x-axis values for my desired plot are days (0, 1, 2, ..., 21), and I like the histogram for that better than the bar chart.

I tried a few other plots to see if what could possibly be causing the error:

# remove binwidth arg. still see NAs in tooltip
tp_na2 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb, fill = cyl)) + 
  facet_grid(.~vs)
ggplotly(tp_na2)
# using a more continuous variable. still see NAs.
tp_na3 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = qsec, fill = cyl)) + 
  facet_wrap(~vs)
ggplotly(tp_na3)
# add a text aes() to force x label to show. In tooltip, there are two entries for x, both correct
tp2 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb, fill = cyl, text = paste("Carb:", carb)), binwidth = 1) + 
  facet_grid(.~vs)
ggplotly(tp2)
# no facets, it works as expected
tp3 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb, fill = cyl), binwidth = 1) 
ggplotly(tp3)
# no fill, it works as expected
tp4 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb), binwidth = 1) + 
  facet_grid(.~vs)
ggplotly(tp4)

screen shot 2018-10-31 at 1 26 30 pm

# adding just carb as the text aes results in multiple NAs in the tooltip
tp5 <- mtcars %>% mutate(cyl = as.factor(cyl)) %>%
  ggplot() + 
  geom_histogram(aes(x = carb, fill = cly, text = carb), binwidth = 1) + 
  facet_grid(.~vs)
ggplotly(tp5)

As you can see in the examples, facet_grid vs. facet_wrap doesn't seem to make a difference. There must be something about the fill + facet combo that goes wonky. I find it especially interesting that adding the text inside aes seems to work, but then I have double labels. I hope this is helpful for others. 🤷‍♀️

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte mtcars-reprex mit ggplot2, plotly, geom_histogram, fill und facet_grid oder facet_wrap auszuführen, und vergleiche es dann mit dem aufgeführten Workaround sowie mit Fällen ohne Facets oder fill. Als erledigt gilt die Aufgabe, wenn die Tooltips des Histogramms die x-Achsenwerte anstelle von NAs anzeigen und dabei das Faceting- und fill-Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.