2017-06-08 53 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