[英]a barplot with two different variables on R
希望在同一條圖中繪制以下數據。 它是一個長度頻率條形圖,顯示了人口中男性和女性的長度等級:
我對此並不陌生,我不知道如何將數據放在這里,但這是一個示例:
Lengthclass Both Males Females
60 7 5 2
70 10 5 5
80 11 6 5
90 4 2 2
100 3 3 0
110 3 0 3
120 1 1 0
130 0 0 0
140 1 0 1
150 2 0 2
如果我使用此代碼: {barplot()}
,則不會在同一圖上給我所有三個變量。
我需要一個看起來像這樣但在R上的圖。
謝謝:)
classes <- levels(cut(60:100, breaks = c(60,70,80,90,100),
right =FALSE))
my.df <- data.frame(lengthclass = classes,
both = c(7,10,11,4),
male = c(5,5,6,2),
female = c(2,5,5,2))
barplot(t(as.matrix(my.df[, 2:4])),
beside = TRUE,
names.arg = my.df$lengthclass,
legend.text = TRUE,
ylim = c(0,12),
ylab = "number of individuals",
xlab = "Length class (cm)")
您的barplot被稱為“分組barplot”(與“ stacked barplot”相反)。
將數據barplot()
matrix
然后在對barplot()
調用中使用barplot()
beside=TRUE
。 這是一個使用內置數據集的示例:
> VADeaths
Rural Male Rural Female Urban Male Urban Female
50-54 11.7 8.7 15.4 8.4
55-59 18.1 11.7 24.3 13.6
60-64 26.9 20.3 37.0 19.3
65-69 41.0 30.9 54.6 35.1
70-74 66.0 54.3 71.1 50.0
> barplot(VADeaths,beside=TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.