Tou Mou
Tou Mou

Reputation: 1274

How to obtain a plot from dataframe

Good afternoon !

Assume we have the following dataframe :

[1,]         1 0.2943036 0.5019925 0.6485579 0.7519886 0.7719700 0.8390800
[2,]         2 0.2943036 0.5019925 0.6485579 0.7519886 0.8002251 0.8390800
[3,]         3 0.2943036 0.5019925 0.6485579 0.7519886 0.7719700 0.8163203
[4,]         4 0.2943036 0.5019925 0.6485579 0.7519886 0.8002251 0.8390800
[5,]         5 0.2943036 0.5019925 0.6485579 0.7519886 0.8002251 0.8390800
[6,]         6 0.2943036 0.5019925 0.6485579 0.7169111 0.7719700 0.8163203
                        
[1,] 0.8423178 0.8593898
[2,] 0.8703779 0.8955885
[3,] 0.8703779 0.8593898
[4,] 0.8703779 0.8955885
[5,] 0.8703779 0.8955885
[6,] 0.8423178 0.8593898

res_final=structure(c(1, 2, 3, 4, 5, 6, 0.294303552937154, 0.294303552937154, 
0.294303552937154, 0.294303552937154, 0.294303552937154, 0.294303552937154, 
0.501992524602876, 0.501992524602876, 0.501992524602876, 0.501992524602876, 
0.501992524602876, 0.501992524602876, 0.648557894001512, 0.648557894001512, 
0.648557894001512, 0.648557894001512, 0.648557894001512, 0.648557894001512, 
0.751988554448582, 0.751988554448582, 0.751988554448582, 0.751988554448582, 
0.751988554448582, 0.71691105972394, 0.771969986695426, 0.800225140644398, 
0.771969986695426, 0.800225140644398, 0.800225140644398, 0.771969986695426, 
0.839080029787269, 0.839080029787269, 0.816320316445505, 0.839080029787269, 
0.839080029787269, 0.816320316445505, 0.842317781397926, 0.870377899917965, 
0.870377899917965, 0.870377899917965, 0.870377899917965, 0.842317781397926, 
0.859389755570637, 0.895588541263924, 0.859389755570637, 0.895588541263924, 
0.895588541263924, 0.859389755570637), .Dim = c(6L, 9L), .Dimnames = list(
    NULL, c("res_final", "", "", "", "", "", "", "", "")))

I'm wanting to plot each row as a continous curve , i tried:

apply(res_final,1,function(x) plot(res_final[x,] , xlab = "Number of sensors ", ylab = "Cumulative detection probability", main = "Evolution of cumulative probabilities for 6-targets based on the Number of sensors"))

However , this doesn't allow to plot all of the 6 continious curves within the same plot. I'm also searching to distinguish between the curves using different colors.

I hope my question is clear.

Thank you a lot for your help !

Upvotes: 0

Views: 37

Answers (1)

Kenyon Ng
Kenyon Ng

Reputation: 48

The idea is to create a long data frame with 3 columns: x, y and a column that identifies the curves.

colnames(res_final) <- c('res_final', 2:9)
dat <- tibble::as_tibble(res_final) %>% 
  tidyr::pivot_longer(-res_final, names_to = 'x', values_to = 'y') %>%
  dplyr::mutate(res_final = as.factor(res_final),
                x = as.numeric(x))

I have added a column name to your matrix. I am assuming that will be your x axis?

I prefer ggplot, so here's the code

ggplot(dat) + geom_line(aes(x, y, col = res_final))

enter image description here

If you prefer steps, you can try

ggplot(dat) + geom_step(aes(x, y, col = res_final))

enter image description here

Upvotes: 1

Related Questions