[英]R: ggplot and plotly axis margin won't change
我在使用ggplotly
周围的ggplot
阻止 y 轴文本与刻度重叠时ggplot
。 我怎样才能解决这个问题? 我试过以下代码:
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
让我们从这里使用一个简单的可重现示例。
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)
我们可以按照 MLavoie 的建议移动调整边距,但我们的轴图例也会随之移动。
gp %>% layout(margin = list(l = 75))
轴标签实际上不是标签而是注释,所以我们先移动它。 您可以在图形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>
好的,所以注释存储在 15 个列表中; "lifeExp" 是此列表的第二个( [[2]]
) 元素。 在这种情况下,“x”( [['x']]
)和“y”值分别控制左右/上下移动。
# 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))
从Github上找到一个优雅地解决了这个问题的答案。
layout_ggplotly <- function(gg, x = -0.02, y = -0.08){
# The 1 and 2 goes into the list that contains the options for the x and y axis labels respectively
gg[['x']][['layout']][['annotations']][[1]][['y']] <- x
gg[['x']][['layout']][['annotations']][[2]][['x']] <- y
gg
}
gp %>% layout_ggplotly
我发现上面的两个答案都非常有用。 但是,我注意到上面给出的layout_ggplotly()
函数仅在 x 和 y 轴标题存在时才能正常工作。 如果缺少其中任何一个 - 例如,由于theme(axis.title.x = element_blank())
- 然后使用...[[1]]...
和...[[2]]...
指的是错误的标题/注释。
我写了一个我在我的项目中使用的函数来解决这种限制,我相信这是建立在以前的答案的基础上的。
annotatify <- function(gg, x.y = -0.05, y.x = -0.05) {
wip <- gg[['x']][['layout']][['annotations']] %>%
enframe() %>%
mutate(value = map(value, as_tibble)) %>%
unnest(cols = c(value)) %>%
filter(!is.na(annotationType)) %>%
mutate(
x = case_when(x == 0.5 ~ x, TRUE ~ x.y),
y = case_when(y == 0.5 ~ y, TRUE ~ y.x)
) %>%
select(name, text, x, y) %>%
unique()
if (nrow(wip) == 2) {
for (i in 1:nrow(wip)) {
if (wip$x[i] == 0.50) {
gg[['x']][['layout']][['annotations']][[1]][['y']] <- wip$y[i]
} else {
gg[['x']][['layout']][['annotations']][[2]][['x']] <- wip$x[i]
}
}
} else if (wip$y == 0.5) {
gg[['x']][['layout']][['annotations']][[1]][['x']] <- wip$x
} else {
gg[['x']][['layout']][['annotations']][[1]][['y']] <- wip$y
}
gg
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.