2012-04-03 48 views
1

我有这样垂直50%的行添加到GGPLOT2点图

qplot(Index, Popularity ,data = data.slopeone.agg) 

创建GGPLOT2点图表现在我想在所有数据点的一半加上一条垂直线。换句话说,积分是0.5。我想添加这一行来查看这些数据的一部分使得50%。

如何在R中实现这一点?我知道geom_vline,但不知道如何确定vline的位置。

数据结构如下: 每一行都有一个ItemId这个项目的流行度和我的图表的索引来显示按受欢迎程度排序的值。

head(data.slopeone.agg) 
    Item Popularity Index 
184 258 0.07695880  1 
29 50 0.07294129  2 
121 181 0.07162558  3 
203 286 0.07030986  4 
225 313 0.06500478  5 
65 100 0.06366796  6 

我的表是这样的:http://img838.imageshack.us/img838/3194/popt.png

+0

我不知道什么结构data.slopeone.agg需要。你能包括一个小样本吗? – Seth 2012-04-03 16:24:02

+1

也许使用'中值'? – James 2012-04-03 16:26:21

回答

0

的指数现在我发现了如何解决我的问题。也许这不是最优雅的方式:

定义一个函数来获得人气的数据的50%的垂直线位置

getPopularityVLineIndex = function(popData){ 

    halfPopularity = sum(popData$Popularity)/2 

    # initialize helper variables 
    fiftyPercent = 0; 
    counter = 1; 

    # sum up the popularity values until the half of the sum is reached 
    while (fiftyPercent < halfPopularity) { 
     fiftyPercent = fiftyPercent + popData$Popularity[which(popData$Index == counter)] 
     counter = counter + 1 
    } 

    # the current counter value is the position of the vertical line 
    vLineIndex = counter 

    return (vLineIndex) 
} 

qplot(Index, Popularity ,data = popData) + geom_vline(xintercept = getPopularityVLineIndex(popData), colour="red", linetype = "longdash")) 

如果有人知道一个更优雅的方式,随意张贴。但也许我的问题现在更容易理解;)

1
p <- qplot(data=data.slopeone.agg, x = Index, y = Popularity) 

现在确定中间流行度值的 “索引”。 请注意,如果有偶数次的观察值,那么中位数将无法工作。

attach(data.slopeone.agg) 

得到平均人口观察

medpop=sort(Popularity)[floor(length(Popularity)/2)] 

获取价值

lineplace= Index[which(Popularity==medpop)] 
detach(data.slopeone.agg) 

p + geom_vline(xintercept = lineplace) 
+0

'wt'应该是什么? – 2012-04-03 17:58:06

+0

对不起,我更正了我的答案。我想你想要x变量的中位数,所以'索引'在你的情况。 – Seth 2012-04-03 18:25:59

+0

否@我不希望在指数的50%处有一条线。我想在曲线下面积分是0.5的位置上有一条线。那不是一回事。看看我发布的图表图片 – 2012-04-05 07:06:19