0

I would like to create a spaghetti plot similar to this one here

1.

Unfortunately my data looks like this

2. I have 11 columns that have NA's, so I remove them with neuron1 <- drop_na(neuron)

Then I have a datatable with 13 columns and 169 rows. My goal is to display the expression of each gene across these 169 rows. Basically I would only need the "area" on the x-axis and on the y-axis the 11 genes. I am able to plot the data, but only when selecting the genes specifically e.g with this code:

ggplot(neuron1, aes(area)) + 
geom_line(aes(y=MAP2, group=1)) +
geom_line(aes(y=REEP1, group=1, color="red"))

It would be okay to repeat this 11 times but I have some datasets with more genes so it would really be nice to be able to group them properly and then run a short code.

Thank you very much in advance!

tjebo
  • 21,977
  • 7
  • 58
  • 94
Pipi_
  • 3
  • 2
  • Hi Welcome to SO. This is a line plot, not a spaghetti plot. You don't need to remove your NA, ggplot2 will do this for you. Bring the data into long format. – tjebo Dec 30 '20 at 17:24
  • or https://stackoverflow.com/q/3777174/7941188 – tjebo Dec 30 '20 at 17:26
  • Dear Tjebo, thank you very much for editing and answering! I'm pretty new to R so some basic functions still seem hard to me.. I saw that I can bring my datatable to a long format by the melt() or gather() function, but somehow I don't get the datatable that I would need. Could you tell me how I specifiy the different genes? – Pipi_ Dec 30 '20 at 17:45

1 Answers1

0

To long for comment. Lacking your data, this code may be a bit of a shot into the dark. Try something like this:

library (tidyverse)

yourdata %>%
  pivot_longer(cols = c(-area, -region), names_to = "key", values_to = "value") %>%

ggplot(aes(area, value) +
  geom_line(aes(group = key))

Not sure how this will work with your area as an x, because it's a categorical variable (therefore not sure if geom_line is the right choice for visualisation)

tjebo
  • 21,977
  • 7
  • 58
  • 94