geom_area produces blank areas between layersStacked geom_area produces blank between layersbootstrap ICC in RDifferent colors in ggplot2 using groupsConverting multiple year ranges into a list of years in R'Min' not meaningful for factors in encounter history data, R Studiounexpected behavior of for loopR Code for transforming integers to dateError while doing Linear Regression in RSorting table output chronologicallyCan't do the barplot in rCreating a dual y-axis one on top of each other

Dear Fellow PSE Users,

Escape the labyrinth!

Can one guy with a duplicator initiate a nuclear apocalypse?

Did HaShem ever command a Navi (Prophet) to break a law?

What can I actually do with a high credit score?

Is there any actual security benefit to restricting foreign IPs?

What was the deeper meaning of Hermione wanting the cloak?

Prove that the area of the trangles are equal.

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

All numbers in a 5x5 Minesweeper grid

What to do as a player when ranger animal companion dies

Why is the stock market so unpredictable?

I was cheated into a job and want to leave ASAP, what do I tell my interviewers?

As a discovery writer, how to complete unfinished novel (which is highly diverted from original plot ) after a time-gap

Does battery condition have anything to do with macbook pro performance?

Is it safe to unplug a blinking USB drive after 'safely' ejecting it?

Lead Amalgam as a Material for a Sword

Removing rows containing NA in every column

Make Interviewee Comfortable in Potentially Intimate Environment

Which museums have artworks of all four ninja turtles' namesakes?

Simulate a 1D Game-of-Life-ish Model

Tips for remembering the order of parameters for ln?

Regular Expressions with `<` and `?` strange matches

Is it possible to get a pointer to one subobject via a pointer to a different, unreleated subobject?



geom_area produces blank areas between layers


Stacked geom_area produces blank between layersbootstrap ICC in RDifferent colors in ggplot2 using groupsConverting multiple year ranges into a list of years in R'Min' not meaningful for factors in encounter history data, R Studiounexpected behavior of for loopR Code for transforming integers to dateError while doing Linear Regression in RSorting table output chronologicallyCan't do the barplot in rCreating a dual y-axis one on top of each other






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have the following data in data.frame tbl:



library(tibble)

tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))


I wanted to create a layered (stacked) area chart, using the following code:



ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")


But the result I'm getting has strange blank gaps in some places between the layers:



area chart



I found this question but that doesn't seem to be the issue I'm having - there's no negative numbers in my data.



Any ideas how to fix this?



Here's what seems to be going here: New Types are added and old ones removed from the full list at different times. When that happens, the area for these starts with a straight vertical line, but the area for the Type above this new one does diagonally from 0 to the new starting point for this layer. That doesn't sound like a reasonable behaviour to me. How do I get ggplot to plot all the colours one on top of the other rather than trying to create ribbons? I thought that was the whole point of geom_area...










share|improve this question


























  • Could you check your data sample code - it gives errors

    – Oka
    Mar 28 at 13:21











  • Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

    – iod
    Mar 28 at 13:24











  • Yep, tibble fixed the issue

    – Oka
    Mar 28 at 13:31


















1















I have the following data in data.frame tbl:



library(tibble)

tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))


I wanted to create a layered (stacked) area chart, using the following code:



ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")


But the result I'm getting has strange blank gaps in some places between the layers:



area chart



I found this question but that doesn't seem to be the issue I'm having - there's no negative numbers in my data.



Any ideas how to fix this?



Here's what seems to be going here: New Types are added and old ones removed from the full list at different times. When that happens, the area for these starts with a straight vertical line, but the area for the Type above this new one does diagonally from 0 to the new starting point for this layer. That doesn't sound like a reasonable behaviour to me. How do I get ggplot to plot all the colours one on top of the other rather than trying to create ribbons? I thought that was the whole point of geom_area...










share|improve this question


























  • Could you check your data sample code - it gives errors

    – Oka
    Mar 28 at 13:21











  • Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

    – iod
    Mar 28 at 13:24











  • Yep, tibble fixed the issue

    – Oka
    Mar 28 at 13:31














1












1








1








I have the following data in data.frame tbl:



library(tibble)

tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))


I wanted to create a layered (stacked) area chart, using the following code:



ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")


But the result I'm getting has strange blank gaps in some places between the layers:



area chart



I found this question but that doesn't seem to be the issue I'm having - there's no negative numbers in my data.



Any ideas how to fix this?



Here's what seems to be going here: New Types are added and old ones removed from the full list at different times. When that happens, the area for these starts with a straight vertical line, but the area for the Type above this new one does diagonally from 0 to the new starting point for this layer. That doesn't sound like a reasonable behaviour to me. How do I get ggplot to plot all the colours one on top of the other rather than trying to create ribbons? I thought that was the whole point of geom_area...










share|improve this question
















I have the following data in data.frame tbl:



library(tibble)

tbl <- structure(list(
year = c(2007, 2008, 2008, 2009, 2009, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, NA),
Type = c("RLR", "PLR", "RLR", "PLR", "RLR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "Supplement", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "PLR", "RLR", "RR", "LR", "PLR", "RLR", "RR", "LR", "Other", "PLR", "RR", "LR", "Other", "RR", "LR", "Other", "RR", "RR"),
`n()` = c(4L, 2L, 35L, 14L, 41L, 34L, 37L, 61L, 40L, 21L, 149L, 1L, 38L, 17L, 134L, 41L, 4L, 115L, 23L, 3L, 76L, 1L, 27L, 2L, 78L, 28L, 2L, 4L, 36L, 33L, 5L, 32L, 22L, 3L, 36L, 5L)),
row.names = c(NA, -36L),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
groups = structure(list(
year = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, NA),
.rows = list(1L, 2:3, 4:5, 6:8, 9:12, 13:15, 16:18, 19:21, 22:25, 26:29, 30:32, 33:35, 36L)),
row.names = c(NA, -13L),
class = c("tbl_df", "tbl", "data.frame"),
.drop = TRUE))


I wanted to create a layered (stacked) area chart, using the following code:



ggplot(tbl,aes(x=year,y=`n()`,fill=Type)) +
geom_area(position="stack") +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")


But the result I'm getting has strange blank gaps in some places between the layers:



area chart



I found this question but that doesn't seem to be the issue I'm having - there's no negative numbers in my data.



Any ideas how to fix this?



Here's what seems to be going here: New Types are added and old ones removed from the full list at different times. When that happens, the area for these starts with a straight vertical line, but the area for the Type above this new one does diagonally from 0 to the new starting point for this layer. That doesn't sound like a reasonable behaviour to me. How do I get ggplot to plot all the colours one on top of the other rather than trying to create ribbons? I thought that was the whole point of geom_area...







r ggplot2 geom-area






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 14:21









Oka

8282 silver badges9 bronze badges




8282 silver badges9 bronze badges










asked Mar 28 at 13:08









iodiod

5,1172 gold badges7 silver badges26 bronze badges




5,1172 gold badges7 silver badges26 bronze badges















  • Could you check your data sample code - it gives errors

    – Oka
    Mar 28 at 13:21











  • Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

    – iod
    Mar 28 at 13:24











  • Yep, tibble fixed the issue

    – Oka
    Mar 28 at 13:31


















  • Could you check your data sample code - it gives errors

    – Oka
    Mar 28 at 13:21











  • Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

    – iod
    Mar 28 at 13:24











  • Yep, tibble fixed the issue

    – Oka
    Mar 28 at 13:31

















Could you check your data sample code - it gives errors

– Oka
Mar 28 at 13:21





Could you check your data sample code - it gives errors

– Oka
Mar 28 at 13:21













Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

– iod
Mar 28 at 13:24





Works fine for me - you might need to have the tibble package loaded (e.g., through dplyr).

– iod
Mar 28 at 13:24













Yep, tibble fixed the issue

– Oka
Mar 28 at 13:31






Yep, tibble fixed the issue

– Oka
Mar 28 at 13:31













1 Answer
1






active

oldest

votes


















3
















There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.



library(dplyr) # for %>% operator
library(tidyr) # for drop_na function

# Your data here....

tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))

ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")




Created on 2019-03-28 by the reprex package (v0.2.1)






share|improve this answer



























  • Brilliant! Thanks.

    – iod
    Mar 28 at 16:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398452%2fgeom-area-produces-blank-areas-between-layers%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3
















There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.



library(dplyr) # for %>% operator
library(tidyr) # for drop_na function

# Your data here....

tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))

ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")




Created on 2019-03-28 by the reprex package (v0.2.1)






share|improve this answer



























  • Brilliant! Thanks.

    – iod
    Mar 28 at 16:05















3
















There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.



library(dplyr) # for %>% operator
library(tidyr) # for drop_na function

# Your data here....

tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))

ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")




Created on 2019-03-28 by the reprex package (v0.2.1)






share|improve this answer



























  • Brilliant! Thanks.

    – iod
    Mar 28 at 16:05













3














3










3









There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.



library(dplyr) # for %>% operator
library(tidyr) # for drop_na function

# Your data here....

tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))

ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")




Created on 2019-03-28 by the reprex package (v0.2.1)






share|improve this answer















There are some (year, Type) combinations that are missing (implicit zeros). If you add them (explicit zeros), there are no empty areas.



library(dplyr) # for %>% operator
library(tidyr) # for drop_na function

# Your data here....

tbl <- tbl %>%
# Ungroup or `complete` won't work as expected
ungroup() %>%
# There is one NA year
drop_na() %>%
# Add all (Type, year) combinations, filling in with 0s where `n()` is not observed
complete(Type, year, fill = list(`n()` = 0))

ggplot(tbl, aes(x=year,y=`n()`, group=Type, fill=Type)) +
geom_area(alpha = 0.5) +
theme_light() +
scale_colour_brewer(type="qual",palette = 1) +
ylab("Count")




Created on 2019-03-28 by the reprex package (v0.2.1)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 14:24

























answered Mar 28 at 13:22









dipetkovdipetkov

1,4961 silver badge8 bronze badges




1,4961 silver badge8 bronze badges















  • Brilliant! Thanks.

    – iod
    Mar 28 at 16:05

















  • Brilliant! Thanks.

    – iod
    Mar 28 at 16:05
















Brilliant! Thanks.

– iod
Mar 28 at 16:05





Brilliant! Thanks.

– iod
Mar 28 at 16:05








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55398452%2fgeom-area-produces-blank-areas-between-layers%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript