R:ggplot和plotly axis margin不会改变 [英] R: ggplot and plotly axis margin won't change

查看:251
本文介绍了R:ggplot和plotly axis margin不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ggplotly 围绕 ggplot 停止y轴文本与ticks重叠时遇到问题。我怎样才能解决这个问题?我已经尝试了下面的代码:





<$ p $ amp; gt; c(cumsum(rnorm(1 * 36)),cumsum(rnorm(1 * 36))) ,
Group = rep(c(A,B),each = 36),
Segment = rep(seq(1,12),each = 36))

plot <-ggplot(df1,aes(CO2,fill = Group))+
geom_density(alpha = 0.8)+
facet_wrap(〜Segment)+
theme_bw()+
实验室(x =CO2,y =密度)
#不应该有以下工作吗?
pb< - plotly_build(图)
pb $布局$保证金$ l < - 200
pb $布局$保证金$ b < - 100
pb


解决方案

我们使用



我们可以调整按照MLavoie的建议



轴图例其实不是一个传说,但注释,所以让我们先移动它:

  gp [['x']] [['layout'] ] [['annotations']] [[2]] [['x']] < -  -0.1 
gp%>%layout(margin = list(l = 75))


I'm having problems stopping the y-axis text from overlapping with the ticks using ggplotly around ggplot. How can I fix this? I've tried the following code:

set.seed(395)
df1<- data.frame(CO2= c(cumsum(rnorm(1*36)), cumsum(rnorm(1*36))),
                  Group= rep(c("A","B"), each=36),
                  Segment=rep(seq(1,12),each=36))

plot<-ggplot(df1, aes(CO2, fill = Group)) +
           geom_density(alpha = 0.8)+
           facet_wrap(~ Segment)+
           theme_bw()+
           labs(x="CO2", y="density")
#Shouldn't the following work?
    pb <- plotly_build(plot)
    pb$layout$margin$l <- 200
    pb$layout$margin$b <- 100
    pb

解决方案

Let's use a simple reproducible example from here.

library(gapminder)
library(plotly)
p <- ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + scale_x_log10()
p <- p + aes(color=continent) + facet_wrap(~year)
gp <- ggplotly(p)

We can move the adjust the margins as suggested by MLavoie but then our axis legend moves as well.

gp %>% layout(margin = list(l = 75))

The axis legend is actually not a legend but an annotation, so let's move it first:

gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1
gp %>% layout(margin = list(l = 75))

这篇关于R:ggplot和plotly axis margin不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆