Skip to content

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

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
alanocallaghan opened this issue Nov 22, 2016 · 6 comments
Closed
Labels

Comments

@alanocallaghan
Copy link
Contributor

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)
@alanocallaghan alanocallaghan 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) Nov 22, 2016
@alanocallaghan
Copy link
Contributor Author

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.

@alanocallaghan
Copy link
Contributor Author

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

@alanocallaghan
Copy link
Contributor Author

alanocallaghan commented Nov 23, 2016

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")

@alanocallaghan
Copy link
Contributor Author

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

@yinghawl
Copy link

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

@alanocallaghan
Copy link
Contributor Author

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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants