繁体   English   中英

如何使用 R 表示分类数据

[英]How Represent Categorical Data using R

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

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

这是我得到的结果

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

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

谢谢!

我的偏好是使用 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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM