【问题标题】:ggplot2 + plotly : Axis title disappearggplot2 + plotly:轴标题消失
【发布时间】:2016-05-11 10:15:11
【问题描述】:

ggplotly() 用于ggplot 图形时遇到问题:y 轴消失。这是一个使用iris 数据集的可重现示例(这个示例非常垃圾,但无论如何)

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)

如您所见,Y 轴标题消失了。

好吧,如果 ylim 被删除它可以工作,但我想指定 y 限制。

我尝试执行以下操作:

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)

但现在是传说标题不合适了。

我的配置:R 3.2.0,plotly 2.0.16,ggp​​lot2 2.0.0

在这两个例子中,ggplot 给出的图表是我想要的,但 ggplotly 给出了别的东西。这是一个问题,有解决方法吗?

【问题讨论】:

    标签: r ggplot2 plotly ggplotly r-plotly


    【解决方案1】:

    我遇到了同样的问题,感谢您的 cmets,我可以解决它。但是我遇到了轴标签附加到情节的问题。所以我通过添加边距来解决它。

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

    `

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。通过 ggplotly 推送的 ggplot 对象显示出我的 y 轴标签 [在闪亮的应用程序中] 的剪裁。

      为了修复它,我按照 MLavoie 的建议进行了操作,但它同时具有我的 ggplot 标签和我的 ggplotly 标签。为了解决这个问题,我只需将我的 ggplot 标签设置为空格,一切正常(如果你将它们设置为空,绘图标签将与轴刻度值重叠)。

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

      【讨论】:

        【解决方案3】:

        我不确定为什么会这样,但这里有一个解决方法。它会给你你想要的。

        p <- ggplotly(g)
        x <- list(
            title = "X Title"
        )
        y <- list(
            title = "Y Title"
        )
        p %>% layout(xaxis = x, yaxis = y)
        

        【讨论】:

        • 这对 facets 来说更糟
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 2021-10-18
        • 1970-01-01
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多