0

My code For a university course i have to plot the inferred excess mortality up until 2019 and as of 2020. However, when running my code it keeps saying i have an aes error: Error in geom_line(): ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (2296) ✖ Fix the following mappings: y

How could i solve this issue, i have tried the solutions shown on https://www.statology.org/aesthetics-must-be-either-length-1-or-same-as-data/ without any luck

Any help is appreciated! my code:

ggplot(data=preds[preds$YEAR<=2019,], aes(x=as.numeric(as.character(WEEK)), y=1E7*preds$excess_deaths/POPULATION), colour = factor(YEAR)) +
  geom_line(aes(y=1E7*preds$excess_deaths/POPULATION, colour = NA)) +
  geom_ribbon(data=preds[preds$YEAR<=2019,], aes(ymin=1E7*preds$lower.CI/POPULATION, ymax=1E7*preds$upper.CI/POPULATION), colour=NA, alpha=I(0.4)) + 
  geom_ribbon(aes(ymin=1E7*preds$excess_deaths.lower.PI/POPULATION, ymax=1E7*preds$excess_deaths.upper.PI/POPULATION), colour=NA, alpha=I(0.2)) + 
  geom_line(data=preds[preds$YEAR>=2020,], aes(y=1E7*preds$excess_deaths/POPULATION, colour = NA)) +
  geom_ribbon(data=preds[preds$YEAR>=2020,], 
              aes(ymin=1E7*preds$excess_deaths.lower.CI/POPULATION, ymax=1E7*preds$excess_deaths.upper.CI/POPULATION), colour=NA, alpha=I(0.4)) + 
  geom_ribbon(data=preds[preds$YEAR>=2020,], 
              aes(ymin=1E7*preds$excess_deaths.lower.PI/POPULATION, ymax=1E7*preds$excess_deaths.upper.PI/POPULATION), colour=NA, alpha=I(0.2)) + 
  facet_wrap(~ model, ncol = 1) +
  xlab("Date") +
  ylab("Excess mortality (Per 10M inhabitants") +
  ggtitle(label = 'EXCESS MORTALITY IN BELGIUM PER WEEK (PER 10M INHABITANTS)', subtitle = 'Data Statbel 2009-2022') +
  theme_economist_white()

Trying to change the color and fill functions of aes according to multiple sites without any luck. Expecting the color to resemble each year. when running i received this error: Error in geom_line(): ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error in check_aesthetics(): ! Aesthetics must be either length 1 or the same as the data (2296) ✖ Fix the following mappings: y

  • 1
    genenrally try to do the calcualtions and filtering before the plotting, so you know whats the problem with your code(and you can reuse code more easily) – user12256545 Nov 20 '22 at 15:21
  • 1
    There are errors in the code and it is difficult to make heads or tails without seeing a sample of the data. I am sure there is an easier way to do this. First is step is to remove "preds$" from all of the `aes()` definitions. When one uses "preds$" you are referencing back to the original data frame and not from "data=". I suggest simplify this code block down to 1 year and getting that to work before attempting multiple years. – Dave2e Nov 20 '22 at 15:25

0 Answers0