【问题标题】:How to include greek letters and bold characters in a legend using plotly?如何使用情节在图例中包含希腊字母和粗体字符?
【发布时间】:2017-11-10 05:13:53
【问题描述】:

我正在尝试使用图例中的希腊字母和 x 轴的图例中的粗体字符制作图表。这适用于 ggplot:

library(ggplot2)
library(plotly)
## Dataframe
df <- data.frame(dose=c("D0.5", "D1", "D2"),len=c(4.2, 10, 29.5))

## Legend with the greek letter pi
my.labs <- list(bquote(Pi==.(5)) )

## Plot with ggplot
p=ggplot(data=df, aes(x=dose, y=len, group=1,colour = paste("Pi = ",5,sep=""))) +
geom_line()+
geom_point()+
scale_colour_manual(values=3, labels=my.labs)+
theme(legend.title=element_blank(), legend.position = c(.1, .9),axis.title.x = element_text(face="bold", colour="black", size=10))

p

但是,当使用 plotly 时,x 轴的希腊字母和粗体图例会消失:

p=plotly_build(p)
style( p, hoverinfo = "x+y" ) %>% 
layout( legend = list(x = 0.1, y = 0.9, font=list(size=12)) )

【问题讨论】:

    标签: r legend plotly


    【解决方案1】:

    您需要进行两个小改动才能非常接近您的 ggplot 图表。

    手动将xaxis title 设置为粗体

    layout(legend = list(x = 0.1, 
                         y = 0.9, 
                       font=list(size=12)),
           xaxis = list(title = "<b>dose</b>")
    )
    

    ggplot 中使用 pi 的 HTML 代码(适用于 Windows 中的 RStudio)或添加 π(适用于 Ubuntu 中的 RStudio)。

    colour = paste("&pi; = ",5,sep="")
    

    colour = paste("π = ",5,sep="")
    

    library(ggplot2)
    library(plotly)
    ## Dataframe
    df <- data.frame(dose=c("D0.5", "D1", "D2"),len=c(4.2, 10, 29.5))
    
    ## Legend with the greek letter pi
    my.labs <- list(bquote(Pi==.(5)) )
    
    ## Plot with ggplot
    p=ggplot(data=df, aes(x=dose, y=len, group=1,colour = paste("&pi; = ",5,sep=""))) +
      geom_line()+
      geom_point()+
      scale_colour_manual(values=3, labels=my.labs)+
      theme(legend.title=element_blank(), legend.position = c(.1, .9),axis.title.x = element_text(face="bold", colour="black", size=10))
    p
    p=plotly_build(p)
    p <- style(p, hoverinfo = "x+y" ) %>% 
      layout(legend = list(x = 0.1, 
                           y = 0.9, 
                           font=list(size=12)),
             xaxis = list(title = "<b>dose</b>")
      )
    
    p
    

    【讨论】:

    • 谢谢@Maximilian!不幸的是,pi 的 HTML 代码似乎对我不起作用。我只是得到一个带有“π = 5”的图例。您对此有什么建议吗?谢谢!这是我的 sessionInfo:R 版本 3.3.3 (2017-03-06) 平台:x86_64-apple-darwin13.4.0 (64-bit) 运行于:OS X Yosemite 10.10.5
    • @Nicolas:奇怪!我刚刚在 Ubuntu 中使用 RStudio 进行了尝试,遇到了同样的问题。我会更新问题。
    • 您的替代解决方案也适用于我的 Mac,谢谢!
    猜你喜欢
    • 2011-08-28
    • 2012-03-04
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多