Skip to content

Support for ggplot stacked bar plots? #198

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
mrvollger opened this issue Apr 8, 2015 · 3 comments
Closed

Support for ggplot stacked bar plots? #198

mrvollger opened this issue Apr 8, 2015 · 3 comments

Comments

@mrvollger
Copy link

I am not sure this is an issue as much as a question as to whether this kind of graph is supposed to be supported. I have a stacked bar plot in ggplot and it does not appear the same way online in ploty. Here is the code and the link to the graph on ploty.

library(ggplot2)
library(reshape2)
library(gtools)
library(scales)
library(dplyr)
library(devtools)
library(plotly)
setwd("/home/mitchell/IW/hmm")

load("mutations.data")
head(mutations)

output:

       chr    pos change ref alt dp ao strain Percentage Mutation                   id
11    chrX 289758    T>G   T   G 49 22  cis--          1  A>C:T>G    chrX289758A>C:T>G
28 chrVIII 204811    A>C   A   C 44 18  cis--          1  A>C:T>G chrVIII204811A>C:T>G
34  chrXII  65829    A>C   A   C 44 19  cis--          1  A>C:T>G   chrXII65829A>C:T>G
40   chrIX 187852    T>G   T   G 37 16  cis--          1  A>C:T>G   chrIX187852A>C:T>G
41    chrV 519341    T>G   T   G 41 17  cis--          1  A>C:T>G    chrV519341A>C:T>G
42   chrVI 113016    T>G   T   G 33 14  cis--          1  A>C:T>G   chrVI113016A>C:T>G
p <- ggplot(mutations, aes(x = strain, fill=Mutation ))
p <- p + geom_bar(stat="identity", position="fill", aes(y=Percentage ))
p <- p + scale_y_continuous(labels = percent_format())
p

Local out put graph:

image

py <- plotly(username="mrvollger", key="aboy5gyo0b")
r<-py$ggplotly(p)
r$response$url

output url:
https://plot.ly/~mrvollger/59

@cpsievert
Copy link
Collaborator

Thanks for reporting! Here is a reproducible example of the same problem:

p <- ggplot(mtcars, aes(factor(vs), fill=factor(cyl))) + 
  geom_bar(position = "fill")

rplot

https://plot.ly/~cpsievert/432/count-vs-factorvs/

I can take a look at fixing this @tdhock @chriddyp @mkcor.

@tdhock
Copy link
Contributor

tdhock commented Apr 9, 2015

there definitely are some tests for bar graphs like this e.g.

http://ropensci.github.io/plotly-test-table/tables/3105143f285dc2c5cb7194a3bab702539b28746f/bar-stack.html

actually the order of bars and legend entries is not the same between plotly and ggplot2 -- is that a problem? if so we should change this line
https://github.com/ropensci/plotly/blob/master/tests/testthat/test-ggplot-bar.R#L32 to

expect_identical(trace.names, c("Bio", "Math"))

@cpsievert
Copy link
Collaborator

The problem here has to do with position_fill() -- which essentially normalizes the stacked bar chart. I'm working on a fix for that and I will add some proper tests.

cpsievert added a commit that referenced this issue Apr 16, 2015
Let ggplot handle histogran binning. Fix #198
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