如何将每行的最小和最多两列绘制为散点图

时间:2012-05-18 14:42:08

标签: r ggplot2 data-manipulation

我的数据框看起来像这样:

| id | length_bgn | length_end |
--------------------------------
| 1  |   209      |    215     |
| 2  |   324      |    125     |
| 3  |   167      |    156     |
| .. |   ...      |    ...     |
--------------------------------

我想制作一个散点图,其中X轴上的长度(每行)最小,Y轴上的每行最大。我试过了:

qplot(min(length_bgn, length_end), max(length_bgn, length_end), data=df)

但是,这会将两列最小值的单个点与两列的最大值进行对比。我使用了错误的功能吗?或者以某种方式操纵数据框会更好吗?请原谅任何天真,我仍然是R和ggplot2的新手。

非常感谢任何帮助。

编辑:使用上述代码的示例图:

example scatterplot

2 个答案:

答案 0 :(得分:4)

编辑

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)

答案 1 :(得分:3)

@ gauden的解决方案很好(在绘图之前进行处理),但是如果你真的希望ggplot为你做这项工作,你也可以。 [使用@gauden提供的df。]

qplot(pmin(length_bgn, length_end), pmax(length_bgn, length_end), data=df)

enter image description here

相关问题