Home > OS >  Plotting multiple columns from dataframe in R
Plotting multiple columns from dataframe in R

Time:09-29

This is my data:

> ptm <- read.csv('ptm.csv')

> ptm
               Method X1HSTB X1I6WA X1U19A X1YSAC X1YTBA X2CDSA X5O38A
1 Class weighted loss   0.37   0.83   0.44   0.49   0.76   0.64   0.68
2                  OS   0.58   0.81   0.29   0.33   0.76   0.62   0.68
3  2 phase swapped OS   0.40   0.81   0.29   0.30   0.75   0.70   0.63
4        Thresholding   0.42   0.81   0.29   0.29   0.70   0.69   0.66
5  2 phase swapped US   0.43   0.80   0.27   0.29   0.71   0.66   0.54
6                  US   0.35   0.77   0.27   0.53   0.70   0.26   0.69
7          Unbalanced   0.43   0.69   0.27   0.34   0.70   0.44   0.52
8             Control   0.75   0.87   0.80   0.61   0.82   0.84   0.83

This is a rough idea of the plot I'd like to make: [1]: https://i.stack.imgur.com/0KlX1.png

This is what I tried:

> ptm_plot <- ggplot(data=ptm, aes(x="1HSTB", "1I6WA", "1U19A", "1YSAC",      
"1YTBA","2CDSA", "5O38A",
  y=X1HSTB, X1I6WA, X1U19A, X1YSAC, X1YTBA, X2CDSA, X5O38A, colour=Method))   
  geom_point(stat="identity")
> ptm_plot

This was the result: [2]: https://i.stack.imgur.com/ZveLo.png

It's close to what I want but I can't seem to get all my columns in there. Any help would be appreciated!

CodePudding user response:

We may need to reshape to 'long' format

library(dplyr)
library(tidyr)
ptm %>%
   pivot_longer(cols = -Method) %>%
   ggplot(aes(x = name, y = value, fill = Method))   
    geom_point(stat = 'identity')
  • Related