r - ggplot2 + 绘图 : Axis title disappear

标签 r ggplot2 plotly ggplotly r-plotly

在对 ggplot 图表使用 ggplotly() 时遇到问题: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 给出了其他东西。这是一个问题,有解决方法吗?

最佳答案

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

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

关于r - ggplot2 + 绘图 : Axis title disappear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156334/

相关文章:

r - 如何隐形返回列表的一部分

r - 在 ggplot2 中使用特定值进行分面

r - 使用 ggplot 记录计数的时间序列

r - 如何调整绘图标记颜色以匹配 R 中的有序类别?

pandas - 在 Plotly 图表上编辑构面标题

r - 省略某些值时如何创建桑基图

r - 如何使用 dplyr 参数化变量名

R 包 cem 和 MatchIt : Different imbalance measure

r - 如何设置 data.frame 的行名?

R如何在ggplot2中为金字塔状图添加分面标签