0

我认为这可能有一个简单的答案——我似乎在任何地方都找不到——所以我暂时放弃再现性。我有一个旨在绘制ggplot2. 我用来mapply向它传递一些字符串向量作为函数输入参数。这里关注的参数是title。它被输入一个字符向量,其中包含诸如"this is a plot title".

然后是下面的代码:

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

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

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

实际上工作得很好,情节标题也"this is a plot title"符合预期。

但是,如果标题很长,并且我想指定一个点来使用它来包装标题,\n则它无法正常工作。

确切地说,如果我给 ggtitle 提供一个"this is a \n plot title". 我得到的正是引号中包含的内容,而不是在\n. 我的怀疑是我需要eval, or pasteor get,但是我的这种请求的编队未能达到预期的结果。我很感激帮助。

更新:我想这一定是与 mapply 的交互。这应该允许您重现问题。

创建字符串的data.frame作为样本并将其分配给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)

终于来了mapply

mapply(MakelineFred, series=fred.M.SA$SeriesID, title=fred.M.SA$Title)
4

0 回答 0