简体   繁体   中英

how to create grouped barchat with our ggplot in r

Year    Balance TotalDeposits
1   18837.44    18000
2   39313.74    36000
3   61571.47    54000
4   85765.63    72000
5   112064.68   90000
6   140651.75   108000
7   171725.89   126000
8   205503.49   144000
9   242219.73   162000
10  282130.29   180000
11  325513.06   198000

I have matrix

I would like to create a grouped barchat where x axis takes year as categorial variable. And bar represents balace and total deposits for corresponding year.

please suggest basic barchat function rather than ggplot or lattice functions.

I am reading the data from .csv file.

What I have tried so far

d <- read.table(text="Year  Balance TotalDeposits
1   18837.44    18000
2   39313.74    36000
3   61571.47    54000
4   85765.63    72000
5   112064.68   90000
6   140651.75   108000
7   171725.89   126000
8   205503.49   144000
9   242219.73   162000
10  282130.29   180000
11  325513.06   198000")
d <- do.call(rbind,d)
barplot(d , beside=TRUE)

For example:

mat <- as.matrix(t(d[,2:3]))
colnames(mat) <- d[,1]
barplot(mat , beside=TRUE,legend.text=TRUE,
    args.legend=list(x=8))

在此处输入图片说明

Where d is :

d <- read.table(text="Year  Balance TotalDeposits
1   18837.44    18000
2   39313.74    36000
3   61571.47    54000
4   85765.63    72000
5   112064.68   90000
6   140651.75   108000
7   171725.89   126000
8   205503.49   144000
9   242219.73   162000
10  282130.29   180000
11  325513.06   198000",header=TRUE)

I am not sure if this is exactly what you want...

# after reading in dataframe into d

m <- t(d)
barplot(m[2:3, ] , beside=TRUE, names.arg=m[1, ], 
        col=2:3, las=1, xlab="Year")
legend("topleft", legend=c("Balance", "TotalDeposits"), fill=2:3, box.col=NA)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM