用 plotly/ggplotly 斜体化 strip.text

Italicizing strip.text with plotly/ggplotly

我一直在尝试使用 R 中“plotly”库中的 ggplot2 和 ggplotly 函数来制作一个在 y 轴上带有斜体标签的多面网格,但似乎无法实现。

这里我将使用 Plotly 编写的多面网格的“自由 Y 轴”示例: https://plotly.com/ggplot2/facet_grid/

我对此代码所做的唯一更改是在 ggplot theme() 中将两个轴的条带文本变大,并将 y 轴文本设为斜体。

library(reshape2)
library(ggplot2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

# With panels that have the same scaling, but different range (and therefore different physical sizes)
p <- p + facet_grid(sex ~ smoker, scales="free", space="free") +
              theme(strip.text.x=element_text(size=13),
                    strip.text.y=element_text(size=13,face="italic"))

p

fig <- ggplotly(p)

fig

ggplot (p) 生成的图如下所示,y 轴刻面标签为斜体: ggplot with proper italicization

ggplotly (fig) 生成的图如下所示: ggplotly with no italicization

您可以修改 p 中的数据,为斜体添加 html 标签

library(reshape2)
library(ggplot2)
library(plotly)

p <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

p <- p + facet_grid(sex ~ smoker, scales="free", space="free") +
    theme(strip.text.x=element_text(size=13),
          strip.text.y=element_text(size=13, face="italic"))

p$data$sex <- paste0("<i>", p$data$sex, "</i>")
fig <- ggplotly(p) 
fig

reprex package (v0.3.0)

于 2020-07-08 创建