R:ggplot和plotly轴裕度赢得';不变
我在使用R:ggplot和plotly轴裕度赢得';不变,r,ggplot2,plotly,R,Ggplot2,Plotly,我在使用ggplot周围的ggplot阻止y轴文本与记号重叠时遇到问题。我怎样才能解决这个问题?我尝试了以下代码: set.seed(395) df1让我们使用一个简单的可重复的例子 轴标签实际上不是标签而是注释,所以让我们先移动它。您可以在图形gp中查询注释的结构: # find the annotation you want to move str(gp[['x']][['layout']][['annotations']]) List of 15 $ :List of 13
ggplot
周围的ggplot
阻止y轴文本与记号重叠时遇到问题。我怎样才能解决这个问题?我尝试了以下代码:
set.seed(395)
df1让我们使用一个简单的可重复的例子
轴标签实际上不是标签而是注释,所以让我们先移动它。您可以在图形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>
从中找到一个优雅地解决了这个问题的答案
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\u ggplotly我发现上面两个答案都很有用。但是,我注意到上面给出的layout\u ggplotly()
函数只有在存在x轴和y轴标题时才能正常工作。如果缺少其中任何一个,例如,由于主题(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
}
注释%
变异(值=映射(值,作为不可匹配))%>%
unnest(cols=c(值))%>%
筛选器(!is.na(annotationType))%>%
变异(
当(x==0.5~x,真~x.y)时,
当(y==0.5~y,真~y.x)时,y=case_
) %>%
选择(名称、文本、x、y)%>%
唯一的()
如果(nrow(在制品)==2){
用于(i/1:nrow(在制品)){
如果(在制品$x[i]==0.50){
gg['x'][['layout'][['annotations'][[1]['y']]你试过这个吗:?它似乎没有任何区别。我应该澄清一下我使用的是GGplotly我在eval(expr,envir,enclose)中遇到了错误:ggplot
调用中找不到对象“y”。你的例子不起作用;我试过类似pb%>%layout的东西(margin=list(t=150,l=150,r=150,b=150))谢谢!这很有用!你对这段代码或文档有什么参考资料吗?这是一个plotly的东西还是你正在调整的ggplot功能?@HCAI:很高兴它起作用了!margins是一个plotly布局属性(我添加了一个指向原始答案的链接),第二部分是对ggplot进行调整/修改,以获得绘图转换结果。转换通常效果很好,但有时需要一些帮助。当我尝试此操作时,我得到:Error in*tmp*
[[2]]:下标超出bounds@MichaelSzczepaniak:我刚刚再试了一次,代码对我仍然有效。你能将你的代码和设置作为一个单独的问题发布吗?@Max:Thx以获得答复。我在这里发布了另一个带有代码的问题:
# 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>
# 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))
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
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
}