2017-06-08 52 views
0

我想在传说中的希腊字母和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

然而希腊字母,为x轴的大胆Legend采用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

回答

0

你会需要进行两个小的更改才能接近您的ggplot图。

设置xaxistitle大胆手动

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

使用HTML代码PI在ggplot(在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 
+0

感谢@Maximilian!不幸的是,pi的HTML代码似乎不适合我。我只是得到一个带有“π = 5”的传奇。你会对此有任何建议吗? 谢谢! 这是我的sessionInfo:R版本3.3.3(2017-03-06) 平台:x86_64-apple-darwin13.4.0(64位) 运行于:OS X Yosemite 10.10.5 – Nicolas

+0

@Nicolas:奇怪!我刚刚在Ubuntu中使用RStudio尝试过,并遇到同样的问题。我会更新这个问题。 –

+0

您的替代解决方案也适用于我的Mac,谢谢! – Nicolas