Making multiple graphs from one table using ggplot

Hi, I'm very new to R and having some difficulty managing my data.

I'm trying to track neutrophils crossing a membrane in the Z-plane and I want to display this graphically (x = time, y = position in Z-plane). My cell tracking software tracks many cells at once and outputs a table with all the tracking IDs (TID) in the 1st column with Time in the 2nd and Z-position in the 3rd. I'm tring to get R to start a new table each time the TID number changes. At the moment its plotting all the points in the table on one graph and connecting them all (as you can see in the graph provided). The package I'm using is ggplot2

The data.frame. I've provided is very minimalist, but my data outputs can contain 100s of Tracking ID's which I need to individually plot.

library(ggplot2)

testdata <- data.frame(TID=numeric(0), Time..sec.=numeric(0), Z..um.=numeric(0))
testdata <- edit(testdata)
print(testdata)
#>    TID Time..sec. Z..um.
#> 1    0          0    0.0
#> 2    0        120   13.8
#> 3    0        240    7.5
#> 4    0        360    7.2
#> 5    0        480    8.0
#> 6    1          0   12.9
#> 7    1        120    8.5
#> 8    1        240    8.4
#> 9    1        360    7.9
#> 10   1        480    7.8

#Plot testdata
ggplot(data = testdata, aes(x=Time..sec., y=Z..um., group=1))+
  geom_line()+
  geom_point()

Created on 2019-08-26 by the reprex package (v0.3.0)

Any guidance would be very much appreciated.

Welcome @LarkenI!

Your reprex did not work for me -- all I see is a link to a file that is on your personal computer.

To better understand your question, can you further explain what you want the output to be? Would it be useful to use the facet_wrap() function in ggplot, to separate images by TID? Do you want to save each one as a file on your computer (.png or .jpg)? In that case I'd either:

for tid1 in unique(df$TID) {
    df1 <- df %>% filter(TID == tid1)
    gg1 <- ggplot(df1, ...the rest of the ggplot code...)
    filename1 <- paste("plot_", tid1, ".png")
    ggsave(filename1, gg1)
}

or use functions and purrr to do the same:

print_tid_graph <- function(df0, tid1) {
  df1 <- df0 %>% filter(TID == tid1)
  gg1 <- ggplot(df1, ...the rest of the ggplot code...)
  filename1 <- paste("plot_", tid1, ".png")
  ggsave(filename1, gg1)
}
purrr::walk(.x = unique(df$TID), .f = print_tid_graph, df0 = df)

Hi @AJF
Thank you so much for your reply, I've fixed my reprex so this hopefully make more sense. I don't think I need to separate and save the different data sets (although that might actually be really helpful at some point) but rather just use TID to indicate when to start plotting a new graph.

So I have 4 different versions -- not sure if any of them are what you are thinking of:


# Library & Input Data

suppressPackageStartupMessages(library(tidyverse))
testdata <- tibble::tribble(
  ~TID, ~time_sec, ~z_um,
     0,         0,     0,
     0,       120,  13.8,
     0,       240,   7.5,
     0,       360,   7.2,
     0,       480,     8,
     1,         0,  12.9,
     1,       120,   8.5,
     1,       240,   8.4,
     1,       360,   7.9,
     1,       480,   7.8
  )

# Method 1

ggplot(data = testdata, aes(x=time_sec, y=z_um, group=TID))+
  geom_line()+
  geom_point()


# Method 2

ggplot(data = testdata, aes(x=time_sec, y=z_um, color=as.factor(TID)))+
  geom_line()+
  geom_point() +
  labs(color = "TID")


# Method 3

ggplot(data = testdata, aes(x=time_sec, y=z_um))+
  geom_line()+
  facet_wrap(~TID) +
  geom_point()


# Method 4

make_gg <- function(df0, TID0) {
  df1 <- df0 %>% dplyr::filter(TID == TID0)
  gg1 <- ggplot(data = df1, aes(x=time_sec, y=z_um))+
    geom_line()+
    geom_point() +
    labs(title = paste0("TID: ", TID0))
  print(gg1)
}

purrr::walk(.x = unique(testdata$TID), .f = make_gg, df0 = testdata)

Created on 2019-08-26 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.