如何使用R表示分类数据

时间:2014-10-31 16:10:49

标签: r bar-chart

我正在学习R编程语言,我正在尝试绘制一个名为codeCountry的分类变量,它有50个国家代码。我在互联网上看到你可以使用函数barplot()来完成它,问题是我无法很好地查看所有国家代码。 我将所有数据都放在table()对象中。我试过这样做:

codeCountry<-table(port$code_country)
barplot(codeCountry, main = "Countries", xlab = "Country Code")

This is the result I get

这是我得到的结果,问题是我想拥有所有代码avalia。可以用R?

有人可以给我一些帮助吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我的偏好是使用ggplot2。虽然是一个小插图。

# toy data
set.seed(1)
country = paste0("AAA", sprintf("%02d", 1:50))
population = sample(50:2000, 50)
df = data.frame(country,  population)
# to graph
library(ggplot2)
ggplot(df, aes(x = country, y = population)) + 
  geom_bar(stat = "identity", fill = "grey80", colour = "black", width = 0.4) +
  theme(axis.text.x=element_text(angle=-45, hjust=0.001)) # "angle" will tilt the labels

enter image description here