2016-10-19 2 views
0

j'ai données qui ressemble à ceci:données lisses pour un graphique geom_area

df <- tibble(
    x = 1:20, 
    y = c(4, 4, 2, 1, 8, 3, 4, 2, 8, 2, 2, 2, 2, 6, 1, 7, 8, 9, 9, 2) 
) 

Graphed, il ressemble à ceci:

df %>% 
    ggplot(aes(x, y)) + 
    geom_area() 

enter image description here

Mais le graphique est vraiment discordante. Comment pourrais-je lisser les bords dentelés à quelque chose comme ceci:

enter image description here

Merci!

+4

'df%>% ggplot (aes (x, y)) + stat_smooth (geom = 'area') ' – Axeman

+0

Ajuster l'esprit h 'span'. Aimer. Merci! – emehex

Répondre

1

Expansion du commentaire d'Axeman: smooth peut être contrôlé par le paramètre span.

Exemple 1:

df %>% 
    ggplot(aes(x, y)) + 
    # original, delete if desired 
    geom_area(alpha = 1/2) + 
    stat_smooth(
     geom = 'area', method = 'loess', span = 1/3, 
     alpha = 1/2, fill = "red") + 
    labs(title = "Smooth with `span = 1/3`") 

enter image description here

Exemple 2:

df %>% 
    ggplot(aes(x, y)) + 
    geom_area(alpha = 1/2) + 
    # original, delete if desired 
    stat_smooth(
     geom = 'area', method = 'loess', span = 7/10, 
     alpha = 1/2, fill = "blue") + 
    labs(title = "Smooth with `span = 7/10`") 

enter image description here