plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

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

Open alanocallaghan opened this issue 9 years ago • 6 comments

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 avatar Nov 22 '16 21:11 alanocallaghan

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 avatar Nov 22 '16 22:11 alanocallaghan

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 avatar Nov 23 '16 20:11 alanocallaghan

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 avatar Nov 23 '16 22:11 alanocallaghan

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

alanocallaghan avatar Nov 23 '16 22:11 alanocallaghan

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

yinghawl avatar May 10 '24 21:05 yinghawl

Doesn't look that way to me

alanocallaghan avatar May 10 '24 21:05 alanocallaghan