简体   繁体   English

我在这个情节里做错了什么?

[英]What am I doing wrong in this plot?

I'm trying to plot geom_area as below. 我正在尝试绘制geom_area,如下所示。

natural_prods %>% 
  ggplot(., aes(x = WEEK_NUM, y = SPEND, fill = HSHD_COMPOSITION)) +
  geom_area()

But my output looks like this. 但是我的输出看起来像这样。 Can anyone help me understand why this could be happening? 谁能帮助我了解为什么会这样吗? I'm trying to see the pattern in which households of different size(HSHD_COMPOSITION) spend over time(WEEK_NUM). 我正在尝试查看不同规模(HSHD_COMPOSITION)的家庭随时间(WEEK_NUM)花费的模式。

Sample Data 样本数据

 SPEND  HSHD_COMPOSITION    WEEK_NUM
1.99    Single Female       1
4.79    2 Adults and Kids   1
1.98    1 Adult and Kids    2
7.73    NA                  1
2.15    1 Adult and Kids    3
3.99    2 Adults and Kids   1
1.57    1 Adult and Kids    3
8                      NA   4
5.99    2 Adults and Kids   5
4.99    2 Adults and Kids   4
5.16    Single Female       6
1.39    Single Male         7
1.5            NA           3
0.79    2 Adults and Kids   2
0.99    Single Male         6
1.99    2 Adults            4
2.5           NA            5
3.2    2 Adults and Kids    7
1       1 Adult and Kids    8

sample data R: 样本数据R:

structure(list(BASKET_NUM = c("003705", "004612", "009086", "009829", 
"010003", "015787", "019087", "021615", "021712", "023202", "027586", 
"029586", "032961", "033600", "037672", "038300", "048690", "052045", 
"052218", "053068"), SPEND = c(1.75, 11.56, 1.99, 2.79, 4.79, 
6.29, 1.98, 1.49, 14.98, 5.97, 5, 4.99, 7.73, 2.15, 3.99, 1.57, 
8, 5.99, 4.99, 4.99), HSHD_COMPOSITION = structure(c(3L, 3L, 
5L, 1L, 4L, 3L, 2L, 3L, 2L, NA, 3L, 6L, NA, 2L, 4L, 2L, NA, 4L, 
4L, 4L), .Label = c("1 Adult", "1 Adult and Kids", "2 Adults", 
"2 Adults and Kids", "Single Female", "Single Male"), class = "factor"), 
    WEEK_NUM = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 4L, 4L, 5L, 4L, 5L, 6L, 6L, 6L), COMMODITY = c("PRODUCE", 
    "GROCERY STAPLE", "GROCERY STAPLE", "DAIRY", "FROZEN FOOD", 
    "DAIRY", "PRODUCE", "PRODUCE", "MEAT - BEEF", "GROCERY STAPLE", 
    "DAIRY", "PRODUCE", "MEAT - CHICKEN", "PRODUCE", "FROZEN FOOD", 
    "PRODUCE", "DAIRY", "GROCERY STAPLE", "PRODUCE", "MEAT - PORK"
    )), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))

在此处输入图片说明

I need my output to look like this 我需要我的输出看起来像这样

在此处输入图片说明

Since your data set is very small, I went for a stacked barplot with width = 1 由于您的数据集非常小,因此我选择了width = 1的堆积条形图

library( ggplot2 )
natural_prods %>% 
  filter( !is.na( HSHD_COMPOSITION ) ) %>%
  group_by( WEEK_NUM, HSHD_COMPOSITION ) %>%
  summarise( total = sum( SPEND ) ) %>%
  ggplot( aes( x = WEEK_NUM, y = total, colour = HSHD_COMPOSITION, fill = HSHD_COMPOSITION ) ) +
  geom_col( width = 1, position = "stack" )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM