我有一些数据。例如,有两列。第一列数据是连续的。第二列值是二进制值(t | f)。我想用R语言在条形图中绘制它。在第一列中,我要将数字分组到0-100、101-200等类别中,然后在y轴上绘制t的数量。我在R中使用了ggplot2。但是我不清楚如何对这些x轴数据进行分组。
1 123 t 2 145 t 3 222 t 4 345 f 5 455 t 6 567 t 7 245 t 8 300 t 9 150 t 10 600 t 11 333 t
首先,这是data.frame中的示例数据
dd<-structure(list(V1 = 1:11, V2 = c(123L, 145L, 222L, 345L, 455L,
567L, 245L, 300L, 150L, 600L, 333L), V3 = structure(c(2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
.Label = c("f", "t"), class = "factor")), .Names = c("V1",
"V2", "V3"), class = "data.frame", row.names = c(NA, -11L))
这是一种绘图策略
ggplot(dd, aes(x=cut(V2, breaks=c(0,1:9*100)), weight=as.numeric(V3=="t"))) +
geom_bar(stat="bin") + xlab("value")
我们定义x
和weights
在aes()
。我们cut()
通常将数字分解为多个范围。然后,我们weights
将每个值转换为零/一值,这些值将一起添加到垃圾箱中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句