【问题标题】:Axis title overlaps axis ticks using rmarkdown + plotly使用 rmarkdown + plotly 轴标题与轴刻度重叠
【发布时间】:2019-12-19 15:35:48
【问题描述】:

在 rmarkdown 中使用 plotly 时,轴标题与轴刻度重叠。我试过Plot margins in RMarkdown/knitrR: ggplot and plotly axis margin won't changeY Axis - Margin Size。没有成功。给你看我的代码:

只有 ggplot 可以正常工作:

    ---
    title: |
      SLIDES
   author: |
      ME   
   date: |
      `r format(Sys.time(), '%d %B, %Y')`

   output: slidy_presentation
   runtime: knitr
   ---
```{r my.chunk, echo=FALSE, fig.width = 18, fig.height = 9, results='asis', message=FALSE}
   require(ggplot2)
   require(plotly)
   d<-data.frame(Year=rep(c(2020:2024),6),Group=rep(c('A','B','C'),each=10),Item=rep(c('w','v'),each=5),
                 Value=round(runif(30,100,500),0))
   g<-ggplot(d, aes(Year,Value,fill=Item ))+ 
     geom_bar( position = "stack", stat = "summary", fun.y = "sum",na.rm=T)+
     scale_y_continuous(labels = scales::comma) +
     scale_fill_manual(values=c('red','green'), breaks=c('w','v'))+ labs(title='TITLE',x = 'Year', y = 'Value')+
     facet_grid(. ~ Group)+ theme(axis.text.x = element_text(angle = 90, hjust = 1,size=11,margin = margin(b = 5)))

   print(g)
```

如果使用 plotly x 轴标题与轴刻度标签重叠:

    ---
    title: |
      SLIDES
   author: |
      ME   
   date: |
      `r format(Sys.time(), '%d %B, %Y')`

   output: slidy_presentation
   runtime: knitr
   ---
```{r my.chunk, echo=FALSE, fig.width = 18, fig.height = 9, results='asis', message=FALSE}
   require(ggplot2)
   require(plotly)
   d<-data.frame(Year=rep(c(2020:2024),6),Group=rep(c('A','B','C'),each=10),Item=rep(c('w','v'),each=5),
                 Value=round(runif(30,100,500),0))
   g<-ggplot(d, aes(Year,Value,fill=Item ))+ 
     geom_bar( position = "stack", stat = "summary", fun.y = "sum",na.rm=T)+
     scale_y_continuous(labels = scales::comma) +
     scale_fill_manual(values=c('red','green'), breaks=c('w','v'))+ labs(title='TITLE',x = 'Year', y = 'Value')+
     facet_grid(. ~ Group)+ theme(axis.text.x = element_text(angle = 90, hjust = 1,size=11,margin = margin(b = 5)))

    ggplotly(g)%>%
    config(displayModeBar = FALSE) %>%
    layout(hovermode = 'compare')

```

谢谢

【问题讨论】:

  • 检查here
  • 向布局设置添加边距参数不起作用 ggplotly(g)%>% config(displayModeBar = FALSE) %>% layout(hovermode = 'compare',xaxis = list(automargin=TRUE),yaxis = list(automargin=TRUE))

标签: r ggplot2 plotly


【解决方案1】:

我终于解决了

   g[['x']][['layout']][["annotations"]][[1]]$y<- -0.15

以 g 作为情节对象

问候

【讨论】:

  • 你在哪里找到这个的,我一直在努力解决这个问题。你能解释一下吗?
  • 请原谅我花了这么长时间才回复,但我很长一段时间都处于离线状态。我从stackoverflow.com/questions/42763280/… 解决了这个问题,我可以理解的是,它是关于修改 plotly 对象的属性,在这种情况下,是包含 X 轴上标题的标签的 Y 位置。
猜你喜欢
  • 2019-05-11
  • 2020-03-30
  • 2021-01-31
  • 2012-03-14
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
相关资源
最近更新 更多