ima
ima

Reputation: 155

Plot multiple graphs from loop with string name

I'm working with a large dataset set and I would like to plot several plots. My data looks likes this

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) 

In reality the SessionID contain information on the parameters used for the calculation of the result, i.e. C is the capacity so C1 actually means C=1). I would like to create plots from this data: on the X-axis I would like to plot the parameter C and on the y-axis the Costs only for the results of P=11 and P=22 where D=0. And than the same plot for D=2.

So far I have tried to split the string of the session with the help of this 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 (as I said I have a large data set with many parameters).

Upvotes: 0

Views: 59

Answers (1)

CPak
CPak

Reputation: 13581

Use myfun to convert your vectors into a data.frame. They require the packages purrr and dplyr

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)

Output

  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

Plot

ggplot2 lets you plot this easily

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

NOTE You can install the required packages with

install.packages(c("ggplot2", "purrr","dplyr"))

Upvotes: 1

Related Questions