Sam
Sam

Reputation: 2605

Barplot using three columns

The data in the table is given below:

  Year  NSW Vic.  Qld   SA   WA Tas.  NT ACT Aust.
1 1917 1904 1409  683  440  306  193   5   3  4941
2 1927 2402 1727  873  565  392  211   4   8  6182
3 1937 2693 1853  993  589  457  233   6  11  6836
4 1947 2985 2055 1106  646  502  257  11  17  7579
5 1957 3625 2656 1413  873  688  326  21  38  9640
6 1967 4295 3274 1700 1110  879  375  62 103 11799
7 1977 5002 3837 2130 1286 1204  415 104 214 14192
8 1987 5617 4210 2675 1393 1496  449 158 265 16264
9 1997 6274 4605 3401 1480 1798  474 187 310 18532

I want to plot a graph with (Year) on my x-axis and (total value) on my Y-axis. The barplot should depicting the ACT and NT value for the respective (Years).

I tried the following command:

barplot(as.matrix(r_data$ACT, r_data$NT), main="r_data", ylab="Total", beside=TRUE)

The above command showed the barplot of ACT column per year but didn't show the Bar plot of NT column.

Upvotes: 2

Views: 1101

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

You have to create the matrix in a different way:

barplot(as.matrix(r_data[c("ACT", "NT")]), 
        main="r_data", ylab="Total", beside=TRUE)

You can also use cbind instead of as.matrix and keep the rest of your original approach:

barplot(cbind(r_data$ACT, r_data$NT), 
        main="r_data", ylab="Total", beside=TRUE)

enter image description here

Upvotes: 2

Related Questions