堆积的条形图 [英] Stacked barchart

查看:67
本文介绍了堆积的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何在R中制作堆积的条形图.

I'm having a really hard time trying to figure out how to make a stacked bar chart in R.

首先,我正在使用的数据来自VCD包

First of all, the data I'm working with is from the VCD package

data("Arthritis", package = "vcd")

现在,我只想绘制 Improved Treatment 列,显示两个条,它们是 Treatment 列的两个结果(安慰剂),并且 Improved 列的结果相互叠加.

Now, I only want to graph the columns Improved and Treatment, displaying two bars which are the two results of the Treatment column (Treated, Placebo) and have the results of the Improved column stacked on top of each other.

我尝试了一些命令,但是它一直给我一个错误:

I tried some commands but it kept giving me an error saying:

'height' must be a vector or a matrix

所以我做了以下事情,但是它为我提供了关节炎数据集中所有列的所有结果.

So I did the following, but it gave me all the results for all the columns in the Arthritis dataset.

arth <- as.matrix(Arthritis)
barplot(arth,
        main="Improvements in Treated vs Placebo",
        col=c("green","yellow", "blue"),
        xlab="Treatment",
        ylab="Frequency")

我不知道该怎么办.任何帮助或指导都将是惊人的.

I have no idea what to try anymore. Any help or guidance would be amazing.

推荐答案

您的方法存在问题.我想您想要这样的东西:

Couple of things wrong with your approach. I think you want something like this:

barplot(xtabs(~ Improved + Treatment, data = Arthritis))

这篇关于堆积的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆