How to plot min and max of two columns per row as a scatterplot
-
11-12-2019 - |
Question
I have a dataframe that looks something like this:
| id | length_bgn | length_end |
--------------------------------
| 1 | 209 | 215 |
| 2 | 324 | 125 |
| 3 | 167 | 156 |
| .. | ... | ... |
--------------------------------
I would like to make a scatterplot with the minimum of the length (per row) on the X-axis and the maximum per row on the Y axis. I have tried:
qplot(min(length_bgn, length_end), max(length_bgn, length_end), data=df)
However this plots a single point of the minimum of both columns against the maximum of both columns. Am I using the wrong functions? Or would it be better to manipulate the dataframe somehow? Please excuse any naivety, I am still quite new to R and ggplot2.
Any help would be much appreciated.
Edit: Example plot using the above code:
Solution
EDIT
library("ggplot2")
df <- read.table(text = " id length_bgn length_end
1 209 215
2 324 125
3 167 156 ", header=TRUE)
df$x <- apply(df[,c(2,3)], 1, min)
df$y <- apply(df[,c(2,3)], 1, max)
qplot(x, y, data=df)
OTHER TIPS
@gauden's solution is good (to do the processing before plotting), but if you really want ggplot
to do that work for you too, you can. [Using df
that @gauden provided.]
qplot(pmin(length_bgn, length_end), pmax(length_bgn, length_end), data=df)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow