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

时间:2016-09-27 13:36:29

标签: r plot plotly r-plotly

看来每当我使用包plotly在R中创建图形时,轴标题始终覆盖轴标签。我现在有两张图表。我应该使用哪些代码来使用plot_ly()ggplot()来增加轴标题和标签之间的空间?如何确保图例在Plot 1中可见而不是切断?

以前的StackOverflow问题给出了构造代码的不同方法,但它们似乎对我的代码不起作用。

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

enter image description here

情节2:

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

enter image description here

1 个答案:

答案 0 :(得分:2)

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

enter image description here

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

enter image description here