Home > Mobile >  how to make a consecutive figure from one data
how to make a consecutive figure from one data

Time:05-06

I have a data like this

I have a data like this

df<-structure(list(Accession = c("Sample1", "Sample1", "Sample1", 
"Sample1", "Sample1", "Sample1", "Sample2", "Sample2", "Sample2", 
"Sample2", "Sample2", "Sample2", "Sample3", "Sample3", "Sample3", 
"Sample3", "Sample3", "Sample3"), Var = c("Control_median", "Control_SD", 
"H1_median", "H1_SD", "H7_median", "H7_SD", "Control_median", 
"Control_SD", "H1_median", "H1_SD", "H7_median", "H7_SD", "Control_median", 
"Control_SD", "H1_median", "H1_SD", "H7_median", "H7_SD"), Value = c(278000, 
128549.6013, 247000, 1665333.7, 0, 116047.4041, 0, 0, 173000, 
112837.6415, 157000, 101154.9966, 226000, 196763.8178, 602000, 
362860.8549, 945000, 598936.5576)), class = "data.frame", row.names = c(NA, 
-18L))

it has 3 samples in 3 condition. I want to plot 3 figures

is there anyone who know how to make it? the SD stands for standard deviation

I want to make a line plot, so I try to make for one and then see how to make it for several

I take the first sample with all the conditions and I plot it like this

 mydd<- df[1:6,]
    ggplot(mydd, aes(x = Var, y = Value)) 
      geom_line(linetype = "dashed")  
      geom_point()  
      scale_x_discrete(limit = c("Control_median","H1_median","H7_median" ))

now the problem is adding the SD I want to add the SD to each but I dont know how to do that I tried to use this but the values are under each other and I cannot figure out how to do that , maybe I should rearrange my data ?

geom_errorbar(aes(ymin = Value-sd, ymax = Value sd))

CodePudding user response:

Yes, you should reshape your data. You need the median and SD to be in their own columns, so you could do something like:

library(tidyverse)

pivot_wider(df, names_from = Var, values_from = Value) %>%
  pivot_longer(-1, names_pattern = "^(.*)_(.*)$", 
               names_to = c("group", ".value")) %>%
  split(.$Accession) %>%
  map(~ {ggplot(.x, aes(group, median))  
              geom_point(size = 3)  
              geom_errorbar(aes(ymin = median - SD, ymax = median   SD),
                            width = 0.5, colour = "gray25")  
              theme_light(base_size = 16)  
              scale_y_continuous(labels = scales::comma)  
              labs(title = .x$Accession[1])
        ggsave(paste0(.x$Accession[1], ".png"))}
  )

Which will save the following 3 plots:

Sample1.png

enter image description here

Sample2.png

enter image description here

Sample3.png

enter image description here

  • Related