使用字符串名称从循环中绘制多个图形

Plot multiple graphs from loop with string name

我正在处理一个大型数据集,我想绘制多个图。我的数据看起来像这样

SessionID <- (Results-C1-P11-D0 ,  Results-CP0.9-P11-D0, Results-CP0.95-P11-D0, Results-C1-P22-D0 ,  Results-CP0.9-P22-D0, Results-CP0.95-P22-D0, Results-C1-P22-D2 ,  Results-CP0.9-P22-D2, Results-CP0.95-P22-D2 )
Costs <- (10, 20, 30, 40, 50, 60, 70, 80, 90) 

实际上,SessionID 包含有关用于计算结果的参数的信息,即 C 是容量,因此 C1 实际上表示 C=1)。 我想根据这些数据创建绘图:在 X 轴上,我想绘制参数 C,在 y 轴上,仅针对 P=11 和 P=22(其中 D=0)的结果绘制成本。并且比 D=2 的相同图。

到目前为止,我已经尝试借助此 here and here, but I don't know what the most efficient way is to split the information of the SessionID since I ultimately want to come up with a plot that is looped over the different parameters like is done here 拆分会话的字符串(正如我所说,我有一个包含许多参数的大型数据集)。

使用 myfun 将您的向量转换为 data.frame。他们需要软件包 purrrdplyr

myfun <- function(S, Costs) {
              require(purrr)
              require(dplyr)
              df <- do.call(rbind.data.frame, strsplit(S, "-")) %>%
                      setNames(c("Results","C","P","D")) %>%
                      cbind(Costs)
              return(df)
         }

df <- myfun(SessionID, Costs)

输出

  Results      C   P  D Costs
1 Results     C1 P11 D0    10
2 Results  CP0.9 P11 D0    20
3 Results CP0.95 P11 D0    30
4 Results     C1 P22 D0    40
5 Results  CP0.9 P22 D0    50
6 Results CP0.95 P22 D0    60
7 Results     C1 P22 D2    70
8 Results  CP0.9 P22 D2    80
9 Results CP0.95 P22 D2    90

情节

ggplot2 让您轻松绘制此图

library(ggplot2)
ggplot(data=df, aes(x=C, y=Costs, color=P)) +
 geom_point() +
 facet_wrap(~D) +
 theme_classic()

注意您可以使用

安装所需的包
install.packages(c("ggplot2", "purrr","dplyr"))