如何在 R 中创建 (100%) 堆叠直方图?

发布于 2024-12-25 03:54:55 字数 1378 浏览 0 评论 0原文

我的数据集:

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

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

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


我想要实现的目标:

我需要的是每对的堆叠直方图,例如 100% 高的列,指示 PREFERENCE 值的分布。

类似于 Excel 中的“100% 堆叠列”,或者(虽然不完全相同,所谓的“马赛克图”):


我尝试过的:

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

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

……但这并不能让我走得太远。所以我尝试了这个,它让我更接近我想要实现的目标,但我想它仍然使用 PREFERENCE 的计数?请注意,此处的 ylab 为“count”,值范围为 19。

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

结果为:

enter image此处的描述

  • 那么,我需要做什么才能让堆积条形图表示直方图?
  • 或者他们实际上已经这样做了吗?
  • 如果是这样,我需要更改什么才能使标签正确(例如使用百分比而不是“计数”)?

顺便说一句,这与这个问题并没有真正的关系,与这个(即可能相同的想法,但不是连续值,而是分组为条形)。

My dataset:

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

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


What I'm trying to achieve:

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

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


What I tried:

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

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

Results in:

enter image description here

  • So, what do I have to do to get the stacked bars to represent a histogram?
  • Or do they actually do this already?
  • If so, what do I have to change to get the labels right (e.g. have percentages instead of the "count")?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-01-01 03:54:55

也许您想要这样的东西:

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

我将您的数据读入dat。这会输出类似这样的内容:

在此处输入图像描述

y 标签仍然是“count”,但您可以通过以下方式手动更改:添加:

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

Maybe you want something like this:

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

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

enter image description here

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

+ scale_x_discrete("Pairs") + scale_y_continuous("Votes")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文