Fitting a line to the mean values of a multilevel variable using geom_smooth

pyring

I have this dataframe.

I create a plot representing on the y-axis "value" and on the x-axis each of the levels of "Column_S", which contains levels from S1 to S10. All this grouped by "VS" and "Inst" (with a facet_grid) and "Estatus" with colour.

na.omit(data) %>% 
  group_by(VS,Estatus,Inst, Columna_S) %>% 
  summarise(media = mean(value), 
            desvio = sd(value),                             
            error_est = desvio / sqrt(n()),            
            intervalo_sup = media + (2*error_est),      
            intervalo_inf = media - (2*error_est)) -> stats

ggplot() +  
  geom_errorbar(
    data = stats,   
    aes(x = Columna_S,
        ymin = intervalo_inf,
        ymax = intervalo_sup,
        color = Estatus   
    ), width = 0.4 )+
  geom_point(data = stats,
             aes(x = Columna_S,
                 y = media,
             color = Estatus))+
  facet_grid(Inst ~ VS, scales = "free") +
  theme_classic()  

However, instead of an independent point for each mean, I'd like to use geom_smooth to plot a line (plus standard error) that fits the means along the x-axis.

I have tried the code below, but it is not generating the desired outcome.

na.omit(data) %>% 
  ggplot(aes(x=Columna_S,y=value,colour=Estatus, na.rm = TRUE) ) +
  geom_smooth(aes(fill=Estatus,na.rm = TRUE)) +
  scale_y_continuous(breaks = seq(0,100, by=25), limits=c(0,100))+
  facet_grid(Institution~Value_system, scales = "free")+
  theme_classic()  
StupidWolf

You have factors on the x-axis, I am not so sure it makes sense to put a smooth line through it, but if thats what you need, it goes like this:

data %>% 
filter(!is.na(Columna_S)) %>%
ggplot(aes(x=Columna_S,y=value,colour=Estatus)) +
geom_smooth(aes(group=Estatus)) +
facet_grid(Inst~VS, scales = "free")+
theme_classic() 

enter image description here

You can consider putting a line through all the means:

data %>% 
filter(!is.na(Columna_S)) %>%
ggplot(aes(x=Columna_S,y=value,colour=Estatus)) +
stat_summary(aes(group=Estatus),fun=mean,geom="line") +
facet_grid(Inst~VS, scales = "free")+
theme_classic()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fitting with ggplot2, geom_smooth and nls

From Dev

ggplot geom_smooth exclude negative values

From Dev

ggplot geom_smooth exclude negative values

From Dev

Is it possible to bypass stat_smooth when using geom_smooth?

From Dev

Plotting using geom_smooth or stat_smooth

From Dev

Plot dashed regression line with geom_smooth in ggplot2

From Dev

Less smoothed line in ggplot2, alternatives to geom_smooth?

From Dev

ggplot2 in R: fill underneath a geom_smooth line

From Dev

Displaying geom_smooth() trend line from a specified x value

From Dev

geom_smooth Match shaded area to line colour

From Dev

ggplot2 in R: fill underneath a geom_smooth line

From Dev

geom_smooth does not plot a line for my data frame

From Dev

Labeling values in ggplot outside geom_smooth threshold

From Dev

Why does geom_smooth() automatically excludes some values?

From Dev

Is there any function to plot similar to geom_smooth() in Plotly using python?

From Dev

Fitting a glm using variable as a column name in R

From Dev

How to add manual colors for a ggplot2 (geom_smooth/geom_line)

From Dev

geom-smooth: fitting the curve better to the points

From Dev

ggplot2: fit geom_smooth() like categorical variable were continuous

From Java

Mixed fill color in ggplot2 legend using geom_smooth() in R

From Dev

R : confidence interval being partially displayed with ggplot2 (using geom_smooth())

From Dev

Linetype and Guide options for ggplot2 when using geom_smooth with continuous variables

From Dev

what does a single variable line mean in javascript?

From Dev

R - Multilevel Variable into Dummies

From Dev

Find points over and under the confidence interval when using geom_stat / geom_smooth in ggplot2

From Dev

geom_line does not generate lines using fun.y = mean as an argument

From Dev

Visualizing longitudinal data with a trajectory/best-fitting mean growth curve and a spaghetti plot using R

From Dev

Fitting smooth through xyplot

From Dev

Fitting smooth through xyplot

Related Related

  1. 1

    Fitting with ggplot2, geom_smooth and nls

  2. 2

    ggplot geom_smooth exclude negative values

  3. 3

    ggplot geom_smooth exclude negative values

  4. 4

    Is it possible to bypass stat_smooth when using geom_smooth?

  5. 5

    Plotting using geom_smooth or stat_smooth

  6. 6

    Plot dashed regression line with geom_smooth in ggplot2

  7. 7

    Less smoothed line in ggplot2, alternatives to geom_smooth?

  8. 8

    ggplot2 in R: fill underneath a geom_smooth line

  9. 9

    Displaying geom_smooth() trend line from a specified x value

  10. 10

    geom_smooth Match shaded area to line colour

  11. 11

    ggplot2 in R: fill underneath a geom_smooth line

  12. 12

    geom_smooth does not plot a line for my data frame

  13. 13

    Labeling values in ggplot outside geom_smooth threshold

  14. 14

    Why does geom_smooth() automatically excludes some values?

  15. 15

    Is there any function to plot similar to geom_smooth() in Plotly using python?

  16. 16

    Fitting a glm using variable as a column name in R

  17. 17

    How to add manual colors for a ggplot2 (geom_smooth/geom_line)

  18. 18

    geom-smooth: fitting the curve better to the points

  19. 19

    ggplot2: fit geom_smooth() like categorical variable were continuous

  20. 20

    Mixed fill color in ggplot2 legend using geom_smooth() in R

  21. 21

    R : confidence interval being partially displayed with ggplot2 (using geom_smooth())

  22. 22

    Linetype and Guide options for ggplot2 when using geom_smooth with continuous variables

  23. 23

    what does a single variable line mean in javascript?

  24. 24

    R - Multilevel Variable into Dummies

  25. 25

    Find points over and under the confidence interval when using geom_stat / geom_smooth in ggplot2

  26. 26

    geom_line does not generate lines using fun.y = mean as an argument

  27. 27

    Visualizing longitudinal data with a trajectory/best-fitting mean growth curve and a spaghetti plot using R

  28. 28

    Fitting smooth through xyplot

  29. 29

    Fitting smooth through xyplot

HotTag

Archive