0

これには簡単な答えがあるかもしれないと思います-どこにも見つけられないようです-そのため、今のところ再現性を放棄します. を描画するように設計された関数がありますggplot2mapply関数の入力パラメータとして、いくつかの文字列ベクトルを渡すために使用します。ここで問題となるパラメータは です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"。でタイトルをラップするのではなく、引用符に含まれているものを正確に取得します\neval、 またはpasteまたはが必要ではないかと疑っていますが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