英文:
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:
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都有自己的轴。因此,xaxis
和yaxis
仅对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)
英文:
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 <- list(tickfont = list(size = 8))
ggplotly(f1) %>%
layout(
xaxis = axis,
xaxis2 = axis,
xaxis3 = axis,
yaxis = axis,
yaxis2 = axis,
yaxis3 = axis
)
<!-- -->
For that reason IMHO the easier approach would be to style your axes directly in ggplot2
using theme
options:
f2 <- f1 +
theme(axis.text = element_text(size = 6))
ggplotly(f2)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论