[英]R plot categorical and quantitaive variables
所以我有看起來像這樣的數據-
Neighborhood
Overall.Cond Blmngtn Blueste BrDale BrkSide ClearCr CollgCr Crawfor Edwards Gilbert Greens
1 0 0 0 0 0 0 0 1 0 0
2 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 2 12 0 0
4 0 0 0 1 2 0 3 6 0 0
5 14 1 9 15 15 163 20 53 87 3
6 0 7 3 21 6 15 19 32 8 2
7 0 0 1 33 6 9 16 22 3 0
8 0 0 0 7 3 5 8 7 1 0
9 0 0 0 1 0 0 5 1 0 0
我想為每個鄰域生成一個頻率直方圖-因此對於Blmngtn,我想要一個直方圖來顯示所有條件的分布。
為此,我將上面的內容放在了一個數據框中
Overall.Cond Neighborhood Freq
1 1 Blmngtn 0
2 2 Blmngtn 0
3 3 Blmngtn 0
4 4 Blmngtn 0
5 5 Blmngtn 14
6 6 Blmngtn 0
7 7 Blmngtn 0
如何為每個鄰域生成直方圖(使用兩種數據格式)?
# Your data
df <- read.table(text="Overall.Cond Blmngtn Blueste BrDale BrkSide ClearCr
CollgCr Crawfor Edwards Gilbert Greens
1 0 0 0 0 0 0 0 1 0 0
2 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 2 12 0 0
4 0 0 0 1 2 0 3 6 0 0
5 14 1 9 15 15 163 20 53 87 3
6 0 7 3 21 6 15 19 32 8 2
7 0 0 1 33 6 9 16 22 3 0
8 0 0 0 7 3 5 8 7 1 0
9 0 0 0 1 0 0 5 1 0 0",
header=T)
names(df) <- tolower(names(df))
# Get frequency for each neighbourhood
# I am assuming that overall.cond is the outcome and the neighbourhood frequencies
# indicate how many times these are observed
# i.e. for Blmngtn you want a histogram of c(5,5,5,5,5,5,5,5,5,5,5,5,5,5) [ie. 5, 14 times]
l <- lapply(df[,2:11] , function(i) rep(df$overall.cond , i))
# Plot histograms (the first nine neighbourhoods)
par(mfrow=c(3,3))
for(i in 1:9) {
hist(l[[i]] , main = names(l[i]))
}
# Plot barplots
par(mfrow=c(3,3))
for(i in 1:9) {
barplot(table(l[[i]]) , main = names(l[i]))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.