堆积面积图Y轴未反映实际数据点 [英] Stacked Area Plot Y-axis not reflecting actual data points
问题描述
我一直在尝试使用ggplot2绘制堆积面积图.我的问题是绘图的y轴无法准确显示 value 的范围.我最大的 value 值是100.
日期A B C D E2019-12-31 0.0 0.0 0.0 0.0 0.0 0.02019-12-24 0.0 0.0 0.0 0.0 0.0 0.0...2016-12-27 18.09 81.91 40.21 9.70 1.7...2014-01-28 92.11 7.89 0.00 0.00 0.00...2000-01-01 0.0 0.0 0.0 0.0 0.0
我将数据的格式从宽到长
long_data< -melt(data,measure.vars = c("A","B","C","D","E")),variable.name =强度")
绘制的 ggplot(long_data,aes(x =日期,y =值,填充=强度))+ geom_area()
我的情节看起来像这样
[堆积面积图] [1][1]:https://i.stack.imgur.com/i5ksF.png我已经在excel中完成了此操作,并且图形相同.我尝试为y轴添加一个限制,但这无济于事.但是,我注意到的一件事是,如果执行 geom_line()
,y轴将正确缩放.
geom_area的默认值为 position ="stacked"
,而您需要将其设置为 position ="identity"
.需要注意的是,您还需要使填充变量成为一个因数,将级别基于y值,否则某些值可能隐藏在更大的值后面.另一种选择是为geom_area设置 alpha = .3
没有数据集,我很难事先进行测试.尝试使用 dput(data)
并将输出复制并粘贴到下一个问题
库(tidyverse)图书馆(forcats)long_data%>%mutate(Intensity = fct_reorder(Intensity,value,.desc = TRUE))%&%;%ggplot(long_data,aes(x =日期,y =值,填充=强度))+geom_area(位置=身份")
I've been trying to plot a stacked area plot using ggplot2. My problem is that the y-axis of my plot is not accurately showing the range of value. My largest number for value is 100.
Date A B C D E
2019-12-31 0.0 0.0 0.0 0.0 0.0
2019-12-24 0.0 0.0 0.0 0.0 0.0
...
2016-12-27 18.09 81.91 40.21 9.70 1.7
...
2014-01-28 92.11 7.89 0.00 0.00 0.00
...
2000-01-01 0.0 0.0 0.0 0.0 0.0
I formated the data from wide to long
long_data <- melt(data,measure.vars = c("A","B","C","D","E"), variable.name = "Intensity")
the plottedggplot(long_data,aes(x = Date,y = value,fill = Intensity)) + geom_area()
My plot looked like this
[stacked area plot][1]
[1]: https://i.stack.imgur.com/i5ksF.png
I have done this in excel and the graph is the same. I've tried adding a limit to the y-axis but it doesn't help. But, one thing I noticed is that if I do geom_line()
the y-axis is scaled correctly.
The geom_area's default is position = "stacked"
while you need it to be position = "identity"
. The caveat here is you need to also make your fill variable a factor, basing the levels on the y value, otherwise some of the values may be hidden behind a bigger value. Another alternative is to set alpha = .3
for geom_area
Without a data set it is hard for me to test beforehand. Try using dput(data)
and copy and pasting the output into your next question
library(tidyverse)
library(forcats)
long_data %>%
mutate(Intensity = fct_reorder(Intensity, value, .desc = TRUE)) %>%
ggplot(long_data,aes(x = Date,
y = value,
fill = Intensity)) +
geom_area(position = "identity")
这篇关于堆积面积图Y轴未反映实际数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!