如何使用plotly在图例中包含希腊字母和粗体字符? [英] How to include greek letters and bold characters in a legend using plotly?

查看:131
本文介绍了如何使用plotly在图例中包含希腊字母和粗体字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个图例,在图例中用希腊字母表示,在x轴中用粗体字符表示.ggplot可以正常工作:

I am trying to make a graph with greek letters in the legend and bold characters in the legend of the x-axis. This works fine with 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轴的粗体图例会消失:

However the greek letter and the bold legend for the x-axis disappear when using plotly:

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

推荐答案

您需要进行两项小改动才能非常接近您的 ggplot 图.

You would need two small changes to get pretty close to your ggplot graph.

手动将 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).

Use the HTML code for pi in ggplot (works with RStudio in Windows) or add π (works with RStudio in Ubuntu).

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

这篇关于如何使用plotly在图例中包含希腊字母和粗体字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆