使用图形更改ggplot中的轴标题位置 [英] Change axis title position in ggplot using plotly

查看:1165
本文介绍了使用图形更改ggplot中的轴标题位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot 图表上使用 ggplotly 时遇到问题。它基本上与此处完全相同。然而,如果图不是分面的,所提供的解决方案不起作用,因为对象 gp [['x']] [['layout']] [['annotations']] 不存在。 (不幸的是,我没有足够的声望发表评论,因此我不得不提出这个问题作为一个新问题。)
因此,我无法找到如何调整y轴标题位置在非平面情节。这是来自另一个问题的工作示例

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

非工作对方:

  p < -  ggplot(gapminder,aes(x = gdpPercap,y = lifeExp))+ 
geom_point()+
scale_x_log10()
gp < - ggplotly(p)
gp [['x']] [['layout']] [['annotations' ] = [[2]] [['x']] < - -0.1
gp%>%layout(margin = list(l = 75))

(注意:在这个特定的例子中,没有重叠在轴标题和轴标签之间,但是我想指出一个事实,即轴标题重新定位不起作用,基于同一个例子。) 解决方案

  library(gapminder)
library(plotly )

gapminder $ grp< -
p < - ggplot(gapminder,aes(x = gdpPercap,y = lifeExp))+
geom_point()+
scale_x_log10()+
facet_wrap(〜grp)
gp< - ggplotly(p)
gp [['x']] [['layout']] [['annotations ']] [[2]] [['x']] < - -0.1
gp%>%layout(margin = list(l = 75))


I have an issue when using ggplotly on a ggplot graph. It is basically the exact same issue as here. However, the offered solution does not work if the plot is not facetted, since the object gp[['x']][['layout']][['annotations']] that should be altered does not exist in this case. (Unfortunately, I don't have enough reputation to comment, therefore I have to raise the issue as a new question.) As a result, I cannot find how to adjust the y axis title position in a non-faceted plot. This is the working example from the other question

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

And this is the non-working counterpart:

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + 
  geom_point() + 
  scale_x_log10()
gp <- ggplotly(p)
gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1
gp %>% layout(margin = list(l = 75))

(NOTE: in this specific example, there is no overlap between axis title and axis labels, however I wanted to point out the fact that axis title re-positioning does not work based on the same example.)

解决方案

Here is a trick that solves your problem:

library(gapminder)
library(plotly)

gapminder$grp <- ""
p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) + 
  geom_point() + 
  scale_x_log10() + 
  facet_wrap(~grp)
gp <- ggplotly(p) 
gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1
gp %>% layout(margin = list(l = 75))

这篇关于使用图形更改ggplot中的轴标题位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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