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

。_饼干妹妹 提交于 2019-12-11 03:29:44

问题


It appears that whenever I create graphs in R using the package plotly, the axis titles always cover the axis labels. I have 2 graphs now where this occurs. What code should I use to increase the space between the axis titles and labels using plot_ly() and ggplot()? And how do I ensure that the legend is visible in Plot 1 and not cut off?

Previous StackOverflow questions give different ways to structure code but they just dont seem to work for my code.

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

Plot 2:

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


回答1:


For plot_ly, not sure it's the best answer, but it could be due to a too small left margin :

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



来源:https://stackoverflow.com/questions/39726146/how-do-you-increase-the-space-between-the-axis-labels-and-axis-titles-in-r-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!