Skip to content

ggplotly does not support axes on right or top (ggplot 2.2.0) #808

Closed

Description

@alanocallaghan

ggplot 2.2.0 newly added right/top axis placements. ggplotly does not currently respect the axis placement set in the ggplot objects in the resultant html

install.packages("heatmaply")
install.packages("htmlwidgets")
## Ensure v.2.2.0
install.packages("ggplot2")


library("heatmaply")
library("htmlwidgets")
library("ggplot2")


h <- heatmaply(mtcars, return_ppxpy=TRUE, 
	row_dend_left=TRUE)

## heatmap with axis on right side (link)
h$p <- h$p + scale_y_discrete(position = "right")

ggsave(plot = h$p, file = "left.png")

pl <- ggplotly(h$p)
saveWidget(pl, "left.html", selfcontained=TRUE)

h$p <- h$p + scale_x_discrete(position = "top")
ggsave(plot = h$p, file = "left_top.png")

pl <- ggplotly(h$p)
saveWidget(pl, "left_top.html", selfcontained=TRUE)

Activity

  1. changed the title [-]ggplotly does not support axes on right or top (ggplot 2.2.0[/-] [+]ggplotly does not support axes on right or top (ggplot 2.2.0)[/+] on Nov 22, 2016
  2. alanocallaghan commented on Nov 22, 2016

    @alanocallaghan
    ContributorAuthor

    This should simply involve checking the axis position in the ggplot object and changing the axis position in the ggplot object appropriately:

    https://community.plot.ly/t/move-axis-labels-to-top-right/534

    Scale position is available in ggplot$scales$scales. In the instance posted above it is ggplot$scales$scales[[2]]$position but I don't know if that's generally true.

  3. alanocallaghan commented on Nov 23, 2016

    @alanocallaghan
    ContributorAuthor

    ggplot$scales$scales[[1]] tends to be the colour scale (if present) while the others are only present if modified. The quick and dirty solution would be to apply across them and return TRUE if position equals "right" or "bottom" but this is not robust

  4. alanocallaghan commented on Nov 23, 2016

    @alanocallaghan
    ContributorAuthor

    Pulling in the information itself is trivial during ggplotly, eg:

    ## If scales are top or right, move them
    x_scale <- scales$get_scales("x")
    if (!is.null(x_scale) && !is.null(x_scale$position) && x_scale$position == "top") gglayout$xaxis$side == "top"
    y_scale <- scales$get_scales("y")
    if (!is.null(y_scale) && !is.null(y_scale$position) && y_scale$position == "top") gglayout$xaxis$side == "right"
    

    However, this does not affect the output (the y-axis is not shown if side==="right")

  5. alanocallaghan commented on Nov 23, 2016

    @alanocallaghan
    ContributorAuthor

    Actually, on further testing, this fix may work. I will try some extra testing and submit a PR if needed

  6. yinghawl commented on May 10, 2024

    @yinghawl

    @alanocallaghan I wonder if the fix has been put in place.

  7. alanocallaghan commented on May 10, 2024

    @alanocallaghan
    ContributorAuthor

    Doesn't look that way to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions