【问题标题】:how to create grouped barchat with our ggplot in r如何在 r 中使用我们的 ggplot 创建分组条形图
【发布时间】:2013-12-31 10:20:25
【问题描述】:
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

我有矩阵

我想创建一个分组条形图,其中 x 轴将年份作为分类变量。 条形代表相应年份的余额和存款总额。

请推荐基本的 barchat 函数,而不是 ggplot 或 lattice 函数。

我正在从 .csv 文件中读取数据。

到目前为止我已经尝试过什么

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)

【问题讨论】:

  • @agstudy 我已经用这个问题更新了我的代码。

标签: r bar-chart


【解决方案1】:

例如:

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

d 是:

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)

【讨论】:

  • 它的工作谢谢。但是你能解释一下三行代码的神奇之处吗?我是 R 新手。
【解决方案2】:

我不确定这是否正是你想要的......

# 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多