Pat S
Pat S

Reputation: 490

Plotting formatted text

I need to generate a formatted string that is included on plots that indicates the median, interquartile range, and the number of observations this is based on. Here's what I'm trying...

plot(x=runif(4, 0,100), y=1:4, yaxt="n", ylab="", xlim=c(0,100))

m <- "2.3~x~10^6"
l <- "1.2~x~10^6"
u <- "4.5~x~10^6"
n <- 50
my_string <- paste0(m, "~(", l, "~-~", u, ")~plain(,)", "~N==~", n, "~)")
text(x=50, y=2.5, parse(text=my_string))

This is generating the following error...

Error in parse(text = my_string) : <text>:1:54: unexpected ')'
1: 2.3~x~10^6~(1.2~x~10^6~-~4.5~x~10^6)~plain(,)~N==~50~)

If I do

plot(x=runif(4, 0,100), y=1:4, yaxt="n", ylab="", xlim=c(0,100))

m <- "2.3~x~10^6"
l <- "1.2~x~10^6"
u <- "4.5~x~10^6"
n <- 50
my_string <- paste0(m, "~(", l, "~-~", u, ")")
text(x=50, y=2.5, parse(text=my_string))

I get the formatting I'm after with the exponent raised, but I don't have the N included. As an aside it seems to throw in additional spaces that ideally wouldn't be there.

Any suggestions to get the first code block working?

Upvotes: 1

Views: 70

Answers (1)

MrFlick
MrFlick

Reputation: 206596

Your first attempt has a rogue ~) at the end. This should work

my_string <- paste0(m, "~(", l, "~-~", u, ")~plain(',')", "~N==~", n)
text(x=50, y=2.5, parse(text=my_string))

Upvotes: 2

Related Questions