這似乎有效,但會產生一條警告訊息,指出未繪制 NA:
Warning messages:
1: Removed 64 row(s) containing missing values (geom_path).
2: Removed 64 rows containing missing values (geom_point).
選項 2: 
可以在此處找到更多格式選項
uj5u.com熱心網友回復:
也許試試這個。我建議將各個 ID 分成單個方面。
library(tidyverse)
my_data <- tibble::as_tibble(my_data)
my_data <- my_data %>%
tidyr::pivot_longer(!c(id), names_to = "tp", values_to = "measure")
my_data <- my_data %>% dplyr::mutate(
tp = factor(tp, levels = c(
"weight_time_1", "weight_time_2", "weight_time_3",
"weight_time_4", "weight_time_5", "weight_time_6",
"weight_time_7", "weight_time_8", "weight_time_9",
"weight_time_10"
)),
id = factor(id)
)
my_data %>%
ggplot2::ggplot(aes(tp, measure, col = id, group = id))
ggplot2::geom_point()
ggplot2::geom_line()
ggplot2::theme(axis.text.x = element_text(angle = 90, vjust = .5, hjust = 1))
ggplot2::facet_wrap(~id)
uj5u.com熱心網友回復:
使用 ggplot2 繪圖時,有很多選項可以處理 NA。
離開他們,接受警告(真的沒有錯)
drop NA before plotting see JeffV's answer using tidyr::drop_na,但是有很多方法,看這個超流行的執行緒
在您的情況下,您可以在旋轉時洗掉 NA - 使用
tidyr::pivot_longer(..., values_drop_na = TRUE)添加
na.rm = TRUE到感興趣的幾何圖形:
library(ggplot2)
library(dplyr)
library(tidyr)
my_data %>%
pivot_longer(cols = starts_with("weight")) %>%
# your x is essentially continuous. Thus make it REALLY continuous!
# your id is categorical, so make it that
mutate(time = as.integer(gsub(".*([0-9] )", "\\1", name)),
id = as.character(id)) %>%
ggplot(aes(x=time, y=value, colour=id, group=id))
geom_line(alpha=.5, na.rm = TRUE)

由reprex 包于 2022-06-01 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484481.html
