简体   繁体   中英

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. 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. I have all my data in a table() object. 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?

Can someone give me some help on this?

Thank you!

My preference is to use 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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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