Home > database >  How to get geom_area() to not be stacked
How to get geom_area() to not be stacked

Time:10-20

I would like to have the two areas in my area plot to not be stacked.

At the minute I am subtracting the blue area from the red area to get the area you see here. Although, because the blue points are greater than the red points, the whole thing falls apart so I am also using max(value, 0) to prevent negative values.

Chart

How can I get it so that my area chart allows the blue to simply sit on top of the red, so that I can just have the red as a straight line along the top.

I can't just add a box because this is the percentage chart, but I need the same for the actual values as well.

ggplot(progress_targets_total, aes(x=wave, y=perc, fill=group))   geom_area()

Here is my code. Very basic. I assume there is some aesthetic term I can just throw into the geom_area() part but the google is not helping today.

Cheers

Some data

    wave value      perc  group
1    1   893 95.101171 Actual
2    1    46  4.898829 Target
3    2   796 90.351873 Actual
4    2    85  9.648127 Target
5    3   694 85.679012 Actual
6    3   116 14.320988 Target

structure(list(wave = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 
6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L), 
    value = c(893, 46, 796, 85, 694, 116, 858, 86, 812, 61, 643, 
    157, 746, 190, 416, 454, 10, 813, 0, 914, 0, 866, 0, 794), 
    perc = c(95.1011714589989, 4.89882854100107, 90.3518728717367, 
    9.64812712826334, 85.679012345679, 14.320987654321, 90.8898305084746, 
    9.11016949152542, 93.0126002290951, 6.98739977090493, 80.375, 
    19.625, 79.7008547008547, 20.2991452991453, 47.816091954023, 
    52.183908045977, 1.21506682867558, 98.7849331713244, 0, 100, 
    0, 100, 0, 100), group = structure(c(2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L), .Label = c("Target", "Actual"), class = "factor")), row.names = c(NA, 
-24L), class = "data.frame")

CodePudding user response:

You could use position = 'identity' to unstack the areas:

library(ggplot2)
ggplot(progress_targets_total, aes(x=wave, y=value, fill= group))   
  geom_area(alpha = 0.4, position = 'identity')

Created on 2022-10-19 with reprex v2.0.2

  • Related