将字符串与\ n传递给ggtitle的问题

时间:2013-03-26 15:13:11

标签: r ggplot2

我认为这可能有一个简单的答案 - 我似乎无法在任何地方找到 - 所以我暂时放弃了再现性。我有一个旨在绘制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,或pasteget,但我对此类请求的构成未能达到预期效果。我很感激帮助。

更新: 我想这肯定是与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)

0 个答案:

没有答案