plotly / plotly/plotly.R

Unexpected behavior with geom_text layers in ggplotly

Offen
#539 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

I'm getting unexpected behavior when assigning color to geom_text() in ggplotly

This is slightly related to issue #469 (and I'll use the same example data) where a geom_text layer wasn't appearing at all. That's no longer the case. The text appears fine (yeah!) but the interactivity isn't functioning as I would expect (rats!).

Here's some data:

tempDf <- data.frame("x" = c(22.1, 22.1, 14.4, 32.1, 22.0, 32.1, 84.3, 15.9, 15.9, 11.8, 11.8, 25.1, 15.7, 22.4, 35.1, 36.6, 70.4, 65.2, 42.9, 36.3, 47.8),
"y" = c(21.0, 21.0, 39.8, 47.6, 69.6, 95.0, 94.8, 17.3, 17.3, 27.6, 27.6, 90.3, 85.7, 94.1, 72.3, 71.5, 100.0, 92.4, 76.9, 34.6, 4.9),
"endX" = c(NA, 13.1, 31.9, 22.0, 29.7, 84.3, 94.4, NA, 15.1, NA, 25.0, 14.3, 22.4, 59.6, NA, 70.4, 65.9, 44.2, 38.3, 48.3, 72.1),
"endY" = c(NA, 33.3, 46.0, 69.6, 93.5, 94.8, 51.6, NA, 22.1, NA, 90.3, 85.4, 94.1, 96.8, NA, 100.0, 91.5, 80.8, 37.1, 5.2, 36.9),
"index" = c(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0),
"string"= c(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0) )

And here's my plot:

example <- ggplot(tempDf, aes(x=x, y=y, color=as.factor(index))) + geom_point(size = 4, shape = 0) + 
    geom_text(aes(label= string), size = 2) + 
    geom_segment(data = tempDf, aes(x=x, xend=endX, y=y, yend=endY, group = index, color=as.factor(index)), size = .3, arrow=arrow(length=unit(0.3,"cm")) ) + xlim(0,100) + ylim(0,100) +
    theme(panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.background = element_rect(fill = "grey90"), 
        axis.ticks.x = element_blank(), 
        axis.ticks.y = element_blank(), 
        axis.text.x = element_blank(), 
        axis.text.y = element_blank(),  
        #legend.title=element_blank(),
        #legend.margin=unit(-.4, "cm"),
        plot.margin=unit(c(0, 0, 1.3, -1), units="cm"),  #top, right, bottom, left
        legend.text = element_text(size = 14, face = "bold") ) + 
    xlab("") + ylab("")
ggplotly(example)

There are three colored sequences (at least that's how I'll refer to them here, one red, one green and one blue). I can toggle off/on ant of the individual color sequences. When I do that all of the elements toggle off (the segments, the text (number) and the shape; arrows aren't yet supported in plotly). Here, I've toggled the red off. This is as I would expect (and what I'm after).
screen shot 2016-04-06 at 5 25 53 pm
However, if I change the color of the geom_text like this (the only difference is that I'm adding: 'color = "black"' to the geom_text layer)

example <- ggplot(tempDf, aes(x=x, y=y, color=as.factor(index))) + geom_point(size = 4, shape = 0) + 
        geom_text(aes(label= string), size = 2, color = "black") + 
        geom_segment(data = tempDf, aes(x=x, xend=endX, y=y, yend=endY, group = index, color=as.factor(index)), size = .3, arrow=arrow(length=unit(0.3,"cm")) ) + xlim(0,100) + ylim(0,100) +
        theme(panel.grid.minor = element_blank(), 
            panel.grid.major = element_blank(), 
            panel.background = element_rect(fill = "grey90"), 
            axis.ticks.x = element_blank(), 
            axis.ticks.y = element_blank(), 
            axis.text.x = element_blank(), 
            axis.text.y = element_blank(),  
            #legend.title=element_blank(),
            #legend.margin=unit(-.4, "cm"),
            plot.margin=unit(c(0, 0, 1.3, -1), units="cm"),  #top, right, bottom, left
            legend.text = element_text(size = 14, face = "bold") ) + 
        xlab("") + ylab("")
ggplotly(example)

Now when I toggle off any of the colored sequences, the black numbers are left behind. Here I've left the red layer 'on') but notice all of the black letters from the other sequences are left still showing.
screen shot 2016-04-06 at 5 25 34 pm

That's not right, is it? The behavior I'd expect (and what I'm hoping for) is that the black numbers also toggle off with the rest of the elements.

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 ggplotly-Beispiel mit geom_point, geom_text und geom_segment zu reproduzieren, und vergleiche eine zugeordnete Farbe mit einer festgelegten schwarzen Textfarbe. Verfolge, wie ggplotly Layer für das Umschalten von Legendeneinträgen gruppiert. Als erledigt gilt die Aufgabe, wenn schwarze geom_text-Beschriftungen zusammen mit der entsprechenden farbigen Sequenz ausgeblendet werden, während das bestehende Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
r
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 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.