0

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

Graph with ggplot

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)) )

Same graph using plotly

Nicolas
  • 105
  • 2
  • 9

1 Answers1

0

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

Set the xaxis title to bold manually

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

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="")

or

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
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • Thanks @Maximilian! Unfortunately, the HTML code for pi does not seem to work for me. I just get a legend with "π = 5". Would you have any recommandations on this? Thanks! Here is my sessionInfo: R version 3.3.3 (2017-03-06) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X Yosemite 10.10.5 – Nicolas Jun 09 '17 at 08:47
  • @Nicolas: Weird! I just tried it with RStudio in Ubuntu and encountered the same problem. I'll update the question. – Maximilian Peters Jun 09 '17 at 09:51
  • Your alternative solution also works on my mac, thanks! – Nicolas Jun 09 '17 at 12:41