如何使用函数 ggplot() 和 plot_ly 在 R 中增加轴标签和轴标题之间的 space?

How do you increase the space between the axis labels and axis titles in R using functions ggplot() and plot_ly?

似乎每当我使用包 plotly 在 R 中创建图形时,轴标题总是覆盖轴标签。我现在有 2 个图表发生这种情况。我应该使用什么代码来使用 plot_ly()ggplot() 增加轴标题和标签之间的 space?我如何确保图例在 Plot 1 中可见而不被切断?

以前的 Whosebug 问题提供了不同的代码结构方式,但它们似乎不适用于我的代码。

地块 1:

plot <- ggplot(dat, aes(x=HeightUnderCWD, y=GrassHeight))+ geom_point()+ labs(x = "Height under CWD (cm)", y = "Grass Height (cm)")+ theme(text=element_text(size=20, family="Arial"))+ stat_smooth(method = "lm",formula = y ~ x,se = FALSE, color='Darkgreen')+ stat_smooth(aes(x = HeightUnderCWD, y = 7, linetype = "Linear Fit"), method = "lm", formula = y ~ x, se = FALSE,size = 1,color='lightgreen') ggplotly(plot)

情节 2:

p<-plot_ly(y = GrassHeight+1, color = CWDPosition,type = "box",colors = "Set1")%>% layout(xaxis = x, yaxis = y, font = f)

对于 plot_ly,不确定这是最佳答案,但可能是因为左边距太小:

plot_ly(type="box") %>% 
  layout(margin = list(l=25, r=50, b=50, t=50, pad=0),
         yaxis=list(title="GrassHeight")) %>%
  add_trace(y = ~rnorm(50, 0)) %>%
  add_trace(y = ~rnorm(50, 1))

plot_ly(type="box") %>% 
  layout(margin = list(l=100, r=50, b=50, t=50, pad=0),
         yaxis=list(title="GrassHeight")) %>%
  add_trace(y = ~rnorm(50, 0)) %>%
  add_trace(y = ~rnorm(50, 1))