Home > Mobile >  Problems in plotting line and error bars on the plot in R
Problems in plotting line and error bars on the plot in R

Time:10-12

I have this piece of R code that should plot some data enter image description here

CodePudding user response:

I'm sorry - I was incorrect about "Day" being a factor - thanks for fixing the broken link.

One potential solution is to add a 'group' aesthetic, e.g.

library(tidyverse)

theme_set(theme_classic())

growth_data <- read.delim ("~/Desktop/growth_data.txt") %>% tibble()
#tidying the data.
growth_data_long <- growth_data %>% pivot_longer(-animal,
                                                 names_to=("Day"),
                                                 values_to=("Growth"))

growth2 <- growth_data_long %>%
  mutate(group = str_extract(animal, "\\w "))
growth2
#> # A tibble: 60 × 4
#>    animal    Day   Growth group  
#>    <chr>     <chr>  <dbl> <chr>  
#>  1 Control 1 Day.1   1.08 Control
#>  2 Control 1 Day.2   1.49 Control
#>  3 Control 1 Day.3   2.73 Control
#>  4 Control 1 Day.4   2.81 Control
#>  5 Control 1 Day.5   3.8  Control
#>  6 Control 1 Day.6   4.8  Control
#>  7 Control 2 Day.1   1.22 Control
#>  8 Control 2 Day.2   1.86 Control
#>  9 Control 2 Day.3   2.01 Control
#> 10 Control 2 Day.4   2.53 Control
#> # … with 50 more rows


growth2 %>%
  filter(group != "") %>%
  ggplot(aes(Day, Growth, color = group, group = group))  
  geom_point()  
  geom_smooth(method = "lm")
#> `geom_smooth()` using formula 'y ~ x'

Created on 2021-10-11 by the reprex package (v2.0.1)

The docs go into more detail about grouping.

  • Related