R绘制分类和定量变量

时间:2014-03-14 00:16:26

标签: r

所以我的数据看起来像这样 -

            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

如何为每个邻域生成直方图(使用任一格式的数据)?

1 个答案:

答案 0 :(得分: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]))
  }
相关问题