Shaxi Liver Shaxi Liver - 2 months ago 6
R Question

Plot rows with similar name on the same graph

That's the data which I would like to plot:

structure(list(`10` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`34` = c(0, 0, 0, 0, 0, 0, 0, 0, 547725, 0),
`59` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`84` = c(0, 0, 0, 8173070.8, 0, 0, 0, 0, 0, 0),
`110` = c(0, 0, 0, 20302893.6, 0, 0, 0, 0, 0, 0),
`134` = c(0, 0, 0, 13696077.5, 0, 0, 0, 0, 0, 0),
`165` = c(1024325, 0, 0, 10486165.5, 0, 0, 0, 0, 0, 0),
`199` = c(1183267.5, 0, 0, 6015700, 0, 0, 0, 0, 0, 0),
`234` = c(1771708.3, 0, 0, 3384495.8, 3384495.8, 0, 0, 0, 0, 1144700),
`257` = c(2007712.3, 0, 0, 0, 6980230.6, 0, 0, 0, 0, 0),
`362` = c(3339118.9, 0, 0, 0, 7280030.6, 1119625, 0, 0, 0, 0),
`433` = c(973797.9, 0, 0, 0, 6230170, 1497625, 0, 0, 0, 0),
`506` = c(0, 0, 0, 0, 12905925, 0, 0, 0, 0, 0),
`581` = c(0, 2140050, 0, 0, 4560645.8, 0, 3170133.3, 0, 0, 0),
`652` = c(0, 639437.7, 639437.7, 0, 2349711.3, 0, 902318.3, 902318.3, 0, 0),
`733` = c(0, 0, 1397257.5, 0, 2274710, 0, 0, 1414458.3, 0, 0),
`818` = c(0, 0, 742731.8, 0, 2953550, 0, 0, 563876.7, 0, 0),
`896` = c(0, 0, 714654.7, 0, 1199563.3, 0, 0, 561000, 0, 0),
`972` = c(0, 0, 434271.5, 0, 1358225, 0, 0, 0, 0, 0),
`1039` = c(0, 0, 227435, 0, 934840, 0, 0, 0, 0, 0)),
.Names = c("10", "34", "59", "84", "110", "134", "165", "199", "234", "257", "362", "433", "506", "581", "652", "733", "818", "896", "972", "1039"),
row.names = c("Mark121_1", "Mark121_2", "Mark121_3", "Mark143_1", "Mark143_2", "Mark152_1", "Mark152_2", "Mark152_3", "Mark444_1", "Mark444_2"),
class = "data.frame")


I would like to put the lines for the rows which differ only in the number after
_
(dash) on the same plot. The different colors for the lines are necessary. I was thinking about using
matplot
but I have no idea how to select the rows with similar strings.

Using simple words I would like to have lines for:


  1. Mark121_*

  2. Mark143_*

  3. Mark152_*

  4. Mark444_*



on the same graph. It means 4 different graphs containing multiple lines.

Answer

This solution uses "dplyr" and "ggplot2" and "purrr". There is a large difference in scale so I change to log10, you might not want that.

df2 <- df %>% mutate(Name= rownames(.)) %>%
  gather(key=period, value=value,-Name) %>%
  mutate(person= sub("_.", "", Name), period=as.numeric(period))


df2 %>% ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
  geom_line() + facet_wrap(~person)

Edit: Additional request

In order to plot each figure individually

#This saves the figures as a list of plot objects
FiguresList <- unique(df2$person) %>% map(function(P) {
   df2 %>% filter(person ==P) %>% 
    ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
      geom_line()}
  )
FiguresList[[1]]

#This saves each plot as a pdf named by the person e.g "Mark121.pdf"
unique(df2$person) %>% map(function(P) {
   df2 %>% filter(person ==P) %>% 
    ggplot(., aes(x=period, y=log10(value), colour=Name, group=Name)) +
      geom_line()
    ggsave(paste(P,".pdf", sep=""))}
  )