-1

I have read this Plot all rows of a data frame with ggplot

I am trying to plot each row of a data and save it

df <- structure(list(X1 = c(0.006605138, 0.001165448, 0.006975109, 
0.002207839, 0.00187902, 0.002208638, 0.001199808, 0.001162252, 
0.001338847, 0.001106317), X2 = c(0.006041392, 0.001639298, 0.006140877, 
0.002958169, 0.002744017, 0.003107995, 0.001729594, 0.001582564, 
0.001971713, 0.001693236), X3 = c(0.024180351, 0.002189061, 0.027377442, 
0.002886651, 0.002816333, 0.003527908, 0.00231891, 0.001695633, 
0.00212034, 0.001962923)), row.names = c("AA", "AB", "AC", "AF", 
"AD", "JJ", "JA", "NM", "KA", "LF"), class = "data.frame")

I am trying to plot it one by one and then save it like this

plot(df[1,],length(df),type="l")

enter image description here

Learner
  • 757
  • 3
  • 15
  • 1
    Do you not want to use `ggplot`? And what is not working here? Are you getting errors? What else have you tried? – morgan121 Jan 25 '19 at 05:16
  • @RAB not really, I don't mind any way plotting it. I want to plot it with line but I cannot – Learner Jan 25 '19 at 05:18
  • What do you want the plot to look like? Its hard to help with so little information here – morgan121 Jan 25 '19 at 05:19
  • Why would it be necessary to plot row by row? – NelsonGon Jan 25 '19 at 05:19
  • @RAB I added an example – Learner Jan 25 '19 at 05:21
  • @NelsonGon it is because row are more important to see than column for me , you can twist it if it bothers you `t` can transpose it if it is easier to work with – Learner Jan 25 '19 at 05:22
  • there is nothing even remotely close to 40 in your dataset that you provided....I'm still really confused. What is that graph of? Is it related to your dataset? and which part of that graph/lines do you want saved? – morgan121 Jan 25 '19 at 05:23
  • @RAB you wanted an example, I put an example> I want to plot each row with line type. I don't know why it Is confusing , please tell me what is not clear and I will explain. this is just an example. in this example there are 4 points in my data each row has 3 data points – Learner Jan 25 '19 at 05:24

1 Answers1

2

You could convert your data to long format so that the x and y cords for each are all in columns and then use dlply to plot each name (which used to be each row) separately. Can also add a call to save the plots in there if required.

library(plyr)
library(tidyverse)
df_l <- df %>% rownames_to_column("Name") %>% gather(Var, Value, -Name) %>%
  arrange(Name) %>% group_by(Name) %>% mutate(n=row_number()) 

plotlist <- dlply(df_l, .(Name),
                  function(x) ggplot(x, aes(x = n, y = Value))+
                       geom_point()+geom_line())

Or save instead of assigning to a variable

d_ply(df_l, .(Name),
                  function(x) (ggplot(x, aes(x = n, y = Value))+
                    geom_point()+geom_line()) %>% 
       ggsave(., filename =  paste0("plt", x$Name[1],".jpg")))

assuming you want them in your current working directory

Sarah
  • 3,022
  • 1
  • 19
  • 40
  • Had a similar solution. Lol! – NelsonGon Jan 25 '19 at 05:31
  • @user2738526 wow, nice, can you also add a call to save the plots , I set my directory to a folder in desktop plot `setwd("~/Desktop/plot")` – Learner Jan 25 '19 at 05:35
  • https://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk This will help you with your saving of plots question – Javier Jan 25 '19 at 05:38
  • @Learner I've updated to save. If that one works if you could accept it as answered that would be great – Sarah Jan 25 '19 at 06:08