-1

I am working on a large data set with many rows and columns. The data here is showing is portion of it. I want to put all the values of column b to e against the variable a

I tried the melt function and looks like it doesn't work. Please help me to solve this one by using ggplot2 and if necessary for loop or appy function

enter image description here

Mark
  • 1
  • The data is in the "enter the image description here" – Mark Feb 01 '19 at 18:50
  • 3
    Welcome to StackOverflow! Please don't include your data as an image, since it's difficult for people to help you solve your problem if you don't include code they can run on their own machine to reproduce your situation. Take a look at this advice: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/ – qdread Feb 01 '19 at 18:51

1 Answers1

0

You can use the function gather() (library tidyr), that will regroup the column b to e together. Then, you can plot with ggplot() and split your graph with the function facet_grid().

df %>% 
gather(2:5, key = myColumns, value = myValues) %>% 
ggplot(aes(x = a, y = myValues, color = myColumns)) + 
geom_line() + 
facet_grid(myColumns ~ .)

enter image description here

demarsylvain
  • 2,103
  • 2
  • 14
  • 33
  • Thanks a lot! Can you show it putting the all the lines in the same plot instead of four different graph and also avoiding the pipe operator – Mark Feb 01 '19 at 20:00
  • To prevent the pipe operator, you can create temporary object. `df2 <- gather(df, 2:5, key = myColumns, value = myValues)`. Then, don't use `facet_grid()` if you want lines in the same graph. The parameter `color =` will create the four lines (one by levels of the variable you give to the parameter). – demarsylvain Feb 01 '19 at 20:28