Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot iterating over rows in R

Tags:

r

I am trying to plot two Y values row by row onto the same plot. Here is an example dataset (5 rows):

T0 <- c(1842,2047,1831,2627,1769)
T3 <- c(13842,6847,4480,15873,17163)
test <- data.frame(T0, T3)

test
    T0    T3
1 1842 13842
2 2047  6847
3 1831  4480
4 2627 15873
5 1769 17163

I would like to produce a plot of the paired Y values (T0 and T3) for each row like this: enter image description here

My question is, how to iterate across rows and plot each pair of values?

like image 812
user95146 Avatar asked Jun 04 '26 07:06

user95146


2 Answers

Use matplot(), comes with R.

matplot(t(test), type='l')

enter image description here

like image 152
jay.sf Avatar answered Jun 07 '26 00:06

jay.sf


You could use geom_segment, supplying fixed values of x = 0 and xend = 1 while supplying your two columns as y = T0 and yend = T3

library(ggplot2)

ggplot(test) + 
  geom_segment(aes(x = 0, xend = 1, y = T0, yend = T3))

enter image description here

like image 45
Allan Cameron Avatar answered Jun 07 '26 01:06

Allan Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!