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

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

问题描述

我在使用 ggplot 周围的 ggplotly 阻止 y 轴文本与刻度重叠时遇到问题.我怎样才能解决这个问题?我试过以下代码:

set.seed(395)df1<- data.frame(CO2= c(cumsum(rnorm(1*36)), cumsum(rnorm(1*36))),组=代表(c(A",B"),每个=36),段=rep(seq(1,12),each=36))图<-ggplot(df1, aes(CO2, fill = Group)) +geom_density(alpha = 0.8)+facet_wrap(~ 段)+主题_bw()+实验室(x="CO2", y="密度")#下面的工作不应该吗?pb <- plotly_build(plot)pb$layout$margin$l <- 200pb$layout$margin$b <- 100铅

解决方案

让我们使用一个来自

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

轴标签其实不是标签而是注解,所以我们先移动一下.可以查询图中注解的结构gp:

# 找到要移动的注解str(gp[['x']][['layout']][['annotations']])15 人名单$ : 13 的列表..$ 文本 : chr "gdpPercap"..$ x : 数字 0.5..$ y : 数字 -0.0294..$ showarrow : logi FALSE..$ 斧头:数字 0..$ ay : 数字 0..$ 字体:列表 3.. ..$ 颜色 : chr "rgba(0,0,0,1)".. ..$ 家庭:chr "".. ..$ 大小:数字 14.6..$外部参照:chr纸"..$ yref : chr "paper"..$ 文本角度:数字 0..$ xanchor : chr "center"..$ yanchor : chr "top"..$ annotationType: chr "axis"$ : 13 的列表..$ 文本 : chr "lifeExp"..$ x : 数量 -0.0346..$ y : 数字 0.5.... <截断>

好的,所以注释存储在 15 个列表中;"lifeExp" 是该列表的 second([[2]]) 元素.在这种情况下,x"([['x']])和y"值分别控制左右/上下移动.

# 检查x轴标签的当前x位置gp[['x']][['layout']][['annotations']][[2]][['x']][1] -0.03459532# 将标签进一步向左移动gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1gp %>% 布局(边距 = 列表(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 label is actually not a label but an annotation, so let's move it first. You can query the structure of the annotations in the graph gp:

# find the annotation you want to move
str(gp[['x']][['layout']][['annotations']]) 

List of 15
 $ :List of 13
  ..$ text          : chr "gdpPercap"
  ..$ x             : num 0.5
  ..$ y             : num -0.0294
  ..$ showarrow     : logi FALSE
  ..$ ax            : num 0
  ..$ ay            : num 0
  ..$ font          :List of 3
  .. ..$ color : chr "rgba(0,0,0,1)"
  .. ..$ family: chr ""
  .. ..$ size  : num 14.6
  ..$ xref          : chr "paper"
  ..$ yref          : chr "paper"
  ..$ textangle     : num 0
  ..$ xanchor       : chr "center"
  ..$ yanchor       : chr "top"
  ..$ annotationType: chr "axis"
 $ :List of 13
  ..$ text          : chr "lifeExp"
  ..$ x             : num -0.0346
  ..$ y             : num 0.5
.... <truncated>

Ok, so annotations are stored in a list of 15; "lifeExp" is the second([[2]]) element of this list. The "x" ([['x']]) and "y" values control the movement left and right/up and down in this case, respectively.

# Check current x-location of x-axis label
gp[['x']][['layout']][['annotations']][[2]][['x']]
[1] -0.03459532

# Move the label further to the left
gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1
gp %>% layout(margin = list(l = 75))

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

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