0

I think this might have an easy answer - which I can't seem to find anywhere - so I'll forgo the reproducibility for the moment. I have a function designed to draw a ggplot2. I use mapply to pass it a few vectors of strings for the functions input parameters. The parameter of concern here is title. Which is fed a character vector with elements such as "this is a plot title".

Then the following code:

p <- ggplot(df, aes(x=date, y=value)) 

## plot the line 
p <- p + geom_line()

## add plot title
p <- p + ggtitle(title)

actually works just fine and the plot title is "this is a plot title" as expected.

However if the title is long and I want to specify a point to wrap the title using \n it fails to work.

Precisely if I feed ggtitle an element of "this is a \n plot title". I get exactly that contained in the quotes, rather than wrapping the title at the \n. My suspicion is I need eval, or paste or get, but my formations of such a request have failed to achieve the desired results. I appreciate the help.

UPDATE: I guess it must be the interaction with mapply. This should allow you to reproduce the problem.

create data.frame of strings as sample and assign it to fred.M.SA

  structure(list(RegionalCoverage = c("National", "National", "National", 
  "National", "National", "National"), GeographicLevel = c("MSA", 
  "MSA", "MSA", "MSA", "MSA", "MSA"), Category = c("Workers", "Workers", 
"Workers", "Workers", "Workers", "Workers"), Sector = c("Labor Market", 
"Labor Market", "Labor Market", "Labor Market", "Labor Market", 
"Labor Market"), Source2 = c("FRED", "FRED", "FRED", "FRED", 
"FRED", "FRED"), Title = c("Unemployment Rate in La Crosse, WI-MN (MSA)", 
"Trade, Transportation and Utilities Employment in La Crosse, WI-MN (MSA)", 
"Professional and Business Services Employment in La Crosse, WI-MN (MSA)", 
"Other Services Employment in La Crosse, WI-MN (MSA)", "Manufacturing Employment in La Crosse, WI-MN (MSA)", 
"Leisure and Hospitality Employment \\n in La Crosse, WI-MN (MSA)"
), SeriesID = c("LACR155UR", "LACR155TRAD", "LACR155PBSV", "LACR155SRVO", 
"LACR155MFG", "LACR155LEIH"), Units = c("%", "Thous. of Persons", 
"Thous. of Persons", "Thous. of Persons", "Thous. of Persons", 
"Thous. of Persons"), Freq = c("M", "M", "M", "M", "M", "M"), 
    Seas = c("SA", "SA", "SA", "SA", "SA", "SA"), OriginalSource = c("U.S. Department of Labor: Bureau of Labor Statistics", 
    "Federal Reserve Bank of St. Louis", "Federal Reserve Bank of St. Louis", 
    "Federal Reserve Bank of St. Louis", "Federal Reserve Bank of St. Louis", 
    "Federal Reserve Bank of St. Louis"), Method = c("ImportXML", 
    "ImportXML", "ImportXML", "ImportXML", "ImportXML", "ImportXML"
    ), LinktoSource = c("", "", "", "", "", ""), Link.to.Data.Spreadsheet.Name = c("", 
    "", "", "", "", ""), Link.to.Data.Storage = c("", "", "", 
    "", "", ""), Link.to.Data.Manipulation.File = c(NA, NA, NA, 
    NA, NA, NA), Link.to.Data.Manipulation.File.1 = c(NA, NA, 
    NA, NA, NA, NA)), .Names = c("RegionalCoverage", "GeographicLevel", 
"Category", "Sector", "Source2", "Title", "SeriesID", "Units", 
"Freq", "Seas", "OriginalSource", "Method", "LinktoSource", "Link.to.Data.Spreadsheet.Name", 
"Link.to.Data.Storage", "Link.to.Data.Manipulation.File", "Link.to.Data.Manipulation.File.1"
), row.names = c(27L, 34L, 44L, 46L, 47L, 48L), class = "data.frame")

MakelineFred  <- function(series, ylab="",xlab="", title="") {

require(ggplot2)      # hadley's plotting framework
require(scales)       # to adjust y axis scales
require(ggthemes)     # extra themes including tufte
require(xts)          # our favorite time series
require(gridExtra)   # for adding a caption
require(timeDate)    # for our prediction at the end
require(quantmod)    # 

# Get Data using quantmod
data <- getSymbols(series,src="FRED") #fred ignore from dates

# convert the string df to object df
data.xts <- get(data)   

## convert data to data.frame
df <- data.frame(
date=as.Date(index(data.xts)),
value=as.numeric(data.xts))

p <- ggplot(df, aes(x=date, y=value)) 

## plot the line 
p <- p + geom_line()

## add plot title
p <- p + ggtitle(title)
file <- paste("_",series,".png",sep="")
ggsave(file=file, plot=p,  width=6, height=4)

finally here is the mapply call.

mapply(MakelineFred, series=fred.M.SA$SeriesID, title=fred.M.SA$Title)
tjbrooks
  • 91
  • 2
  • 12
  • 3
    Well, it works on my system... – juba Mar 26 '13 at 15:16
  • Please add the output of `sessionInfo()` to your question. – Roland Mar 26 '13 at 15:18
  • Yes, you just need `\n` rather than `\\n`. – joran Mar 26 '13 at 15:54
  • 1
    I may have discovered my problem. It appears from the dput() above that when I read in the dataframe from a csv the character `\n` is being escaped. note the extra \ in the Leisure and Hospitality Employment title, which is not in my original csv file. so it appears to be my read.csv. – tjbrooks Mar 26 '13 at 16:05
  • The problem is that my original csv has just `\n` not `\\n`. So the read.csv appears to be adding the escape. However the alternative is apparently to evaluate the `\n` upon reading which is not what I want to do. – tjbrooks Mar 26 '13 at 16:07
  • I'm hobbling along with this solution for the title. http://stackoverflow.com/a/3935429/1725889 – tjbrooks Mar 26 '13 at 16:45
  • Or you could just use `gsub` to replace one with the other after using `read.csv`. – joran Mar 26 '13 at 16:53
  • @joran good point. Seems like I do stuff like this a lot in R. I can't quite figure out the correct syntax so I end up hacking around my own shortcomings. But its a great idea! Thanks – tjbrooks Mar 27 '13 at 02:12

0 Answers0