使用表达式(粘贴(将数学符号插入图例中) [英] Using expression(paste( to insert math notation into a legend

查看:233
本文介绍了使用表达式(粘贴(将数学符号插入图例中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望替换以下内容(是的,我用TeX格式写了它,只是为了清楚)

$ P_ {M1}(\tilde {对于绿线的图例条目(cptsdtbehavioralm),以及 >
$ P_ {M2}(\tilde {z}> z) - P_ {M0}(\tilde {z}> z)$

为蓝线的图例条目(fullbehavioralmodel)。



下面是我生成该图的代码(我省略了10,000观测数据集和生成函数Fm0,Fm1和Fm2的变换): p>

  bmp(bias_plot_v4.bmp,width = 540,pointsize = 10)
ggplot(data.frame(x = c(0,80)),aes(x))+
stat_function(fun = function(x)((1-Fm1(x)) - (1- Fm0(x))),geom =line ,
aes(color =cptsdtbehavioralm),n = 1000)+
stat_function(fun = function(x)((1-Fm2(x)) - (1- Fm0 ),geom =line,
aes(color =fullbehavioralmodel),n = 1000)+
theme_bw()+
ylab(Probability)+
xlab (表达(粘贴(每3周鱼叉式网络钓鱼攻击的安全漏洞,
italic((z)))))+
主题(aspect.ratio = .618)+
theme(legend.position = c(0.845,0.8))+
theme(legend.key = element_blank())+
scale_color_manual(values = c(cptsdtbehavioralm=green2,
fullbehavioralmodel=blue),name =Bias)
dev.off()

每当我尝试用数学表达式替换cptsdtbehavioralm和fullbehavioralmodel - 例如为了保持简单,表达式(P [{M1}]) - 我得到以下类型的错误:

< pre $ Error:意想不到的'='in:
theme(legend.key = element_blank())+
scale_color_manual(values = c(expression(P [{ M1}]))=

我在智慧的结尾 - 任何见解或建议都会非常非常受欢迎。

解决方案

下面是一个简单的例子,介绍如何使用 values labels 来获得我想要的结果。



请注意将数据中的值映射为您希望使用的颜色,而标签是您要显示的标签(所以这就是您想要的位置(表达式)。

  ggplot(data = data.frame(x = c(0,5)),aes(x = x))+ 
stat_function(fun = dnorm,aes(color ='red'))+
stat_function(fun = dexp,aes(color ='blue'))+
scale_colour_manual (值= c( 'red'='red','blue'='blue'),name ='',
labels =表达式(P [M1](tilde(z)> 0),P [M0] (z)> 0)))


I wish to substitute the following (yes, I've written it here in TeX format, just to be clear)
$P_{M1}(\tilde{z}>z) - P_{M0}(\tilde{z}>z)$
for the green line's legend entry (cptsdtbehavioralm), and
$P_{M2}(\tilde{z}>z) - P_{M0}(\tilde{z}>z)$
for the blue line's legend entry (fullbehavioralmodel).

Here is the code with which I generated the plot (I am omitting the 10,000-observation dataset and the transforms to generate the functions Fm0, Fm1 and Fm2):

bmp("bias_plot_v4.bmp", width=540, pointsize=10)
ggplot(data.frame(x=c(0,80)),aes(x) ) +
   stat_function(fun=function(x)((1-Fm1(x)) - (1- Fm0(x))), geom="line", 
aes(colour="cptsdtbehavioralm"), n=1000) +
   stat_function(fun=function(x)((1-Fm2(x)) - (1- Fm0(x))), geom="line", 
aes(colour="fullbehavioralmodel"), n=1000) +
 theme_bw() +
   ylab("Probability") +
 xlab(expression(paste("Security breaches per 3-week spear-phishing campaign ", 
italic( (z) )))) +  
 theme(aspect.ratio=.618) + 
   theme(legend.position=c(0.845,0.8)) +
   theme(legend.key = element_blank()) +
   scale_color_manual(values = c("cptsdtbehavioralm"="green2", 
"fullbehavioralmodel" = "blue"), name="Bias")
dev.off()

Whenever I try to replace the "cptsdtbehavioralm" and "fullbehavioralmodel" with a math expression -- e.g. to keep it simple, expression(P[{M1}]) -- I get the following type of errors:

Error: unexpected '=' in:
"       theme(legend.key = element_blank()) +
   scale_color_manual(values = c(expression(P[{M1}]))="

I'm at my wit's end -- any insight or suggestions would be very, very welcome.

解决方案

Here is a simple example of how to use values and labels to get what I think you want.

Note that values map the values in the data to the colours you wish to use, while labels are the labels you want displayed (so this is where you would put the expression).

ggplot(data = data.frame(x= c(0,5)),aes(x=x)) +
    stat_function(fun=dnorm,aes(colour = 'red')) + 
    stat_function(fun = dexp, aes(colour = 'blue')) + 
    scale_colour_manual(values = c('red' = 'red','blue' = 'blue'),name = '', 
          labels = expression(P[M1](tilde(z)>0),P[M0](tilde(z)>0)))

这篇关于使用表达式(粘贴(将数学符号插入图例中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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