【问题标题】:Problems in plotting line and error bars on the plot in R在 R 中的绘图上绘制线和误差线的问题
【发布时间】:2021-11-29 19:40:17
【问题描述】:

我有这段R 代码应该绘制一些数据growth_data.txt。基本上,它应该绘制一个线图,显示该数据集中的对照和处理动物的单线(或线 + 点)。也就是说,一条线用于所有对照,一条线用于所有处理过的动物。为每个时间点添加适当的误差线。但我不知道为什么情节没有在情节上显示线条和误差线,这很奇怪。

我的代码有什么问题?如何解决?我包括了我现在得到的情节。

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)

【问题讨论】:

  • Day 是一个因素吗?如果您使用aes(as.numeric(Day), Growth, color=group),您会收到电话吗?并将method = lm 更改为method = "lm" 否则将不起作用。我认为这个问题可能与stackoverflow.com/questions/35560433/… 重复,但没有minimal reproducible example 很难说
  • growth_data 中的天为横轴。
  • 没有一个对我有用。 "lm" 没有做出改变。数字日只是绘制两条线并删除所有内容。
  • 添加了新链接。

标签: r ggplot2 plot


【解决方案1】:

抱歉 - 我认为“Day”是一个因素是不正确的 - 感谢您修复损坏的链接。

一种可能的解决方案是添加“群体”美学,例如

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'

reprex package (v2.0.1) 于 2021 年 10 月 11 日创建

The docs 详细了解分组。

【讨论】:

  • Tnx。有用!所以唯一的区别是最后一个参数“group = group”?它到底是做什么的?
  • 基本上它告诉 ggplot 这些点是由变量“group”链接的,所以当你用 geom_smooth() 画一条线时,画两条线而不是一条线:@ 中有进一步的讨论和示例987654323@
猜你喜欢
  • 1970-01-01
  • 2018-03-30
  • 2013-03-15
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 2012-10-30
  • 1970-01-01
相关资源
最近更新 更多