直接在ggplot2中创建比例geom_area图 [英] Create proportional geom_area plot directly in ggplot2

查看:135
本文介绍了直接在ggplot2中创建比例geom_area图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个比例geom_area图。我认为一定有可能直接在ggplot2中执行此操作,而不是事先计算总数和比例。
我创建了一个最小的工作示例,显示了我现在所得到的。

I would like to create a proportional geom_area plot. I thought there must be the possibility to directly do this in ggplot2 instead of calculating the totals and the proportions beforehand. I created a minimal working example, which shows what I got now.

library(data.table)
library(dplyr)
library(zoo)
dt <- structure(list(Date = structure(c(2000, 2000, 2000, 2000, 2000, 
                                  2000.25, 2000.25, 2000.25, 2000.25, 2000.25, 2000.5, 2000.5, 
                                  2000.5, 2000.5, 2000.5, 2000.75, 2000.75, 2000.75, 2000.75, 2000.75, 
                                  2001, 2001, 2001, 2001, 2001, 2001.25, 2001.25, 2001.25, 2001.25, 
                                  2001.25, 2001.5, 2001.5, 2001.5, 2001.5, 2001.5, 2001.75, 2001.75, 
                                  2001.75, 2001.75, 2001.75, 2002, 2002, 2002, 2002, 2002, 2002.25, 
                                  2002.25, 2002.25, 2002.25, 2002.25, 2002.5, 2002.5, 2002.5, 2002.5, 
                                  2002.5, 2002.75, 2002.75, 2002.75, 2002.75, 2002.75), class = "yearqtr"), 
               Category = c(2L, NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L, 
                            NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L, 
                            NA, 1L, 4L, 3L, 2L, 1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L, 
                            1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L, 
                            1L, 4L, NA, 3L), Value = c(51, 15, 17, 3, 37, 50, 16, 17, 
                                                       3, 47, 49, 16, 17, 3, 37, 53, 16, 17, 2, 38, 57, 2, 16, 2, 
                                                       39, 58, 2, 16, 2, 39, 59, 17, 2, 2, 38, 59, 16, 3, 2, 37, 
                                                       58, 17, 3, 3, 35, 58, 17, 3, 3, 36, 56, 17, 3, 3, 36, 57, 
                                                       17, 3, 3, 37))
,.Names = c("Date", "Category", "Value")
, class = c("data.table", "data.frame"), row.names = c(NA, -60L))
data.table::melt(dt, id.vars = c("Date", "Category")
, measure.vars = c("Value")
)  %>%  ggplot(data = ., aes(x = Date, y = value, fill = as.factor(Category))) +
    geom_area(stat = "identity") +
    theme(legend.title=element_blank()) + 
    scale_x_yearqtr(format = "%Y-Q%q",n = 8, expand = c(0,0))


我想将所有内容缩放到0到1的空间,并使其完全填满。

I would like to scale everything into the 0 to 1 space and let it fill it up completely.

推荐答案

如果我对您的理解正确,那么您所要做的就是将 position = fill 添加到 geom_area()

If I understand you correctly, all you need is to add position = "fill" to geom_area():

data.table::melt(dt, id.vars = c("Date", "Category")
                 , measure.vars = c("Value")
)  %>%  ggplot(data = ., aes(x = Date, y = value, fill = as.factor(Category))) +
  geom_area(stat = "identity", position = "fill") +
  theme(legend.title=element_blank()) + 
  scale_x_yearqtr(format = "%Y-Q%q",n = 8, expand = c(0,0))

结果:

这篇关于直接在ggplot2中创建比例geom_area图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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