如何使用plotly在图例中包含希腊字母和粗体字符?

时间:2017-06-08 13:03:09

标签: r legend plotly

我正在尝试使用图例中的希腊字母和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

Graph with ggplot

然而,当使用plotly时,希腊字母和x轴的大胆图例消失了:

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

Same graph using plotly

1 个答案:

答案 0 :(得分:0)

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

手动将xaxis title设置为粗体

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

ggplot中使用pi代码(在Windows中与RStudio一起使用)或添加π(在Ubuntu中使用RStudio)。

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

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

enter image description here

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