R 在ggplot中绘制具有正负侧的累积直方图?
R 在ggplot中绘制具有正负侧的累积直方图?,r,plot,ggplot2,histogram,R,Plot,Ggplot2,Histogram,R版本3.1.1(2014-07-10)平台:i386-w64-mingw32/i386(32位) 我正在使用ggplot2制作直方图。其目的是在一个图中结合负向和正向的累积直方图。我可以很容易地分别画出负向和正向的直方图,但一旦我把它们结合起来,就会变得一团糟 样本数据: df <- structure(list(NEG = c(-42.962, -1.86, -13.275, -56.188, -2.25, -12.199, -3.953, -13.309, -4.512, -11.
R版本3.1.1(2014-07-10)平台:i386-w64-mingw32/i386(32位)
我正在使用ggplot2
制作直方图。其目的是在一个图中结合负向和正向的累积直方图。我可以很容易地分别画出负向和正向的直方图,但一旦我把它们结合起来,就会变得一团糟
样本数据:
df <- structure(list(NEG = c(-42.962, -1.86, -13.275, -56.188, -2.25,
-12.199, -3.953, -13.309, -4.512, -11.461, -19.813, -54.311,
-59.934, -7.045, -14.44, -40.829, -143.034, -233.009, -70.72,
-5.578), POS = c(180.328, 290.809, 156.894, 31.414, 629.74, 590.672,
268.89, 69.618, 415.007, 138.444, 10.139, 20.565, 106.027, 69.129,
19.809, 8.22, 53.711, 36.035, 11.694, 12.705)), .Names = c("NEG",
"POS"), row.names = c(NA, 20L), class = "data.frame")
消极方面的代码也起作用:
ggplot(df)+
geom_histogram(aes(x= NEG, y=cumsum(..count..)/4),binwidth=1)
但将这两层结合起来会产生混乱:
ggplot(df)+
geom_histogram(aes(x= POS, y=rev(cumsum(rev(..count..)))/4),binwidth=1)+
geom_histogram(aes(x= NEG, y=cumsum(..count..)/4),binwidth=1)
我希望你能帮助我
非常感谢 问题在于,对于这两个层,计算整个x轴上的累积和
ggplot(df)+
geom_histogram(aes(x= POS, y=ifelse(x>=0,
rev(cumsum(rev(..count..)))/4,
0)),
binwidth = 1)+
geom_histogram(aes(x= NEG, y=ifelse(x<=0,
cumsum(..count..)/4,
0)),
binwidth = 1)
ggplot(df)+
几何图形直方图(aes(x=POS,y=ifelse(x>=0,
修订版(累计修订版(修订版(…计数))/4,
0)),
binwidth=1+
geom_直方图(aes(x=NEG,y=ifelse)(xNice,清晰的问题,带有可复制的示例和您尝试过的代码~>(+1)。干杯。
ggplot(df)+
geom_histogram(aes(x= POS, y=ifelse(x>=0,
rev(cumsum(rev(..count..)))/4,
0)),
binwidth = 1)+
geom_histogram(aes(x= NEG, y=ifelse(x<=0,
cumsum(..count..)/4,
0)),
binwidth = 1)