我正在做一个分析运动员在身高和原籍国方面成功的项目。是否可以在R中创建直方图,其中x轴的高度分为多个区间,y轴表示在该特定区域中赢得的奖牌总数?
答案 0 :(得分:0)
对MrFlick来说,你真的想要创建一个基于垃圾箱的条形图。
我的回答是使用"发现"数据集作为我如何做到这一点的一个例子,创建一个每十年重要发现数量的条形图,当数据按年提供给我们时:
library(datasets)
library(ggplot2)
# number of discoveries per year from 1860-1959
data(discoveries)
# make data frame and add a column for the year
test <- data.frame(discoveries, year = c(1860:1959))
#set size of bin
bin_size = 10
#create bin field based on test$year and test$bin_size
test$year_bins <- findInterval(test$year, seq(if(round(min(test$year)) == min(test$year)) {
min(test$year)
} else {
(round(min(test$year)/bin_size)-1)*bin_size
},
if(round(max(test$year)) == max(test$year)) {
max(test$year)
} else {
(round(max(test$year)/bin_size)+1)*bin_size
},
bin_size))
#aggregate total discoveries by bin number
test2 <- aggregate(discoveries ~ year_bins, data = test, FUN = sum)
#create a bar chart for test3
ggplot(data= test2, aes(x=year_bins, y=discoveries)) +
geom_bar(colour="black", stat = "identity") +
guides(fill = FALSE) +
scale_x_continuous(breaks = 1:max(test2$year_bins))