如何使用函数ggplot()和plot_ly增加R中轴标签和轴标题之间的间距? [英] 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()
来增加轴标题和标签之间的间距?以及如何确保图例在图1中可见并且不中断?
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?
之前的StackOverflow问题为结构代码提供了不同的方式,但它们似乎不适用于我的代码.
Previous StackOverflow questions give different ways to structure code but they just dont seem to work for my code.
图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 <- 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)
p<-plot_ly(y = GrassHeight+1, color = CWDPosition,type = "box",colors = "Set1")%>%
layout(xaxis = x, yaxis = y, font = f)
推荐答案
对于plot_ly
,不确定是否是最佳答案,但这可能是由于左侧边距太小:
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))
这篇关于如何使用函数ggplot()和plot_ly增加R中轴标签和轴标题之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!