Skip to content

ggplot to ggplotly doesn't work with customized 'fill' values in R #421

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
speedreader opened this issue Jan 23, 2016 · 5 comments
Closed
Labels

Comments

@speedreader
Copy link

Trying to use plotly and ggplot to make a geom_bar with a custom colored bar.

set.seed(8)
y <- rpois(1000, 2.9)
table(y)

holdThis <- as.data.frame(table(y))
holdThis$cumTotal <- cumsum(holdThis$Freq)
holdThis$percentage <- (as.numeric(holdThis$Freq)/10000) * 100
holdThis$y <- as.numeric(as.character(holdThis$y))

# In practice this will be generated dynamically, but for here I'll just fill them manually.
fillCol <- c('blue', 'blue','blue','blue','blue','blue','red','blue','blue','blue')

p <- ggplot(holdThis, aes(x=y, y=Freq)) + geom_bar(stat='identity', fill= fillCol, color="black", size = .25, alpha = .7) + scale_x_discrete(breaks = y)

ggplotly(p)

ggplot produces this (works):

screen shot 2016-01-23 at 3 14 04 pm

ggplotly(p) produces this message:
Warning message:
In if (!grepl(pat, x)) return(x) :
the condition has length > 1 and only the first element will be used

And this:
screen shot 2016-01-23 at 3 14 26 pm

@speedreader speedreader changed the title ggplot to ggplotly doesn't work with customized 'fill' values ggplot to ggplotly doesn't work with customized 'fill' values in R Jan 23, 2016
@cpsievert
Copy link
Collaborator

Bug confirmed, thanks.

@cpsievert cpsievert added the bug label Jan 26, 2016
@yonicd
Copy link

yonicd commented Feb 24, 2016

I have same issue with geom_boxplots when the fill variable has a different number of levels than the x axis variable

set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), 
                            cond2 = factor(rep(c("C","D","E","F"), each=100)),
                             rating = c(rnorm(200),rnorm(200, mean=.8))
)

dat2 <- data.frame(cond = factor(rep(c("A","B"), each=200)),
                  cond2 = factor(rep(c("C","D"), each=200))
                  rating = c(rnorm(200),rnorm(200, mean=.8))
                  )

p1=ggplot(dat, aes(x=cond, y=rating, fill=cond2)) + geom_boxplot()
p2=ggplot(dat2, aes(x=cond, y=rating, fill=cond2)) + geom_boxplot()

ggplotly(p1)
Warning messages:
1: In if (!grepl(pat, x)) return(x) :
  the condition has length > 1 and only the first element will be used
2: In if (!grepl(pat, x)) return(x) :
  the condition has length > 1 and only the first element will be used

ggplotly(p2)

image

image

@yonicd
Copy link

yonicd commented Feb 24, 2016

the bug looks to be in line 630 of the gg2list function
you need to wrap the logical with any to make it work for multiple comparisons

  rm_alpha <- function(x) {
    if (length(x) == 0) 
      return(x)
    pat <- "^rgba\\("
    if (any(!grepl(pat, x)))  # <-------
      return(x)
    sub(",\\s*[0]?[.]?[0-9]+\\)$", ")", sub(pat, "rgb(", 
                                            x))
  }

@cpsievert
Copy link
Collaborator

This should be fixed in f1900cc. Let us know if you still have problems.

@yonicd
Copy link

yonicd commented Mar 8, 2016

much better but there is still a bug.

running the code below brings up the correct graph but when toggling off any of the fill factors (eg yr==2006) interactively it causes the last filled (right most) boxplot to increase in width.

library(plotly)
data=read.csv("https://raw.githubusercontent.com/yonicd/CIMDO/master/temp/example.csv",header = T,stringsAsFactors = F)
ggplot(data,aes(x=cut0,y=y3,fill=yr))+geom_boxplot()
ggplotly()

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

No branches or pull requests

3 participants