plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.52k stars 621 forks source link

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

Open alanocallaghan opened 7 years ago

alanocallaghan commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

yinghawl commented 1 month ago

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

alanocallaghan commented 1 month ago

Doesn't look that way to me