Eric Archer - NOAA Federal
2013-Jul-05 03:14 UTC
[R] Substituting Greek symbols in some tick labels
I have a character vector that I'm using to label ticks in a dotchart. Some of the elements in the vector have an asterisk (*) where a Greek Delta needs to be placed when the plot is generated. Here's a simple example: x <- 1:4 x.lab <- c("a*a", "bbb", "c*c", "ddd") dotchart(x, labels = x.lab) The first and third labels should be 'a<Delta>a' and 'c<Delta>c'. I've tried things like, x.lab <- strsplit(x.lab, "[*]") x.lab <- lapply(x.lab, function(y) expression(paste(y, sep = Delta))) but because 'y' is unevaluated, the resulting list elements won't work as tick labels. I've tried to modify it by using bquote and substitute, but couldn't get anything closer. Any suggestions? Thanks! Cheers, eric -- Eric Archer, Ph.D. Southwest Fisheries Science Center NMFS, NOAA 8901 La Jolla Shores Drive La Jolla, CA 92037 USA 858-546-7121 (work) 858-546-7003 (FAX) Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp "The universe doesn't care what you believe. The wonderful thing about science is that it doesn't ask for your faith, it just asks for your eyes." - Randall Munroe "Lighthouses are more helpful than churches." - Benjamin Franklin "...but I'll take a GPS over either one." - John C. "Craig" George [[alternative HTML version deleted]]
On Jul 4, 2013, at 8:14 PM, Eric Archer - NOAA Federal wrote:> I have a character vector that I'm using to label ticks in a > dotchart. Some > of the elements in the vector have an asterisk (*) where a Greek Delta > needs to be placed when the plot is generated. Here's a simple > example: > > x <- 1:4 > x.lab <- c("a*a", "bbb", "c*c", "ddd") > dotchart(x, labels = x.lab) > > The first and third labels should be 'a<Delta>a' and 'c<Delta>c'. I've > tried things like, > > x.lab <- strsplit(x.lab, "[*]") > x.lab <- lapply(x.lab, function(y) expression(paste(y, sep = Delta)))The plotmath function paste has no sep argument. Do you want to do this "by hand"? (Since you have not offered values of 'y'.) x.lab <- expression( a*Delta*a, bbb, c*Delta*c, ddd) # Note use of "*" and no quotes in an expression vector. x <- 1:4 dotchart(x, labels = x.lab) -- David.> > but because 'y' is unevaluated, the resulting list elements won't > work as > tick labels. I've tried to modify it by using bquote and substitute, > but > couldn't get anything closer. Any suggestions? Thanks! > > Cheers, > eric > > -- > > Eric Archer, Ph.D. > Southwest Fisheries Science Center > NMFS, NOAA > 8901 La Jolla Shores Drive > La Jolla, CA 92037 USA > 858-546-7121 (work) > 858-546-7003 (FAX) > > Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics > ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp > > "The universe doesn't care what you believe. > The wonderful thing about science is that it > doesn't ask for your faith, it just asks > for your eyes." - Randall Munroe > > "Lighthouses are more helpful than churches." > - Benjamin Franklin > > "...but I'll take a GPS over either one." > - John C. "Craig" George > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD Alameda, CA, USA
On Jul 4, 2013, at 8:14 PM, Eric Archer - NOAA Federal wrote:> I have a character vector that I'm using to label ticks in a dotchart. Some > of the elements in the vector have an asterisk (*) where a Greek Delta > needs to be placed when the plot is generated. Here's a simple example: > > x <- 1:4 > x.lab <- c("a*a", "bbb", "c*c", "ddd") > dotchart(x, labels = x.lab) > > The first and third labels should be 'a<Delta>a' and 'c<Delta>c'. I've > tried things like, > > x.lab <- strsplit(x.lab, "[*]") > x.lab <- lapply(x.lab, function(y) expression(paste(y, sep = Delta))) > > but because 'y' is unevaluated, the resulting list elements won't work as > tick labels. I've tried to modify it by using bquote and substitute, but > couldn't get anything closer. Any suggestions? Thanks!Right. I would not throw away the "*" but rather change it to an inline Delta: x.lab <- gsub("\\*", "*Delta*", x.lab) # parse(text=x.lab) # returns: expression(a*Delta*a, bbb, c*Delta*c, ddd)# Then parse it: dotchart(x, labels = parse(text=x.lab) )> > Cheers, > eric > > -- > > Eric Archer, Ph.D. > Southwest Fisheries Science Center > NMFS, NOAA > 8901 La Jolla Shores Drive > La Jolla, CA 92037 USA > 858-546-7121 (work) > 858-546-7003 (FAX) > > Marine Mammal Genetics Group: swfsc.noaa.gov/prd-mmgenetics > ETP Cetacean Assessment Program: swfsc.noaa.gov/prd-etp > > "The universe doesn't care what you believe. > The wonderful thing about science is that it > doesn't ask for your faith, it just asks > for your eyes." - Randall Munroe > > "Lighthouses are more helpful than churches." > - Benjamin Franklin > > "...but I'll take a GPS over either one." > - John C. "Craig" George > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius Alameda, CA, USA