簡體   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