简体   繁体   中英

Problems in plotting line and error bars on the plot in R

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)

在此处输入图像描述

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM