LaVOZs

The World’s Largest Online Community for Developers

'; r - Error in f(...) : Aesthetics can not vary with a ribbon - LavOzs.Com

This is the head of my data:

head(datos)

#se acomodan las fechas
datos$Fecha <- factor(datos$Fecha,
                      levels = c("I 2017", "II 2017", "III 2017", "IV 2017", "I 2018", "II 2018", "III 2018", "IV 2018", "I 2019", "II 2019", "III 2019" ))

#se ordenan los niveles y se da etiquetas a cada uno
datos$Sexo <- factor(datos$Sexo,
                     levels = c(0,1),
                     labels = c("Mujeres", "Hombres"))

  ggplot(datos, aes(Fecha, TI, group = Sexo, colour = Sexo, fill=Sexo)) +
    #se colocan los numeros que representan los puntos
    geom_text(aes(label = TI), vjust = -0.3, size = 3, nudge_x = 0, nudge_y = 1) +
    theme_minimal() +
    # se establece la linea que grafica los datos
    geom_line(size=0.5) +
    # se agregan marcadores y se define el tamaño
    geom_point(size=1) +
    ggtitle("Tasa de informalidad, según condición de la actividad por sexo", subtitle = "Periodo: Enero 2017 - Septiembre 2019") +
    # se eliminan los titulos de los ejes
    theme(axis.title = element_blank()) +
    # se define el alto del eje Y
    scale_y_continuous(expand = c(0,0), breaks = seq(0,100,10), limits = c(0,100)) +
    #Se rotan las etiquetas del eje x
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    theme(axis.text.x = element_text(size=10)) +
    geom_area(alpha = .2, stat = "identity")

This is my script. I am trying to make a linear graph and I want to add to the plot a marked area with geom_area. But the thing is, that every time I try to do it, this error appears:

Error in f(...) : Aesthetics can not vary with a ribbon

enter image description here enter image description here

Edit (2/23/2020):

For geom_area try instead:

geom_area(alpha = .2, position = 'identity')

Here is the entire code with your data:

library(ggplot2)
library(tidyverse)

datos <- read.table(
  text =
"N Fecha    TI  Sexo
III 2019    59.6    1
II 2019 58.6    1
I 2019  58.1    1
IV 2018 57.9    1
III 2018    58.2    1
II 2018 59.8    1
I 2018  62.1    1
IV 2017     63.1    1
III 2017    61.3    1
II 2017 60.5    1
I 2017  61.8    1
III 2019    40.4    0
II 2019 41.4    0
I 2019  41.9    0
IV 2018 42.1    0
III 2018    41.8    0
II 2018 40.2    0
I 2018  37.9    0
IV 2017     36.9    0
III 2017    38.7    0
II 2017 39.5    0
I 2017  38.2    0", header = T, stringsAsFactors = F) %>%
  unite(Fecha, N, Fecha, remove = TRUE, sep = " ")

#se acomodan las fechas
datos$Fecha <- factor(datos$Fecha,
                      levels = c("I 2017", "II 2017", "III 2017", "IV 2017", "I 2018", "II 2018", "III 2018", "IV 2018", "I 2019", "II 2019", "III 2019" ))

#se ordenan los niveles y se da etiquetas a cada uno
datos$Sexo <- factor(datos$Sexo,
                     levels = c(0,1),
                     labels = c("Mujeres", "Hombres"))

ggplot(datos, aes(Fecha, TI, group = Sexo, colour = Sexo, fill = Sexo)) +
  #se colocan los numeros que representan los puntos
  geom_text(aes(label = TI), vjust = -0.3, size = 3, nudge_x = 0, nudge_y = 1) +
  theme_minimal() +
  # se establece la linea que grafica los datos
  geom_line(size=0.5) +
  # se agregan marcadores y se define el tamaño
  geom_point(size=1) +
  ggtitle("Tasa de informalidad, según condición de la actividad por sexo", subtitle = "Periodo: Enero 2017 - Septiembre 2019") +
  # se eliminan los titulos de los ejes
  theme(axis.title = element_blank()) +
  # se define el alto del eje Y
  scale_y_continuous(expand = c(0,0), breaks = seq(0,100,10), limits = c(0,100)) +
  #Se rotan las etiquetas del eje x
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(axis.text.x = element_text(size=10)) +
  geom_area(alpha = .2, position = 'identity')

Plot

plot with geom_area

Related
Is there a table or catalog of aesthetics for ggplot2?
How can we make xkcd style graphs?
Remove 'a' from legend when using aesthetics and geom_text
data.table vs dplyr: can one do something well the other can't or does poorly?
ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
ggplot2: can a non-aesthetic parameter vary by factor?
R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph
How to vary line and ribbon colours in a facet_grid