简体   繁体   中英

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

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)

在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM