An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140207/2a515c0b/attachment.pl>
Legend text populated with calculated values and super script?
5 messages · Douglas M. Hultstrand, Jim Lemon, David Winsemius +1 more
On 02/08/2014 02:54 AM, Douglas M. Hultstrand wrote:
Hello,
I am trying to generate a plot legend that contains calculated summary
statistics, one statistic is R^2. I have tried several variations using
the commands "expression" and "bqoute" as stated on the R help pages. I
have not been able to get the R^2 super script correct along with the
calculated statistics.
I provided an example below, what I want is the legend (wdt_leg and
spas_leg) as below but with the R^2 as superscript.
# Example Data
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
# create legend
wdt_leg<- paste("WDT (N = ", wdt_n,", Bias = ",wdt_mbias,", MAE =
",wdt_mae, ", R2 = ", wdt_R2, ")", sep="")
spas_leg<- paste("SPAS (N = ", spas_n,", Bias = ",spas_mbias,", MAE =
",spas_mae, ", R2 = ", spas_R2, ")", sep="")
# create plot
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
leg.txt<- c(spas_leg, wdt_leg)
legend("topleft", legend = leg.txt, col=c("red1","green4"), pch=c(1,3),
cex=0.85)
Hi Doug,
I set out to get your legend strings turned into expressions and after
several tries, gave up. Getting superscripts and subscripts in base
graphics is fairly easy:
supsubtext<-function(x,y,label,sup=NA,sub=NA,xadj=0.5,...) {
nlabchar<-nchar(label)
yadj<-rep(0.5,nlabchar)
if(!is.na(sup)) yadj[sup]<-0
if(!is.na(sub)) yadj[sub]<-1
labbits<-strsplit(label,"")[[1]]
currentx<-x
for(labchar in 1:nlabchar) {
text(x,y,labbits[labchar],adj=c(xadj,yadj[labchar]),...)
x<-x+strwidth(labbits[labchar])
}
}
The above function will display a line of text in which any character
the index of which is in "sup" will be superscripted and any in "sub"
will be subscripted. It is possible to add "cex" values to reduce the
size of those characters if necessary. You can then construct a
serviceable legend manually like this:
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
points(c(1,1),c(3.9,3.7),pch=c(1,3),col=c("red1","green4"))
wdt_leg<-"WDT (N = 50, Bias = 0.58, MAE = 2.1, R2 = 0.85)"
spas_leg<-"SPAS (N = 50, Bias = 0.58, MAE = 2.1, R2 = 0.85)"
supsubtext(1.2,3.9,spas_leg,sup=40)
supsubtext(1.2,3.7,wdt_leg,sup=40)
xylim<-par("usr")
segments(c(xylim[1],3.4),c(3.5,3.5),c(3.4,3.4),c(3.5,xylim[4]))
I am sure that someone more expert with expressions will post a more
elegant solution.
Jim
On Feb 7, 2014, at 7:54 AM, Douglas M. Hultstrand wrote:
Hello,
I am trying to generate a plot legend that contains calculated summary
statistics, one statistic is R^2. I have tried several variations using
the commands "expression" and "bqoute" as stated on the R help pages. I
have not been able to get the R^2 super script correct along with the
calculated statistics.
I provided an example below, what I want is the legend (wdt_leg and
spas_leg) as below but with the R^2 as superscript.
# Example Data
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
# create legend
wdt_leg <- paste("WDT (N = ", wdt_n,", Bias = ",wdt_mbias,", MAE =
",wdt_mae, ", R2 = ", wdt_R2, ")", sep="")
spas_leg <- paste("SPAS (N = ", spas_n,", Bias = ",spas_mbias,", MAE =
",spas_mae, ", R2 = ", spas_R2, ")", sep="")
# create plot
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
leg.txt <- c(spas_leg, wdt_leg)
legend("topleft", legend = leg.txt, col=c("red1","green4"), pch=c(1,3),
cex=0.85)
sublist <-
structure(list(wdt_n = 50, wdt_mbias = 0.58, wdt_mae = 2.1, wdt_R2 = 0.85,
spas_n = 50, spas_mbias = 0.58, spas_mae = 2.1, spas_R2 = 0.85), .Names = c("wdt_n",
"wdt_mbias", "wdt_mae", "wdt_R2", "spas_n", "spas_mbias", "spas_mae",
"spas_R2"))
legends <-c(
substitute(
atop(WDT * group("(", list(N == wdt_n, Bias == wdt_mbias, MAE == wdt_mae ), ")" ),
R^2 == wdt_R2 ) , env=sublist),
substitute(
atop(SPAS * group("(", list(N == spas_n, Bias == spas_mbias, MAE == spas_mae ), ")"),
R^2 == spas_R2 ), env=sublist) )
# I tried with: as.expression( lapply( exprlist, function(e) bquote(e) ) ) but failed repeatedly.
# In order to get `substitute` to cook the bacon, you need to use real expressions, not text.
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
legend("topleft",
legend = as.expression(legends),
col=c("red1","green4"), pch=c(1,3),
cex=0.85)
David Winsemius Alameda, CA, USA
Here's a bquote version:
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
wleg <- bquote(paste("WDT (", N == .(wdt_n), ", ",
Bias == .(wdt_mbias), ", ",
MAE == .(wdt_mae), ", ",
R^2 == .(wdt_R2), ")"))
sleg <- bquote(paste("SPAS (", N == .(spas_n), ", ",
Bias == .(spas_mbias),
", ", MAE == .(spas_mae),
", ", R^2 == .(spas_R2), ")"))
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
legend("topleft", legend = as.expression(c(sleg, wleg)),
col=c("red1","green4"), pch=c(1,3),
cex=0.85)
Dennis
On Fri, Feb 7, 2014 at 4:58 PM, David Winsemius <dwinsemius at comcast.net> wrote:
On Feb 7, 2014, at 7:54 AM, Douglas M. Hultstrand wrote:
Hello,
I am trying to generate a plot legend that contains calculated summary
statistics, one statistic is R^2. I have tried several variations using
the commands "expression" and "bqoute" as stated on the R help pages. I
have not been able to get the R^2 super script correct along with the
calculated statistics.
I provided an example below, what I want is the legend (wdt_leg and
spas_leg) as below but with the R^2 as superscript.
# Example Data
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
# create legend
wdt_leg <- paste("WDT (N = ", wdt_n,", Bias = ",wdt_mbias,", MAE =
",wdt_mae, ", R2 = ", wdt_R2, ")", sep="")
spas_leg <- paste("SPAS (N = ", spas_n,", Bias = ",spas_mbias,", MAE =
",spas_mae, ", R2 = ", spas_R2, ")", sep="")
# create plot
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
leg.txt <- c(spas_leg, wdt_leg)
legend("topleft", legend = leg.txt, col=c("red1","green4"), pch=c(1,3),
cex=0.85)
sublist <-
structure(list(wdt_n = 50, wdt_mbias = 0.58, wdt_mae = 2.1, wdt_R2 = 0.85,
spas_n = 50, spas_mbias = 0.58, spas_mae = 2.1, spas_R2 = 0.85), .Names = c("wdt_n",
"wdt_mbias", "wdt_mae", "wdt_R2", "spas_n", "spas_mbias", "spas_mae",
"spas_R2"))
legends <-c(
substitute(
atop(WDT * group("(", list(N == wdt_n, Bias == wdt_mbias, MAE == wdt_mae ), ")" ),
R^2 == wdt_R2 ) , env=sublist),
substitute(
atop(SPAS * group("(", list(N == spas_n, Bias == spas_mbias, MAE == spas_mae ), ")"),
R^2 == spas_R2 ), env=sublist) )
# I tried with: as.expression( lapply( exprlist, function(e) bquote(e) ) ) but failed repeatedly.
# In order to get `substitute` to cook the bacon, you need to use real expressions, not text.
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
legend("topleft",
legend = as.expression(legends),
col=c("red1","green4"), pch=c(1,3),
cex=0.85)
--
David Winsemius
Alameda, CA, USA
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
2 days later
Thanks everyone for the help. Dennis, the bquote version work great. Thanks, Doug
On 2/7/2014 7:08 PM, Dennis Murphy wrote:
Here's a bquote version:
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
wleg <- bquote(paste("WDT (", N == .(wdt_n), ", ",
Bias == .(wdt_mbias), ", ",
MAE == .(wdt_mae), ", ",
R^2 == .(wdt_R2), ")"))
sleg <- bquote(paste("SPAS (", N == .(spas_n), ", ",
Bias == .(spas_mbias),
", ", MAE == .(spas_mae),
", ", R^2 == .(spas_R2), ")"))
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
legend("topleft", legend = as.expression(c(sleg, wleg)),
col=c("red1","green4"), pch=c(1,3),
cex=0.85)
Dennis
On Fri, Feb 7, 2014 at 4:58 PM, David Winsemius <dwinsemius at comcast.net> wrote:
On Feb 7, 2014, at 7:54 AM, Douglas M. Hultstrand wrote:
Hello,
I am trying to generate a plot legend that contains calculated summary
statistics, one statistic is R^2. I have tried several variations using
the commands "expression" and "bqoute" as stated on the R help pages. I
have not been able to get the R^2 super script correct along with the
calculated statistics.
I provided an example below, what I want is the legend (wdt_leg and
spas_leg) as below but with the R^2 as superscript.
# Example Data
x=c(1,2,3,4); y=c(1,2,3,4); z=c(1.25,1.5,2.5,3.5)
# first stats based on data, used to populate legend
wdt_n = 50; wdt_mbias = 0.58
wdt_mae = 2.1; wdt_R2 = 0.85
# second stats based on data, used to populate legend
spas_n = 50; spas_mbias = 0.58
spas_mae = 2.1; spas_R2 = 0.85
# create legend
wdt_leg <- paste("WDT (N = ", wdt_n,", Bias = ",wdt_mbias,", MAE =
",wdt_mae, ", R2 = ", wdt_R2, ")", sep="")
spas_leg <- paste("SPAS (N = ", spas_n,", Bias = ",spas_mbias,", MAE =
",spas_mae, ", R2 = ", spas_R2, ")", sep="")
# create plot
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
leg.txt <- c(spas_leg, wdt_leg)
legend("topleft", legend = leg.txt, col=c("red1","green4"), pch=c(1,3),
cex=0.85)
sublist <-
structure(list(wdt_n = 50, wdt_mbias = 0.58, wdt_mae = 2.1, wdt_R2 = 0.85,
spas_n = 50, spas_mbias = 0.58, spas_mae = 2.1, spas_R2 = 0.85), .Names = c("wdt_n",
"wdt_mbias", "wdt_mae", "wdt_R2", "spas_n", "spas_mbias", "spas_mae",
"spas_R2"))
legends <-c(
substitute(
atop(WDT * group("(", list(N == wdt_n, Bias == wdt_mbias, MAE == wdt_mae ), ")" ),
R^2 == wdt_R2 ) , env=sublist),
substitute(
atop(SPAS * group("(", list(N == spas_n, Bias == spas_mbias, MAE == spas_mae ), ")"),
R^2 == spas_R2 ), env=sublist) )
# I tried with: as.expression( lapply( exprlist, function(e) bquote(e) ) ) but failed repeatedly.
# In order to get `substitute` to cook the bacon, you need to use real expressions, not text.
plot(x,y, col="red1", pch=1); lines(x,z, type="p", col="green4",pch=3)
legend("topleft",
legend = as.expression(legends),
col=c("red1","green4"), pch=c(1,3),
cex=0.85)
--
David Winsemius
Alameda, CA, USA
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
--------------------------------- Douglas M. Hultstrand, MS Senior Hydrometeorologist Metstat, Inc. Windsor, Colorado voice: 720.771.5840 email: dmhultst at metstat.com web: http://www.metstat.com