如何在ggplotly中设置字体大小?

huangapple go评论97阅读模式
英文:

How to set the font size in ggplotly?

问题

我已经制作了一个ggplot图表。

library(ggplot2)
library(ggplotly)

f1 <- diamonds |> group_by(cut, clarity, color) |> count() |> 
  group_by(cut) |> mutate(N=sum(n)) |> 
      ggplot() +
      aes(
        x = cut,
        y = n,
        fill = cut
      ) +
      geom_col() +
      theme(legend.position = "none")+
      coord_flip()+
  facet_wrap(vars(clarity))
f1

我想将其转换为ggplotly对象。

ggplotly(f1)%>% 
        layout(legend=list(font = list(size = 8)), 
            xaxis = list(titlefont = list(size = 8), tickfont = list(size = 8)),
            yaxis = list(titlefont = list(size = 8), tickfont = list(size = 8)))

然而,文本大小只在第一个子图中更改。我想要将大小应用于所有子图。

英文:

I have made a ggplot

library(ggplot2)
library(ggplotly)

f1 <- diamonds |> group_by(cut,clarity,color) |>  count() |> 
  group_by(cut) |> mutate(N=sum(n)) |> 
      ggplot() +
      aes(
        x =  cut,
        y = n,
        fill = cut
      ) +
      geom_col() +
      theme(legend.position = "none")+
      coord_flip()+
  facet_wrap(vars(clarity))
f1

I want to convert to ggplotly object

ggplotly(f1)%>% 
        layout(legend=list(font = list(size = 8)), 
            xaxis = list(titlefont = list(size = 8), tickfont = list(size = 8)),
            yaxis = list(titlefont = list(size = 8), tickfont = list(size = 8)))

However, the size of text is only changed in the 1st subplot. I would like to have the size applied to all subplots:

如何在ggplotly中设置字体大小?

The issue is I have it in a function so number of facets varies, so this post was not helpful: https://stackoverflow.com/questions/55303856/r-ggplotly-with-facet-wrap-axis-tick-size-not-changing-for-all-plots

答案1

得分: 1

在引用帖子的回答中已经指出,您必须分别为每个轴设置样式,即与ggplot2不同,plotly中的每个子图或facet都有自己的轴。因此,xaxisyaxis仅对facet面板的第一行或第一列产生影响。

library(ggplot2)
library(plotly, warn=FALSE)

axis <- list(tickfont = list(size = 8))

ggplotly(f1) %>%
  layout(
    xaxis = axis,
    xaxis2 = axis,
    xaxis3 = axis,
    yaxis = axis,
    yaxis2 = axis,
    yaxis3 = axis
  )

出于这个原因,我认为更简单的方法是直接在ggplot2中使用theme选项来设置轴的样式:

f2 <- f1 +
  theme(axis.text = element_text(size = 6))

ggplotly(f2)

如何在ggplotly中设置字体大小?

英文:

As already pointed out in the answer in the referenced post you have to style each axis separately, i.e. in contrast to ggplot2 each subplot or facet in plotly has it's own axis. As a consequence, xaxis and yaxis will only have an effect on the first row or column of the facet panels.

library(ggplot2)
library(plotly, warn=FALSE)

axis &lt;- list(tickfont = list(size = 8))

ggplotly(f1) %&gt;%
  layout(
    xaxis = axis,
    xaxis2 = axis,
    xaxis3 = axis,
    yaxis = axis,
    yaxis2 = axis,
    yaxis3 = axis
  )

如何在ggplotly中设置字体大小?<!-- -->

For that reason IMHO the easier approach would be to style your axes directly in ggplot2 using theme options:

f2 &lt;- f1 +
  theme(axis.text = element_text(size = 6))

ggplotly(f2)

如何在ggplotly中设置字体大小?<!-- -->

huangapple
  • 本文由 发表于 2023年6月22日 02:53:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定