Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

plot - Plotting Rows vs Columns in R

Say I have a dataframe where the rows correspond to samples and each column corresponds to how often some molecule appears in the sample. How do I create a plot so that each molecule name is on the x axis and how often it appears in each sample on the y axis.

For example:

temp <- data.frame(mol1 = c(0.2,0.3,0.5), 
                   mol2 = c(0.4,0.7,0.3), 
                   mol3 = c(0.1, 0, 0.8))
rownames(temp) <- c("S1", "S2", "S3")

There would be a tick for "mol1" on the x-axis with the values 0.2, 0.3, and 0.5 above it labeled or with a legend. The same for the other molecules.

question from:https://stackoverflow.com/questions/65713088/plotting-rows-vs-columns-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Perhaps you can try to plot with ggplot :

library(tidyverse)

temp %>%
  rownames_to_column('sample') %>%
  pivot_longer(cols = -sample, names_to = 'molecule') %>%
  ggplot() + aes(sample, value, color = molecule) + 
 geom_point(size = 3) + theme_bw()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...