1

I have this piece of R code that should plot some data growth_data.txt. Basically, it should plot a line graph showing a single line (or line + points) for the control and treated animals in this dataset. That is, one line for all the controls and one line for all the treated animals. Add appropriate error bars for each time point. But I don't know why the plot doesn't show the line and error bars on the plot which is weird.

What is wrong in my code? How to fix it? I included the plot I'm getting now.

library(tximport)
library(DESeq2)
library(tidyverse)
library(cowplot)
library(pheatmap)
library(RColorBrewer)
library(dplyr)
library(ggplot2)
theme_set(theme_classic())

growth_data <- read.delim ("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


growth2 %>% filter(group!= "") %>% ggplot() + aes(Day, Growth, color=group) + geom_point() + geom_smooth(method = lm)

enter image description here

Tina J
  • 4,983
  • 13
  • 59
  • 125
  • Is `Day` a factor? If you use `aes(as.numeric(Day), Growth, color=group)` do you get the line? And change `method = lm` to `method = "lm"` or it won't work. I think perhaps this question is a duplicate of https://stackoverflow.com/questions/35560433/geom-smooth-in-ggplot2-not-working-showing-up but without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) it's hard to say – jared_mamrot Oct 11 '21 at 00:06
  • Day in the growth_data is the horizontal axis. – Tina J Oct 11 '21 at 00:17
  • None worked for me. `"lm"` didn't make a change. And the numeric day just plots two lines and removes everything. – Tina J Oct 11 '21 at 02:37
  • Added the new link. – Tina J Oct 11 '21 at 02:43

1 Answers1

2

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.

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Tnx. It works! So the only difference is the last argument "group = group"? What does it do exactly? – Tina J Oct 11 '21 at 03:50
  • 1
    Basically it is telling ggplot that the points are linked by the variable "group" so when you draw a line with geom_smooth(), draw two lines instead of one: there is further discussion and examples in [the docs](https://ggplot2.tidyverse.org/reference/aes_group_order.html) – jared_mamrot Oct 11 '21 at 05:34