图像由包含前10个人口密集国家列表的数据集组成。因此可以创建一个在x轴上具有国家/地区和y轴上的人口的条形图,其中数据类型即国家/地区值和人口值的类别是"因素"。是否可以在条形图上绘制这些值。任何人都可以提供帮助。
答案 0 :(得分:0)
我希望以下内容对您有所帮助 这是一个数据集,其中包含前10个人口稠密国家的数据:
df <- structure(list(Country = c("China", "India", "United States",
"Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh", "Russia",
"Mexico"), Population = structure(c(3L, 2L, 10L, 9L, 8L, 7L,
6L, 5L, 4L, 1L), .Label = c("130222815", "1342512706", "1388232693",
"143375006", "164827718", "191835936", "196744376", "211243220",
"263510146", "326474013"), class = "factor")), .Names = c("Country",
"Population"), row.names = 2:11, class = "data.frame")
df$Population
变量是factor
,人口值存储在因子级别中:
str(df)
'data.frame': 10 obs. of 2 variables:
$ Country : chr "China" "India" "United States" "Indonesia" ...
$ Population: Factor w/ 10 levels "130222815","1342512706",..: 3 2 10 9 8 7 6 5 4 1
我们可以如下绘制人口价值
首先,我们将df$Population
的级别转换为数字数组:
Pop <- as.numeric(as.character(df$Population))
str(Pop)
num [1:10] 1388232693 1342512706 326474013 263510146 211243220 ...
然后我们使用barplot
绘制人口值:
par(mar=c(4,7,1,1))
options(scipen=9)
barplot(Pop, names=df$Country, yaxt = "n")
yticks <- pretty(Pop)
axis(2, yticks, las=2, labels = prettyNum(yticks, big.mark = ","))