在极坐标图上堆积的条形图

时间:2017-03-01 17:38:54

标签: r ggplot2

我有一个看起来像这样的数据框(这是一个更大的数据帧的样本):

  dvmph variable    value
1      1       X0    100.0
2      3       X0   2486.6
3      5       X0 100519.3
4      7       X0 471515.0
5      1       X1 973180.2
6      3       X1 758789.6
7      5       X1 500884.34
8      7       X1 441252.43
9      9       X1 228094.07
10     1       X2 358144.00
11     3       X2 173614.35
12     5       X2  73395.79
13     7       X2  79245.32
14     9       X2  59789.64
15     1       X3  35539.05
16     3       X3  23196.88
17     5       X3  15686.76
18     7       X3  10589.20
19     9       X3  11124.05
20     1       X4   5221.25
21     3       X4   5630.40
22     5       X4   4045.34
23     7       X4  13108.06
24     9       X4    302.23

我想得到一个像这样的堆积条形图(轴将是不同的,因为上面的数据只是一个样本):

enter image description here

这是我目前的代码:

ggplot(data, aes(variable, dvmph, fill=value)) + geom_bar(stat='identity', position='stack')

我不在乎色差,但是y轴非常扭曲,我不确定我做错了什么:

enter image description here

这个数据的最终目标是绘制极轴上的所有条形图,其中dvmph为r,变量为theta(对于可怕的绘图道歉,我希望这是有道理的):

我的两个问题是:

  1. 为什么我的y轴如此扭曲?
  2. 有没有一种简单的方法可以在极地情节上绘制这些条形图?

2 个答案:

答案 0 :(得分:2)

从你的绘图中,我相信你将错误的值传递给aes() - 也就是说,对于你的堆叠条例子,我相信你想要这个:

ggplot(df, aes(x = variable, y = value, fill = factor(dvmph))) +
  geom_col()

Plot

然后您可以添加coord_polar()以获取以下内容:

ggplot(df, aes(x = variable, y = value, fill = factor(dvmph))) +
  geom_col() +
  coord_polar()

Plot Polar

如果您不喜欢科学记数法,可以随时添加:+ scale_y_continuous(labels = scales::comma_format())

答案 1 :(得分:1)

您有两个预测变量variabledvmph,以及一个响应变量value,它是一个频率。因此,您在x-y图中绘制预测变量的方法,或者作为具有角度和半径的极坐标图,并且显示颜色编码的响应变量是合理的。不幸的是,你选择了错误的几何体。

请尝试geom_tile()而不是geom_bar()

library(ggplot2)
ggplot(DT, aes(variable, factor(dvmph), fill=value)) + geom_tile()

enter image description here

或在极坐标中:

ggplot(DT, aes(variable, factor(dvmph), fill=value)) + geom_tile() +
  coord_polar() + theme_linedraw()

enter image description here