Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

r - Using expression(paste( to insert math notation into a legend

I wish to substitute the following (yes, I've written it here in TeX format, just to be clear)
$P_{M1}(ilde{z}>z) - P_{M0}(ilde{z}>z)$
for the green line's legend entry (cptsdtbehavioralm), and
$P_{M2}(ilde{z}>z) - P_{M0}(ilde{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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...