Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用Apply函数创建多个折线图(ggplot),并根据它们的列名为它们命名_R_Ggplot2 - Fatal编程技术网

R 使用Apply函数创建多个折线图(ggplot),并根据它们的列名为它们命名

R 使用Apply函数创建多个折线图(ggplot),并根据它们的列名为它们命名,r,ggplot2,R,Ggplot2,我有一个数据框架,包含不同时间点不同期限债券的收益率 例如,我的数据帧看起来是这样的 bond_duration <- c("three_mth", "one_yr", "two_yr", "five_yr", "seven_yr", "ten_yr") Jan_2007 <- c(3.12, 2.98, 3.01, 3.07, 3.11, 3.18) Feb_2007 <- c(2.93, 2.89, 2.91, 2.99, 3.02, 3.08) Mar_2007 <

我有一个数据框架,包含不同时间点不同期限债券的收益率

例如,我的数据帧看起来是这样的

bond_duration <- c("three_mth", "one_yr", "two_yr", "five_yr", "seven_yr", "ten_yr")
Jan_2007 <- c(3.12, 2.98, 3.01, 3.07, 3.11, 3.18)
Feb_2007 <- c(2.93, 2.89, 2.91, 2.99, 3.02, 3.08)
Mar_2007 <- c(2.62, 2.53, 2.51, 2.70, 2.79, 2.91)
df <- as.data.frame(cbind(bond_duration, Jan_2007, Feb_2007, Mar_2007))
df[, 2:4] <- apply(df[, 2:4], 2, as.numeric)
但是,当我尝试使用Apply函数为每个时间点循环创建多个折线图时,我的脚本工作正常。脚本能够为每个时间点创建多个线形图,但是每个线形图的标题是相同的。我使用了以下代码:

ggplot(data, aes(x = bond_duration, y = Jan_2007, group = 1)) + geom_point() + geom_line() + 
scale_x_discrete(limits = c("three_mth", "one_yr", "two_yr", "five_yr", "seven_yr", 
                            "ten_yr")) + 
ggtitle(paste(colnames(data)[2], " Yield Curve", sep = "")) +ylab("Yield (%)")
apply(data, 2, function(x) ggplot(data, aes(x = bond_duration, y = x, group = 1)) + geom_point() + geom_line() + 
      scale_x_discrete(limits = c("three_mth", "one_yr", "two_yr", "five_yr", "seven_yr", 
                                  "ten_yr")) + 
      ggtitle(paste(colnames(data)[x], " Yield Curve", sep = "")) + ylab("Yield (%)"))
我怀疑代码的ggtitle部分有问题。我希望每个线图都被命名为(特定的时间点)收益率曲线


感谢您的帮助。谢谢

如上所述,使用数据帧
df
,这将创建一个包含3个图的列表
p

p <- lapply(names(df)[2:4], function(x) {
  ggplot(df, aes_string(x = "bond_duration", y = x, group = 1)) + 
   geom_point() + 
   geom_line() + 
   scale_x_discrete(limits = c("three_mth", "one_yr", "two_yr", "five_yr", 
                               "seven_yr", "ten_yr")) + 
   ggtitle(paste0(x, " Yield Curve")) + ylab("Yield (%)")
})

如上所述,使用数据帧
df
,这将创建一个包含3个图的列表
p

p <- lapply(names(df)[2:4], function(x) {
  ggplot(df, aes_string(x = "bond_duration", y = x, group = 1)) + 
   geom_point() + 
   geom_line() + 
   scale_x_discrete(limits = c("three_mth", "one_yr", "two_yr", "five_yr", 
                               "seven_yr", "ten_yr")) + 
   ggtitle(paste0(x, " Yield Curve")) + ylab("Yield (%)")
})

谢谢它起作用了。只是好奇,为什么lapply采用名称(df)而不是df?您是否将名称向量传递给它而不是整个数据集?此外,为什么x=债券期限而不是x=债券期限?再次感谢!应用函数用于在列表或向量上应用函数。因此,您需要提供一个变量列表,在您的案例中,您希望为3个不同的月份创建一个绘图。这在
名称(df)[2:4]
中作为字符向量提供
aes_string
在编写创建绘图的函数时非常有用,因为您可以使用字符串或带引号的名称/调用来定义美学映射(我们传递给lappy的月份字符串列表)。因此,使用
aes\u string
,必须引用所有变量。我添加了代码以生成一个刻面图,如果您想同时显示所有图,该代码可能会更有用。同样,当您有一个图列表时,您也可以使用:
cowplot::plot\u grid(plotlist=list)
来显示所有感谢!它起作用了。只是好奇,为什么lapply采用名称(df)而不是df?您是否将名称向量传递给它而不是整个数据集?此外,为什么x=债券期限而不是x=债券期限?再次感谢!应用函数用于在列表或向量上应用函数。因此,您需要提供一个变量列表,在您的案例中,您希望为3个不同的月份创建一个绘图。这在
名称(df)[2:4]
中作为字符向量提供
aes_string
在编写创建绘图的函数时非常有用,因为您可以使用字符串或带引号的名称/调用来定义美学映射(我们传递给lappy的月份字符串列表)。因此,使用
aes\u string
,必须引用所有变量。我添加了代码以生成一个刻面图,如果您希望同时显示所有图,该代码可能会更有用。同样,当您有一个图列表时,您也可以使用:
cowplot::plot\u grid(plotlist=list)
来显示所有