Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带bin划分的R柱状图_R_Bar Chart - Fatal编程技术网

带bin划分的R柱状图

带bin划分的R柱状图,r,bar-chart,R,Bar Chart,我需要制作一个条形图,其中的数据被划分为多个箱子 我的数据如下所示: 1.0 5 1.2 4 2.4 1 4.3 6 5.2 10 然后在X轴上,我想要时间值,比如:[1-4],[4-5]等等(取决于csv文件中的数据) 在Y轴上,我想有一些引用,比如10,16,等等 我写了这个R代码: dataset <- read.csv("/Users/MNeptune/Documents/workspace R/BarPlot/times.csv") dataset <- data.mat

我需要制作一个条形图,其中的数据被划分为多个箱子

我的数据如下所示:

1.0 5
1.2 4
2.4 1
4.3 6
5.2 10
然后在X轴上,我想要时间值,比如:[1-4],[4-5]等等(取决于csv文件中的数据)

在Y轴上,我想有一些引用,比如10,16,等等

我写了这个R代码:

dataset <- read.csv("/Users/MNeptune/Documents/workspace R/BarPlot/times.csv")
dataset <- data.matrix(dataset, rownames.force = NA)
time <- dataset[,1]
occurence <- dataset[,2]
min <- min(time);
max <- max(time);

# Creo i bin
Groups <- cut(x = time, breaks = seq(from = min, to = max, by = 2))
Groups <- data.matrix(Groups, rownames.force = NA)

# Raggruppo i dati nei bin
Bygroup = tapply(occurence, Groups, sum)

# Faccio il plot dei bin
barplot(height = Bygroup, xlab = "time", ylab = "occurence")

dataset如果执行以下操作,您可以看到出了什么问题:

seq(from=1.0, to=5.2, by=2)
[1] 1 3 5

cut(c(1.0,1.2,2.4,4.3,5.2), breaks=seq(from=1.0, to=5.2, by=2))
[1] <NA>  (1,3] (1,3] (3,5] <NA> 
Levels: (1,3] (3,5]

如果需要不同的中断,当然可以调整
cut
breaks
参数。特别是,请注意
right
参数到
cut
,它允许您选择是在左侧还是右侧关闭中断间隔。
right=TRUE
是默认值,这就是为什么第一个t您的数据行已从原始代码中的
组中排除

更新:要回答后续问题,您可以按组
找到
最小值的bin,如下所示:

names(Bygroup)[which.min(Bygroup)]
[1] "(2,4]"
如果要按组对
值进行排序,以查找最低值、次最低值等。可以使用
rank
,它返回每个值的排序:

rank(Bygroup)
(0,2] (2,4] (4,6] 
    2     1     3 
rank(Bygroup)
(0,2] (2,4] (4,6] 
    2     1     3