我有两个不同的载体:
> x$layer
[1] 0.40000000 0.26153846 0.28305235 0.21058573 0.37593985 0.29437229
[7] 0.26306306 0.50982801 0.22368421 0.55715871 0.21241830 0.22757940
[13] 0.09388298 0.31101190 0.36363636 0.11277670 0.41382576 0.23955992
[19] 0.33333333 0.12829324 0.40106952 0.56818182 0.22222222 0.21591746
[25] 0.17647059 0.46190476 0.19523810 0.11502831 0.16916558 0.23270440
[31] 0.17787570 0.47401575
> un$layer
[1] -0.260869565 0.128649184 0.068607069 0.071428571 -0.166666667
[6] 0.311011905 0.363636364 0.021739130 0.110389610 0.112776699
[11] 0.136088710 0.000000000 0.076086957 0.101260937 0.238095238
[16] -0.466666667 -0.058823529 0.000000000 0.029411765 0.033799534
[21] 0.060728745 -0.188636364 -0.055555556 0.067910091 0.413825758
[26] 0.239559920 0.000000000 0.071693989 0.000000000 0.000000000
[31] 0.000000000 0.014473684 -0.316239316 -0.006666667 0.080000000
[36] 0.051282051 0.333333333 -0.042553191 0.128293242 -0.042218593
[41] 0.043262411 0.022435897 0.401069519 0.568181818 0.091419407
[46] -0.032258065 0.000000000 0.000000000 -0.241258741 -0.088993711
[51] 0.000000000 0.033043478 -0.044771242 -0.402777778 -0.058823529
我想绘制一个堆积的直方图' x'并且' un'具有两种不同颜色的0.1间隔 -
h=hist(b$layer, xlab='n',
breaks=seq(-0.6,0.6,by=0.1), ylim=c(0,40), xaxt='n', main="", col='blue')
h=hist(un$layer, xlab='n', beside=FALSE,
breaks=seq(-0.6,0.6,by=0.1), ylim=c(0,40), xaxt='n', main="", col='red', add=TRUE)
axis(side=1, at=seq(-0.6,0.6,by=0.1), labels=seq(-0.6,0.6,0.1))
如何策划' un'在' b'的顶部?看起来像是这样的论点:旁边= FALSE并不像条形图那样工作。虽然我更喜欢用基础包来做,但我不介意使用ggplot2,重塑等等,如果这更容易的话。
答案 0 :(得分:2)
这个怎么样?首先,这里的数据采用更友好的复制/可粘贴格式
xlayer <-
c(0.4, 0.26153846, 0.28305235, 0.21058573, 0.37593985, 0.29437229,
0.26306306, 0.50982801, 0.22368421, 0.55715871, 0.2124183, 0.2275794,
0.09388298, 0.3110119, 0.36363636, 0.1127767, 0.41382576, 0.23955992,
0.33333333, 0.12829324, 0.40106952, 0.56818182, 0.22222222, 0.21591746,
0.17647059, 0.46190476, 0.1952381, 0.11502831, 0.16916558, 0.2327044,
0.1778757, 0.47401575)
unlayer <-
c(-0.260869565, 0.128649184, 0.068607069, 0.071428571, -0.166666667,
0.311011905, 0.363636364, 0.02173913, 0.11038961, 0.112776699,
0.13608871, 0, 0.076086957, 0.101260937, 0.238095238, -0.466666667,
-0.058823529, 0, 0.029411765, 0.033799534, 0.060728745, -0.188636364,
-0.055555556, 0.067910091, 0.413825758, 0.23955992, 0, 0.071693989,
0, 0, 0, 0.014473684, -0.316239316, -0.006666667, 0.08, 0.051282051,
0.333333333, -0.042553191, 0.128293242, -0.042218593, 0.043262411,
0.022435897, 0.401069519, 0.568181818, 0.091419407, -0.032258065,
0, 0, -0.241258741, -0.088993711, 0, 0.033043478, -0.044771242,
-0.402777778, -0.058823529)
现在我将数据合并到一个data.frame中并剪切数据。
dd <- rbind(data.frame(layer="x", value=xlayer), data.frame(layer="un", value=unlayer))
然后分成你的特定剪辑
mybreaks<-seq(-0.6,0.6,by=0.1)
mybreaknames <- sprintf("%0.2f",diff(mybreaks)/2+mybreaks[-length(mybreaks)])
dd$group = cut(dd$value, breaks=mybreaks)
现在我绘制数据
barplot(with(dd, table(layer,group)), beside=F, names.arg=mybreaknames)
这给出了以下情节
编辑:
这是一个可能有更好的x轴
的版本mybreaks<-seq(-0.6,0.6,by=0.1)
dd$group = cut(dd$value, breaks=mybreaks)
xx<-barplot(with(dd, table(layer,group)), beside=F, xaxt="n", legend=levels(dd$layer))
ats<-xx[-length(xx)]+diff(xx)/2
ats<-c(2*ats[1]-ats[2], ats, 2*ats[length(ats)]-ats[length(ats)-1])
axis(1, at=ats, labels=sprintf("%.1f",mybreaks))
答案 1 :(得分:2)
这是一个ggplot
解决方案(使用MrFlick的格式很好的数据)
library(ggplot2)
dd <- data.frame(obs = c(xlayer, unlayer),
dist = c(rep("x", length(xlayer)), rep("un", length(unlayer))))
# Stacked, as requested
ggplot(dd, aes(x = obs, fill = dist)) +
geom_bar(position = "stack", binwidth = 0.1)
您会注意到堆叠的顺序与Flick先生的回答相反,可以通过重新排序dist
因素来修改。
# Nice and easy to switch to an overlapped-with-transparency alternative
ggplot(dd, aes(x = obs, fill = dist)) +
geom_bar(position = "identity", binwidth = 0.1, alpha = 0.6)
我发现这个更容易阅读,堆叠顺序并不重要。