我有两个列表,每个包含37个项目(我在这里以3为例):
vacancy.locations <- c("Amsterdam", "Zuid Holland", "Utrecht")
count.locations <- c("11", "9", "40")
我将这两个列表绑定在一起locations <- cbind(vacancy.locations, count.locations
,这样我就可以按降序sortedlocations <- locations[order(-count.locations),]
排序,而不会失去11属于阿姆斯特丹,40属于乌得勒支的事实。
但是,现在我想只保留最多计数的10个位置。任何人都可以帮我这样做吗?
在此之后,我想绘制一个条形图中的前10个位置。目前我正在尝试使用sortedlocations,但是我只在图表中获得1个条,并且所有位置都合并在一起。
barplotLocations <- barplot(height=sortedlocations, las=2, main="locations in vacancies", xlab="locations", ylab="number", cex.axis = .5, cex.names = .75)
帮助? :)
答案 0 :(得分:0)
使用ggplot2
这是一种简单的方法vacancy.locations <- letters
count.locations <- sample(1:1000, length(letters))
location = cbind.data.frame(vacancy.locations,count.locations)
location_sorted = location[order(-count.locations),]
top_location = location_sorted[1:10,]
top_location[,1] = factor(top_location[,1], levels = top_location[,1][order(top_location[,2])])
library(ggplot2)
ggplot(data=top_location, aes(x=vacancy.locations, y=as.factor(count.locations))) +
geom_bar(stat="identity")