ggplot2图例中的希腊符号(密度图) [英] Greek Symbols in ggplot2 legend (density plot)

查看:182
本文介绍了ggplot2图例中的希腊符号(密度图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot中创建了一个密度图,并尝试在图例中使用希腊符号。这就是我试过的:

  value1 = 0.8 
value2 = 0.8
value3 = 0
greeks< - list(bquote(rho ==。(value1)),bquote(rho ==。(value2)),bquote(rho ==。(value3)))

ggplot data = df)+
stat_density(aes(x = R1,color =rho = -0,6,linetype =rho = -0,6),
adjust = 4,lwd = 0.5,geom =line,position =identity)+
stat_density(aes(x = R2,color =rho = 0,6,linetype =rho = 0,6),
adjust = 4,lwd = 0.5,geom =line,position =identity)+
stat_density(aes(x = R3,color =rho = 0,linetype =rho = 0 ),
adjust = 4,lwd = 0.5,geom =line,position =identity)+
xlim(-1,1)+
xlab(Renditen)+
ylab(Dichte)+
ggtitle(Renditeverteilung im Heston-Modell)+
theme(plot.title = element_text(face =bold,size = 16,vjust = 2,family =Times New Roman),
axis.title.x = element_text(vjust = -1,size = 14,family =Times New Roman),
axis.title.y = element_tex legendary.txt(size = 14,family =Times New Roman),legend.title = element_blank() ,
legend.margin =单位(1,cm),
legend.key.height =单位(1,line),
legend.key.size =单位(0.4 ,cm),
legend.key = element_rect(fill = NA),
legend.background = element_blank(),
plot.margin = unit(c(1,1,1 ,1),cm))+
scale_colour_manual(values = c(rho = -0,6=red,rho = 0,6=blue,
标准=希腊)+
scale_linetype_manual(值= c(rho = -0,6= 1,rho = 0,6= 1,
rho = 0= 3))

!( http://i.imgur.com/LOWfs63.jpg

如何能我得到了传说,在一个传说中展示希腊符号,颜色和线型?



Thx提前!

< p >编辑:这是数据框

 > (df)
R1 R2 R3
1 0.22338963 0.15997630 0.2014689661
2 0.04803470 -0.12353615 -0.0802556036
3 0.15555398 0.19013430 0.1984939928
4 0.07646570 -0.05518703 -0.0004357738
5 0.03526795 -0.05357581 -0.0103695887
6 0.14946339 0.06930905 0.1079376659

我的回答:

  value1 =0,6
value2 =0
value3 =-0,6
greeks< - list(bquote(rho ==。(value1)),bquote(rho ==。(value2)),bquote(rho ==。(value3)))


ggplot(data = df)+
stat_density(aes(x = R1,color =rho = -0,6),
adjust = 4,lwd = 0.5,geom = (a =(x = R2,color =rho = 0,6),
adjust = 4,lwd = 0.5,geom =line,position =identity)+
stat_density ,位置=身份)+
stat_density(aes(x = R3,color =rho = 0),
adjust = 4,lwd = 0.5,linetype = 2,geom =line ,position =identity)+
xlim(-1,1)+
xlab(Rendit + +
ylab(Dichte)+
ggtitle(Renditeverteilung im Heston-Modell)+
theme(plot.title = element_text(face =bold,size = 16,vjust = 2,family =Times New Roman),
axis.title.x = element_text(vjust = -1,size = 14,family =Times New Roman),
axis .title.y = element_text(vjust = -0.25,size = 14,family =Times New Roman),
legend.text = element_text(size = 14,family =Times New Roman),legend。 title = element_blank(),
legend.margin =单位(1,cm),
legend.key.height =单位(1,line),
legend.key。 size = unit(0.4,cm),
legend.key = element_rect(fill = NA),
legend.background = element_blank(),
plot.margin = unit(c (color = 1,1,1,1),cm))+
scale_colour_manual(values = c(red,blue,black),labels = greeks)+
guides(color = guide_legend(override.aes = list(linetype = c(1,2,1))))


解决方案

堆叠数据帧后进行简化绘图:

  df2 < -  stack(df)
df2 $ ind< - as.character(df2




$ b

并且做了很多没有成果的实验,我终于找到了:与ggplot规模,颜色和线型参数的问题

  value1 = 0.8 
value2 = 0.8
value3 = 0
greeks< - list(bquote(rho ==。 value1)),bquote(rho ==。(value2)),bquote(rho ==。(value3)))

ggplot(data = df2)+
stat_density(aes(x =值,group = ind,color = ind),
adjust = 4,lwd = 0.5,geom =line,position =identity)+
xlim(-3,3)+
xlab(Renditen)+
ylab(Dichte)+
ggtitle(Renditeverteilung im Heston-Modell)+
theme(plot.title = element_text(face = bold,size = 16,vjust = 2,family =Times New Roman),
axis.title.x = element_text(vjust = -1,size = 14,family =Times新罗马),
axis.title.y = element_text(vjust = -0.25,size = 14,family =Times New Roman),
legend.text = element_text(size = 14,family =Times New Roman),
legend.title = element_blank(),
legend.margin = unit(1,cm),
legend.key.height = unit(1 ,line),
legend.key.size =单位(0.4,cm),
legend.key = element_rect(fill = NA),
legend.background = element_blank ),
plot.margin = unit(c(1,1,1,1),cm))+
scale_colour_manual(values = 1:3,labels = greeks)
+指南(color = guide_legend(override.aes = list(linetype = 1:3)))

< img src =https://i.stack.imgur.com/YFS3B.pngalt =输入图片描述>



制作完成后注意到线型不匹配,我花费了大量的时间,试图没有成功地尝试锤子ggplot。我现在发现htat这是被认为是一个问题,但尚未解决:
https://github.com/hadley/ggplot2/issues/802


I created a density plot in ggplot and tried to use greek symbols in the legend. That is what I tried:

value1 = 0.8
value2 = 0.8
value3 = 0
greeks <- list(bquote(rho==.(value1)), bquote(rho==.(value2)), bquote(rho==.(value3)))

ggplot(data=df)+
  stat_density(aes(x=R1, colour="rho = -0,6",linetype="rho = -0,6"), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  stat_density(aes(x=R2, colour="rho = 0,6",linetype="rho = 0,6"), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  stat_density(aes(x=R3, colour="rho = 0", linetype="rho = 0"), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  xlim(-1, 1)+
  xlab("Renditen")+
  ylab("Dichte")+
  ggtitle("Renditeverteilung im Heston-Modell")+
  theme(plot.title=element_text(face="bold", size=16, vjust=2, family="Times New Roman"),  
        axis.title.x=element_text(vjust=-1, size=14, family="Times New Roman"),
        axis.title.y=element_text(vjust=-0.25, size=14, family="Times New Roman"), 
        legend.text=element_text(size=14, family="Times New Roman"), legend.title=element_blank(),
        legend.margin=unit(1, "cm"),
        legend.key.height=unit(1, "line"), 
        legend.key.size=unit(0.4, "cm"), 
        legend.key=element_rect(fill=NA), 
        legend.background=element_blank(),
        plot.margin=unit(c(1,1,1,1), "cm"))+
  scale_colour_manual(values=c("rho = -0,6"="red", "rho = 0,6"="blue", 
                               "rho = 0"="black"), labels=greeks)+
  scale_linetype_manual(values=c("rho = -0,6"=1, "rho = 0,6"=1, 
                                 "rho = 0"=3))

!(http://i.imgur.com/LOWfs63.jpg)

How can I get the legend to show the greek symbols, the colors and linetypes in one legend?

Thx in advance!

Edit: This is the data frame

> head(df)
          R1          R2            R3
1 0.22338963  0.15997630  0.2014689661
2 0.04803470 -0.12353615 -0.0802556036
3 0.15555398  0.19013430  0.1984939928
4 0.07646570 -0.05518703 -0.0004357738
5 0.03526795 -0.05357581 -0.0103695887
6 0.14946339  0.06930905  0.1079376659

My answer:

value1 = "0,6"
value2 = "0"
value3 = "-0,6"
greeks <- list(bquote(rho==.(value1)), bquote(rho==.(value2)), bquote(rho==.(value3)))


ggplot(data=df)+
  stat_density(aes(x=R1, colour="rho = -0,6"), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  stat_density(aes(x=R2, colour="rho = 0,6"), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  stat_density(aes(x=R3, colour="rho = 0"), 
               adjust=4, lwd=0.5, linetype=2, geom="line", position="identity")+
  xlim(-1, 1)+
  xlab("Renditen")+
  ylab("Dichte")+
  ggtitle("Renditeverteilung im Heston-Modell")+
  theme(plot.title=element_text(face="bold", size=16, vjust=2, family="Times New Roman"),  
        axis.title.x=element_text(vjust=-1, size=14, family="Times New Roman"),
        axis.title.y=element_text(vjust=-0.25, size=14, family="Times New Roman"), 
        legend.text=element_text(size=14, family="Times New Roman"), legend.title=element_blank(),
        legend.margin=unit(1, "cm"),
        legend.key.height=unit(1, "line"), 
        legend.key.size=unit(0.4, "cm"), 
        legend.key=element_rect(fill=NA), 
        legend.background=element_blank(),
        plot.margin=unit(c(1,1,1,1), "cm"))+
  scale_colour_manual(values=c("red","blue", "black"), labels=greeks)+
  guides(colour=guide_legend(override.aes=list(linetype=c(1,2,1))))

解决方案

After stacking the dataframe to simplify the plotting:

df2 <- stack(df)
df2$ind <- as.character(df2$ind)

And doing quite a bit of fruitless experimentation, I finally found: problems with ggplot scale, color and linetype parameters

value1 = 0.8
value2 = 0.8
value3 = 0
greeks <- list(bquote(rho==.(value1)), bquote(rho==.(value2)), bquote(rho==.(value3)))

ggplot(data=df2)+
  stat_density(aes(x=values, group=ind, color=ind), 
               adjust=4, lwd=0.5, geom="line", position="identity")+
  xlim(-3, 3)+
  xlab("Renditen")+
  ylab("Dichte")+
  ggtitle("Renditeverteilung im Heston-Modell")+
  theme(plot.title=element_text(face="bold", size=16, vjust=2, family="Times New Roman"),  
        axis.title.x=element_text(vjust=-1, size=14, family="Times New Roman"),
        axis.title.y=element_text(vjust=-0.25, size=14, family="Times New Roman"), 
        legend.text=element_text(size=14, family="Times New Roman"), 
            legend.title=element_blank(),
        legend.margin=unit(1, "cm"),
        legend.key.height=unit(1, "line"), 
        legend.key.size=unit(0.4, "cm"), 
        legend.key=element_rect(fill=NA), 
        legend.background=element_blank(),
        plot.margin=unit(c(1,1,1,1), "cm"))+
  scale_colour_manual(values=1:3, labels=greeks) 
 +guides(colour=guide_legend(override.aes=list(linetype=1:3)))

After producing this and noticing that the linetypes didn't match up, I spent considerale fruitless time trying hammer ggplot into submission without success. I now find htat this is something that has been recognized as an issue but has not been solved: https://github.com/hadley/ggplot2/issues/802

这篇关于ggplot2图例中的希腊符号(密度图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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