Skip to content

xlab & ylab getting cut off using ggplotly() #510

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
akraemer007 opened this issue Mar 15, 2016 · 21 comments
Closed

xlab & ylab getting cut off using ggplotly() #510

akraemer007 opened this issue Mar 15, 2016 · 21 comments
Labels

Comments

@akraemer007
Copy link

Hello,

I'm new to Plotly and am very excited to use it's flash visualizations and the tooltip. I'm running into an issue with my x & y labels getting cut off when I run the viz with ggplotly -- whereas it works fine when I just run the ggplot().

Below is the code and viz. I appreciate the help in advance!

Andrew

image

Here's my code:

testPlot <-
ggplot(TopCarePaths.Req,aes(x = calc_appears_offset, y = total.completed_sum/total.completed_and_expired_sum ) )+
  geom_point( aes( shape = is_form,  color = PrePostFlag, text = paste("Title:", title_full) ), alpha = 2/5, size = 5) + #dimensions of CarePath
  # geom_point( aes( text = paste("Title:", title_full) ) ) + # changes the tooltip
  # geom_rug(sides="b", position='jitter') + #adds rug, which counts number of points. Added so only applied to X-axis & spread out with jitter
  guides( alpha = FALSE ) + #hides unneccssary legend for hard coded transparancy
  facet_grid( organization_parent_name~. ) + #Facets graph horizontally
  scale_x_continuous( limits = c( -50, 50 ), breaks = seq( -50, 50, 5 ) ) + #controls X axis tick marks
  scale_y_continuous(labels = scales::percent) + #quickly changes y axis to %
  geom_vline( xintercept = 0 ) + #adds solid line to chart
  geom_text( aes( x = 0, label = "Surgery\n", y = .2), family = 'sans', angle=0,  color = 'gray34', size = 3, alpha = 1/5 ) + #puts text next to line for surg date
  theme_bw() + #changes to a simpler, easy to read theme
  theme( strip.background = element_blank() ) + #removes ugly gray from name area of facet
  ggtitle( 'Content Completion by Across Full CarePath' ) +
  ylab('Completion %') +
  xlab( 'Days Appears Offset from Surgery' )
ggplotly()
@bhive01
Copy link

bhive01 commented Mar 15, 2016

I'm seeing this too on my plots lately, but it appears to be related to the window size/ratio.
Here's a MRE:

require(ggplot2)
require(plotly)

randomName <- function(n) {
    random.string <- rep(NA, n)
    randomizeString <- function(x) {
        a <-sample(letters, 1, replace = TRUE)
        return(a)
    }
    return(paste(c(sapply(random.string, randomizeString, simplify = TRUE)), collapse = ""))
}

xvar = replicate(10, randomName(15))

df1 <- data.frame(x = xvar, y = xvar, z = runif(10) )

g = ggplot(df1, aes(x,y,fill=z)) +
        geom_tile() 

ggplotly(g)

Squished, the axis name overlaps with the axis labels:
screen shot 2016-03-15 at 2 20 16 pm
nearly 1:1 everything looks ok
screen shot 2016-03-15 at 2 20 24 pm
longer and the axis name goes off the end
screen shot 2016-03-15 at 2 20 31 pm

@andy-ewing
Copy link

screen shot 2016-03-22 at 1 32 10 pm

I'm having similar issues since the latest updates to R, RStudio, and plotly packages. It may have something to do with the scales package and/or faceting?

@jasdumas
Copy link

I'm experiencing the same issue with making an unstacked bar chart and the y-axis label is getting cut-off similarly to @Kidman007. I have tried some theme() adjustments within ggplot but with no success.

Has there been any solutions yet?

@timelyportfolio
Copy link
Collaborator

Looking at it now, but I am still not as familiar with the code as I would like to be. It appears these lines are most likely to hold the answer. I'll dig deeper.

@timelyportfolio
Copy link
Collaborator

timelyportfolio commented Apr 21, 2016

When I reproduce the plot with the example from @bhive01, I get an annotation for "x" with yref equal to paper which means the annotation will be positioned with reference to the chart paper box on a scale (0,1). The negative value provided (-0.07990868) will scale linearly and inversely based on the size of the plot, which explains why when the plot is small, the x is too high, and when the plot is large, x is too low/off the screen.

I'll ping @cpsievert to see if there is an ideal solution, or if we should resort to a hack.

@cpsievert
Copy link
Collaborator

Yea, I'm not sure the best way to approach this problem. What I really need is a way to place annotations on a (0-1) scale of the entire plot, or on the absolute pixel scale. Any idea if that would be easy to add to plotly.js?

@timelyportfolio
Copy link
Collaborator

@cpsievert, do you remember why in lines this comment was made?

        # draw axis titles as annotations
        # (plotly.js axis titles aren't smart enough to dodge ticks & text)

I am guessing facets, but could be way off here.

Let's hack on the example given above by defining $x$layout$xaxis$title.

require(ggplot2)
require(plotly)

randomName <- function(n) {
  random.string <- rep(NA, n)
  randomizeString <- function(x) {
    a <-sample(letters, 1, replace = TRUE)
    return(a)
  }
  return(paste(c(sapply(random.string, randomizeString, simplify = TRUE)), collapse = ""))
}

xvar = replicate(10, randomName(15))

df1 <- data.frame(x = xvar, y = xvar, z = runif(10) )

g = ggplot(df1, aes(x,y,fill=z)) +
  geom_tile() 

gpw <- as.widget(ggplotly(g))

gpw$x$layout$xaxis$title = "plotly's x"

gpw

image

@jasdumas
Copy link

jasdumas commented Apr 22, 2016

I've tried hacking with gpw$sizingPolicy$padding = 10 but it's still cut-off. For reference my plotly viz is on shiny where the yaxis is being cut-off by the navbarPage.

However I just downloaded the plot to .png from the button and the yaxis is completely normal and not cut-off 😖
EDIT: Some of the downloaded png files are normal the rest show the cut-off y-axis

@cpsievert
Copy link
Collaborator

@timelyportfolio that's right, I'm drawing titles as annotations because x/y titles are always relative the plot in ggplot2, not each axis.

(in that comment, I think I meant to say "annotations aren't smart enough to dodge ticks & text"

@joshmyzie2
Copy link

Probably obvious, but an easy/crude workaround is to append "\n " to your x label and prepend "\n " to your y label. That should push your labels into the visible region.

@timelyportfolio
Copy link
Collaborator

Yes, but will worsen the situation when the plot is small. @cpsievert, do you have an example where the real plotly axis labels don't work?

@cpsievert
Copy link
Collaborator

qplot(data = mtcars, x = mpg, y = wt, facets = ~vs)

@drcrook1
Copy link

drcrook1 commented May 2, 2016

I'm having the same issues: http://stackoverflow.com/questions/36988379/adjust-axis-positions-ggplot2-facets. Also note that my titles are affected.

@timelyportfolio
Copy link
Collaborator

timelyportfolio commented May 4, 2016

@cpsievert, if no facets, should we change the behavior to use plotly defaults. It seems we unintentionally prioritizing facet behavior over non-facet behavior.

@cpsievert
Copy link
Collaborator

@timelyportfolio yea, that's a good point, a pull request would be most welcome :)

@timelyportfolio
Copy link
Collaborator

@cpsievert I took a first stab at a pull request to make this change. I'm not sure that I got the axis title formatting correct. I tried to still use your margin calculations for both facetted and non-facetted plots. I know you will be much more familiar with the edge cases. I would love your help thinking through the best way to test.

@cpsievert
Copy link
Collaborator

This should now be fixed for non-faceted plots, but we'll need a more general solution for facets.

@nathaneastwood
Copy link

nathaneastwood commented Jul 22, 2016

We seem to be seeing some of the plot being cut off (in our case the top of a bar chart) - possibly as a result of this - when used in Shiny. I can try to make a reproducible example if needed, but I don't have the source code. Interestingly the plot is not cut off when just used in the RStudio Viewer.

@sulaxd
Copy link

sulaxd commented Sep 21, 2016

try setting layout(margin = list(b = 90))

@cpsievert
Copy link
Collaborator

Fixed via 9afbb5d

@ldecicco-USGS
Copy link

I was having these margin issues, so I tried installing via github to get this fix.

Now, I get an error:

library(ggplot2)
library(plotly)
df <- data.frame(x=1:10,y=1:10)
x2 <- ggplot(data = df) +
geom_point(aes(x=x,y=y))
ggplotly(x2)
Error in gg2list(p, width = width, height = height, tooltip = tooltip,  : 
  attempt to apply non-function

So, no problem, I'll just in-install this dev version of plotly and re-install via CRAN....but after tha, I was still getting that error.

I had a dev version of ggplot2 installed as well apparently (I was testing my code for the new updates that are coming out soon in ggplot2). Once I installed ggplot2 from CRAN, then that error finally went away.

I don't know if it's related to:
#233
but might be.

So, I still have the margin issues...but admittedly didn't install the fix...but there might be some other issue with the fix....just FYI

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