0

I have a data.frame similar to this table:

Gene P1 P2 P3   
A1    6  8  2   
A2   12  6  3   
A3    8  4  8

But, the word "Gene" is not in the data.frame. A1, A2, and A3 are the rownames.

Anyways, I would like to plot each "Gene" (i.e., each rowname) as a line in a graph where the x-axis corresponds to the Ps (i.e., P1, P2, and P3) and the y-axis corresponds to the values.

I included a picture of my RStudio that shows my data.frame.

Thank you.

  • Welcome to Stack Overflow. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data in a plain text format - for example the output from `dput(yourdata)`. We cannot copy/paste data from images. – neilfws Jun 15 '21 at 03:52
  • I tried. But the dataframe is so large I cannot even see it in the output window. The dataframe is 2966 observations of 64 variables. – Foreignbean Jun 15 '21 at 13:01
  • You can provide a representative subset of the data. Use `dput(head(yourdata, n))` where n is a number of rows, to reduce the number of rows. You could also select a subset of columns. – neilfws Jun 15 '21 at 22:50

1 Answers1

1

Using this code:

library(tidyverse)
df1 <- structure(list(Pool_1.sf = c(1.04654112319058, 0.908757359307814 ), Pool_10.sf = c(1.09143254057473, 0.763847944339038), Pool_11.sf = c(1.18750942376232, 0.970606492107592), Pool_12.sf = c(1.40039457695623, 0.987404435084985 ), Pool_13.sf = c(1.40110264045617, 1.05184583712403), Pool_14.sf = c(0.975272918230445, 1.05978729086064), Pool_15.sf = c(1.18870377730237, 1.350632186878 ), Pool_16.sf = c(1.25322756941453, 1.18822754009871), Pool_17.sf = c(1.29750766166164, 0.561735111884727), Pool_18.sf = c(1.27533917021409, 1.38423737777244 ), Pool_19.sf = c(0.918846998358604, 0.95201145069032), Pool_2.sf = c(0.748425883461423, 0.618517135274528), Pool_20.sf = c(1.35027456541736, 1.11503741384757 ), Pool_21.sf = c(1.06155444204363, 0.991570644521018), Pool_22.sf = c(1.64504600710891, 1.07862501013554), Pool_23.sf = c(1.51097405304331, 0.971834605384123 ), Pool_24.sf = c(1.18420663655483, 0.881393761143161), Pool_25.sf = c(0.925122055385438, 0.93313232911786), Pool_26.sf = c(2.12016328112954, 0.829308431444176 ), Pool_27.sf = c(1.59552456085871, 0.705278334816745), Pool_28.sf = c(1.75141617967796, 0.863808031900547), Pool_29.sf = c(1.71320920062242, 0.782291400605908 ), Pool_3.sf = c(1.09209110640701, 0.776979928448013), Pool_30.sf = c(0.925564956736256, 0.905870068022084), Pool_31.sf = c(1.00114849632652, 0.713896646497438 ), Pool_32.sf = c(0.769653226223374, 1.01812180736834), Pool_33.sf = c(1.64152662148587, 0.912970524890157), Pool_34.sf = c(1.39446534544181, 0.892464822723893 ), Pool_35.sf = c(1.36553718507047, 0.709121064448927), Pool_36.sf = c(1.48178605247809, 0.766690878721894), Pool_37.sf = c(1.05050355917415, 0.862090327153509 ), Pool_38.sf = c(1.36989138311191, 1.01473830511752), Pool_39.sf = c(1.42872045770954, 1.13176474162602), Pool_4.sf = c(1.11960747784989, 0.665514805707436 ), Pool_40.sf = c(1.90897625098439, 1.28419857359682), Pool_41.sf = c(1.2570145072185, 0.987813170293439), Pool_43.sf = c(1.14927112622372, 1.33241620047574 ), Pool_44.sf = c(1.02884805988699, 1.1077339415536), Pool_45.sf = c(1, 1), Pool_46.sf = c(1.15692580371101, 1.01663753799148), Pool_47.sf = c(1.02161799920975, 0.893420612254083), Pool_48.sf = c(0.991350522776138, 0.857531005677309 ), Pool_49.sf = c(0.666054364361721, 0.95128169066564), Pool_5.sf = c(1.27677591889858, 0.65869398169343), Pool_50.sf = c(1.04592846997826, 0.820965050229932 ), Pool_51.sf = c(1.46227623256989, 1.16138433421938), Pool_52.sf = c(1.1826746106421, 1.33429257056276), Pool_53.sf = c(1.16041540250292, 0.878127525893012 ), Pool_54.sf = c(1.14285567434696, 0.870429885808645), Pool_55.sf = c(1.40863161629042, 0.485422488325543), Pool_56.sf = c(1.81157566249543, 0.519084970767436 ), Pool_58.sf = c(1.69798017279487, 1.34651488521988), Pool_59.sf = c(1.55336058362464, 0.982570924872293), Pool_6.sf = c(0.769881996423631, 0.388931536871056 ), Pool_60.sf = c(1.36877679300045, 1.02579768967408), Pool_61.sf = c(1.34960258409398, 0.983191813100761), Pool_62.sf = c(1.08159654058587, 0.76318904250517 ), Pool_63.sf = c(1.98209270942409, 1.05152970776951), Pool_64.sf = c(1.86946484050877, 1.06489241167699), Pool_65.sf = c(1.48159508541161, 0.89626404845365 ), Pool_66.sf = c(1.42400489307256, 1.30732410445944), Pool_7.sf = c(1.17869553929846, 0.620813490764102), Pool_8.sf = c(1.35783021860687, 0.77620120083204 ), Pool_9.sf = c(1.32884787662603, 0.758057408306258)), row.names = c("Glyma.01G000400", "Glyma.01G000900"), class = "data.frame")

df1 %>%
  rownames_to_column() %>%
  pivot_longer(cols = -c(rowname)) %>%
  mutate(name = factor(name, levels = unique(name))) %>%
  ggplot(aes(x = name, y = value, group = rowname, color = rowname)) +
  geom_line() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1.05))

I get this plot:

example_1.png

Is this what you want? Otherwise, what do you want to change?

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Hi Jared. Unfortunately the dataframe is 2966 observations of 64 variables, so inputting the columns names by hand is not an option. – Foreignbean Jun 15 '21 at 13:03
  • I understand that. My answer is comprised of two sections: the first section is what is known as a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Anyone can use that code to generate an "example" dataset, which they can then use to evaluate the solution. In this case, the example dataset is called "df1". The second section is the actual solution. Your dataframe already has rownames, so you don't need to create new rownames, you just need to use the second section of code on your dataset to get your solution. Does that make sense? – jared_mamrot Jun 15 '21 at 23:00
  • I did not work. It plotted each rowname independently, not all together. – Foreignbean Jun 16 '21 at 22:39