R: 只保留列表的一部分 & 是否可以反转 cbind?
R: Keeping only part of a list & is it possible to reverse cbind?
我有两个列表,每个列表有 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 个位置。目前我正在尝试使用排序位置,但是我在所有位置组合的图表中只得到 1 个栏。
barplotLocations <- barplot(height=sortedlocations, las=2, main="locations in vacancies", xlab="locations", ylab="number", cex.axis = .5, cex.names = .75)
帮忙? :)
这是使用 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")
我有两个列表,每个列表有 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 个位置。目前我正在尝试使用排序位置,但是我在所有位置组合的图表中只得到 1 个栏。
barplotLocations <- barplot(height=sortedlocations, las=2, main="locations in vacancies", xlab="locations", ylab="number", cex.axis = .5, cex.names = .75)
帮忙? :)
这是使用 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")