看来每当我使用包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)
情节2:
p<-plot_ly(y = GrassHeight+1, color = CWDPosition,type = "box",colors = "Set1")%>%
layout(xaxis = x, yaxis = y, font = f)
答案 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))
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))