9

I've got an issue when using ggplotly() to a ggplot graph: the y axis disappears. Here's a reproducible example using iris dataset (this example is quite dump, but whatever)

data(iris)
g = ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, fill = Species)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  scale_fill_manual(name = "legend", values = c("blue", "red", "green")) +
  ylab("Y title") +
  ylim(c(0,3)) +
  xlab("X title") +
  ggtitle("Main title")
g
ggplotly(g)

As you can see, the Y axis title vanished.

Well, if ylim is deleted it works, but I'd like to specify y limits.

I tried to do the following:

data(iris)
g = ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, fill = Species)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  scale_fill_manual(name = "legend", values = c("blue", "red", "green")) +
  scale_y_continuous(name = "Y title", limits = c(0, 3)) +
  xlab("X title") +
  ggtitle("Main title")
g
ggplotly(g)

But now it's the legend title that doesn't fit.

My config : R 3.2.0, plotly 2.0.16, ggplot2 2.0.0

In both examples the graph given by ggplot is what I want, but ggplotly gives something else. Is it an issue, is there a workaround?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
prise6
  • 136
  • 1
  • 7

3 Answers3

9

I am not sure why it's happening but here is a work around. It will give you what you want.

p <- ggplotly(g)
x <- list(
    title = "X Title"
)
y <- list(
    title = "Y Title"
)
p %>% layout(xaxis = x, yaxis = y)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • this is even worse for facets – marbel Feb 23 '17 at 18:44
  • What about subtitles ? Can we add one ? – Julien Aug 18 '22 at 07:53
  • 1
    @Julien, yes you can try: ggplotly(g) %>% layout(title = list(text = paste0('Main title', '
    ', '', 'just testing', '')))
    – MLavoie Aug 18 '22 at 14:57
  • How can I put the title of the plot, not the names of the axes ? – Julien Aug 19 '22 at 09:10
  • @Julien I don't follow. Do you mean you only want a title, but no x and y axes? – MLavoie Aug 19 '22 at 13:16
  • I want a title and the title for the Y-axis – Julien Aug 19 '22 at 13:29
  • @Julien if you replace xlab("X title") with xlab(NULL) you will have a plot title and a y axis title but no x axis title. And this question was 5 years old. The original script posted by the author of the question actually works now. – MLavoie Aug 19 '22 at 17:37
  • What about the title of the plot ? – Julien Aug 19 '22 at 21:28
  • @Julien. With ggtitle("Main title") (it is already in the original code from the author) you will have a plot title. Maybe you should ask a question with your own example. It would be easier to answer. Instead of going back and forth in the comment section. – MLavoie Aug 20 '22 at 10:11
2

I was having a similar problem. A ggplot object pushed through ggplotly was exhibiting clipping of my y-axis label [in a Shiny app].

To fix it, I did what MLavoie suggested but then it had BOTH my ggplot labels and my ggplotly labels. To fix this I simply set my ggplot labels to spaces and everything worked (if you set them to nothing, the plotly labels will overlap with the axis tick-mark values).

p <- ggplotly(g + ylab(" ") + xlab(" "))
x <- list(
    title = "X Title"
)
y <- list(
    title = "Y Title"
)
p %>% layout(xaxis = x, yaxis = y)
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
2

I had the same problem, thanks to your comments, i could resolve it. However I had the issue that labels of axis were attached to the plot. So i resolved it by adding margin.

p <- ggplotly(g + ylab(" ") + xlab(" "))
x <- list(
title = "X Title")
y <- list(
title = "Y Title")
p %>% layout(xaxis = x, yaxis = y, margin = list(l = 75, b =50))

`

pari
  • 788
  • 8
  • 12