简体   繁体   English

如何使用 R 表示分类数据

[英]How Represent Categorical Data using R

I'm learning now R programming language and I'm trying to plot a categorial variable called codeCountry which has 50 country codes.我现在正在学习 R 编程语言,我正在尝试绘制一个名为 codeCountry 的分类变量,它有 50 个国家/地区代码。 I saw all over the internet that you can use the function barplot() to do it, the problem is I can't have a good view of all the country codes.我在互联网上看到你可以使用函数 barplot() 来做到这一点,问题是我无法很好地查看所有国家/地区代码。 I have all my data in a table() object.我的所有数据都在 table() 对象中。 I have tried to do this:我试图这样做:

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

这是我得到的结果

This is the result I get, the problem is I would like to have all the codes avaliables.这是我得到的结果,问题是我想要所有可用的代码。 It is possible to do with R?可以用R吗?

Can someone give me some help on this?有人可以给我一些帮助吗?

Thank you!谢谢!

My preference is to use ggplot2.我的偏好是使用 ggplot2。 A small illustration though.虽然是一个小插图。

# 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