我想要一個用一條線連接缺失點的圖表,但不在缺失點顯示符號。這是一些創建一些測驗資料并生成圖表的代碼,但我想要一條 (id=B) 綠線將點 4 和 6 與一條直線連接起來,但在時間 5 沒有綠色三角形。我確實嘗試過,geom_path而不是geom_line,但沒有太大變化。謝謝
library(ggplot2)
set.seed(1)
x <- rep(seq(1:10), 3)
y <- c(runif(10), runif(10) 1, runif(10) 2)
group <- c(rep("A", 10), rep("B", 10), rep("C", 10))
df <- data.frame(cbind(group, x, y))
df$x <- as.numeric(df$x)
df$y <- as.numeric(df$y)
df[15,]$y <- NA
ggplot(data=df, aes(x=x, y=y, group=group))
geom_line(aes(colour=group))
geom_point(aes(colour=group, shape=group))
scale_shape_manual(values = c(1:3))
theme(legend.position="bottom")
uj5u.com熱心網友回復:
您可以通過洗掉 y 所在的行來做到這一點NA:
df2 <- df[!is.na(df$y), ]
ggplot(data=df2, aes(x=x, y=y, group=group))
geom_line(aes(colour=group))
geom_point(aes(colour=group, shape=group))
scale_shape_manual(values = c(1:3))
theme(legend.position="bottom")

uj5u.com熱心網友回復:
library(tidyverse)
# form some data
set.seed(1)
x <- rep(seq(1:10), 3)
y <- c(runif(10), runif(10) 1, runif(10) 2)
group <- c(rep("A", 10), rep("B", 10), rep("C", 10))
df <- tibble(group=group, x = x, y = y)
# add an NA
df[15,]$y <- NA
# plot with a gap where the NA is
p1 <- # p1 fails because of NA
ggplot(data=df, aes(x=x, y=y, group=group))
geom_line(aes(colour=group))
geom_point(aes(colour=group, shape=group))
scale_shape_manual(values = c(1:3))
theme(legend.position="bottom")
# plot with no gap
p2 <- p1 % % {df %>% na.omit()} # p2 does what you want
p1
p2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529067.html
