Skip to content

Ordering of traces is wrong #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jpevitt opened this issue May 29, 2015 · 12 comments
Closed

Ordering of traces is wrong #225

jpevitt opened this issue May 29, 2015 · 12 comments

Comments

@jpevitt
Copy link

jpevitt commented May 29, 2015

Hey guys, I'm pretty new to the plotly API so if I'm missing something please let me know. I have a 100% stacked area chart in ggplot2 that I'm trying to convert into a plotly plot. I followed the - pretty simple - steps on your API site which took me to the URL for it, except on the site my colors and/or lines entirely disappear leaving me with the entire plot blacked out in some random color.

@13bzhang
Copy link
Contributor

Sorry to hear that you are experiencing problems. Do you have code and images that you can share with us?

@13bzhang
Copy link
Contributor

@jpevitt Thanks for your reply. But I cannot find the attached folder. All I see if a a bunch of numbers. Can you post the code directly to this issue or upload your folder as a repo or a gist on GitHub?

@jpevitt
Copy link
Author

jpevitt commented May 30, 2015

Sorry, answered through email, didn't realize it wouldn't display properly.
Here is my code:
df <- read.delim(file.choose(), header=T)
dfm <- melt(df, id.vars="Year")
full <-
ggplot(data=dfm, aes(x=Year, y=value)) +
fte_theme(SCALE, MARGIN) +
geom_area(aes(fill=variable)) +
labs(title="Generic Title", x=NULL, y=NULL) +
theme(legend.position="right") +
theme(legend.title=element_blank()) +
scale_y_continuous(labels=percent)
library(plotly)
py <- plotly()
py$ggplotly(full)

It results in this ggplot plot when I render it in R
ggplot screenshot

But on plotly it results in this:
plotly screenshot

@13bzhang
Copy link
Contributor

Thanks for sharing. I will look into this.

@cpsievert
Copy link
Collaborator

I'm not exactly sure what's going wrong yet, but here is a reproducible example to help debug:

library(dplyr)
df <- diamonds %>% group_by(carat, cut) %>% 
  summarise(n = n()) %>% mutate(freq = n/sum(n))
p <- ggplot(data = df, aes(x = carat, y = freq, fill = cut)) + 
  geom_area()

@cpsievert
Copy link
Collaborator

Ah, this is related to the order in which the traces are drawn. If you hover over the example I provide above, you'll see hover info for all the traces. @13bzhang does that make sense? Feel free to tackle this one if you'd like.

@13bzhang
Copy link
Contributor

Yeah I can take this one.

@13bzhang
Copy link
Contributor

13bzhang commented Jun 4, 2015

I have been working on this one for the past few days. So I noticed that stacked densities also don't work with the current code. For instance:

p <- ggplot(data = movies, aes(x = rating, fill = mpaa,)) + 
  geom_density(position = "stack")
py$ggplotly(p)

ggplot2
225_stacked_density

Plotly
225_bad_density

The problem is that the order of the traces are laid in reverse. I played around with the code in trace_generation.R a bit and added the following:

   if (g$geom == "area" | g$geom == "density"){
     traces <- rev(traces)
   }

Again, this is really unsophisticated but just a first cut.

I was able to produce the following:

ggplot2
225_area

Plotly
225_first_try_area

Plotly
225_first_try_density

Note some problems still left:

  • the legend is reversed in the area example
  • the legend doesn't show up in the density example

Maybe the solution is to make special cases for these types of geoms like in
https://github.com/ropensci/plotly/blob/master/R/trace_generation.R#L251-L263
?

@cpsievert @chriddyp

@cpsievert
Copy link
Collaborator

It looks like this problem isn't necessarily specific to a geom. For example,

qplot(wt, drat, color = gear, data = mtcars)

leads to

That legend should have 3 at the top and 1 at the bottom. I think you can fix this by having trace "3" be the first trace, trace "2" be the second, and trace "1" be the third.

You may also need to add some special handling for the area geom, but I'm not sure.

@cpsievert cpsievert changed the title Area Chart Not Displaying Ordering of traces is wrong Jun 4, 2015
@cpsievert
Copy link
Collaborator

@13bzhang sorry, I didn't read your post carefully. I wouldn't worry about the ordering of the legend, in fact, I think the plotly version is more sensible than ggplot2. Also, the legend hiding problem is reported in #212.

That being said, I'd start a PR with your ordering fix and include a test!

@13bzhang
Copy link
Contributor

13bzhang commented Jun 4, 2015

@cpsievert If it's a more general problem, then the fix should be somewhat easier.

@cpsievert
Copy link
Collaborator

Fixed via a4a9a1d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants