如何在R中创建(100%)堆叠直方图? [英] How can I create a (100%) stacked histogram in R?

查看:254
本文介绍了如何在R中创建(100%)堆叠直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下格式的数据(此处是从CSV文件导入).您可以在CSV 此处找到示例数据集.

I have data in the following format (here, imported from a CSV file). You can find an example dataset as CSV here.

PAIR   PREFERENCE
1      5
1      3
1      2
2      4
2      1
2      3

…等等.总共有19对,PREFERENCE作为离散值,范围从15.

… and so on. In total, there are 19 pairs, and the PREFERENCE ranges from 1 to 5, as discrete values.

我需要一个堆积的直方图,例如每对都有100%高的列,指示PREFERENCE值的分布.

What I need is a stacked histogram, e.g. a 100% high column, for each pair, indicating the distribution of the PREFERENCE values.

类似于Excel中的"100%堆积的列",或者(尽管不尽相同,但称为马赛克图"):

Something similar to the "100% stacked columns" in Excel, or (although not quite the same, a so-called "mosaic plot"):

我认为使用ggplot2最简单,但是我什至不知道从哪里开始.我知道我可以使用以下内容创建一个简单的条形图:

I figured it'd be easiest using ggplot2, but I don't even know where to start. I know I can create a simple bar chart with something like:

ggplot(d, aes(x=factor(PAIR), y=factor(PREFERENCE))) + geom_bar(position="fill")

...但是那并不能使我走的太远.所以我尝试了一下,这使我更接近要实现的目标,但是我想它仍然使用PREFERENCE的计数?请注意,ylab在此处为计数",其值范围为19.

… that however doesn't get me very far. So I tried this, and it gets me somewhat closer to what I'm trying to achieve, but it still uses the count of PREFERENCE, I suppose? Note the ylab being "count" here, and the values ranging to 19.

qplot(factor(PAIR), data=d, geom="bar", fill=factor(PREFERENCE_FIXED))

结果:

  • 那么,我该怎么做才能使堆积的条形图代表直方图?
  • 还是他们实际上已经这样做了?
  • 如果是这样,我该如何更改才能使标签正确(例如,以百分比代替计数")?

顺便说一句,这与这个问题并没有真正的关系,而仅与(即可能是相同的想法,但不是连续的值,而是分成小节). . sub>

By the way, this is not really related to this question, and only marginally related to this (i.e. probably same idea, but not continuous values, instead grouped into bars).

推荐答案

也许您想要这样的东西:

Maybe you want something like this:

ggplot() + 
    geom_bar(data = dat,
             aes(x = factor(PAIR),fill = factor(PREFERENCE)),
             position = "fill")

我已将您的数据读入dat.输出如下:

where I've read your data into dat. This outputs something like this:

y标签仍然是"count",但是您可以通过添加以下内容来手动更改:

The y label is still "count", but you can change that manually by adding:

+ scale_x_discrete("Pairs") + scale_y_continuous("Votes")

这篇关于如何在R中创建(100%)堆叠直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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