将数值数据与R中的因子组合

时间:2016-03-11 20:57:48

标签: r histogram bar-chart

事先可以说我对R完全不熟悉。

我需要协助处理以下问题: 一个表由几列组成,但只有两列(' Type'和'景观的百分比')与我相关。 我想展示景观的百分比' (数字)与' Type'如果可能的话,在直方图或条形图中(因子)。 主要是我想显示" Type"在x轴上(见下图)。

 'data.frame':   9 obs. of  6 variables:
 $ TYPE                   : Factor w/ 9 levels " Broad-leaved forest ",..: 2 3 7 5 6 4 9 8 1
 $ Total.Area             : int  1939 141 358 74 64 102 69 18 2
 $ Percantage.of.Landscape: num  69.25 5.04 12.79 2.64 2.29 ...
 $ Edge.Density           : num  19.36 4.29 8.93 2.5 3 ...
 $ X                      : logi  NA NA NA NA NA NA ...
 $ X.1                    : logi  NA NA NA NA NA NA ...

> levels(data$TYPE)
[1] " Broad-leaved forest "                      " Coniferous forest "                       
[3] " Discontinuous urban fabric "               " Inland marshes "                          
[5] " Land principally occupied by agriculture " " Mineral extraction sites "                
[7] " Mixed forest "                             " Non-irrigated arable land "               
[9] " Pastures "                                
> str(data$Percantage.of.Landscape)
 num [1:9] 69.25 5.04 12.79 2.64 2.29 ...
> data$Percantage.of.Landscape
[1] 69.2500  5.0357 12.7857  2.6429  2.2857  3.6429  2.4643  0.6429  0.0714

image

1 个答案:

答案 0 :(得分:0)

我假设您的数据如下所示?

percentage <- c(69.2500, 5.0357, 12.7857, 2.6429, 2.2857, 3.6429, 2.4643, 0.6429, 0.0714)
type <- as.factor(c("Broad-leaved forest", 
          " Coniferous forest", 
          "Discontinuous urban fabric", 
          "Inland marshes", 
          "Land principally occupied by agriculture", 
          "Mineral extraction sites", 
          "Mixed forest", 
          "Non-irrigated arable land", 
          "Pastures"))
other_variable1 <- 1:9
other_variable2 <- 13:5

data <- data.frame(percentage,
                   type,
                   other_variable1,
                   other_variable2)

然后你可以像这样做一个简单的条形图:

barplot(data$percentage,
        names.arg = data$type)