gpt4 book ai didi

r - ggplot2 的奇怪线条

转载 作者:行者123 更新时间:2023-12-04 09:52:46 25 4
gpt4 key购买 nike

我有以下数据集:

> str(dat)
'data.frame': 5000 obs. of 3 variables:
$ y: num 0.864 0.869 0.871 0.879 0.874 0.871 0.871 0.873 0.864 0.869 ...
$ A: Factor w/ 5 levels "0.2","0.5","0.8",..: 1 1 1 1 1 1 1 1 1 1 ...
$ x: num 1 2 3 4 5 6 7 8 9 10 ...
> head(dat)
y A x
1 0.864 0.2 1
2 0.869 0.2 2
3 0.871 0.2 3
4 0.879 0.2 4
5 0.874 0.2 5
6 0.871 0.2 6

“x”列是向量 c(1:5000):

> all(dat$x==1:5000)
[1] TRUE

因此,在绘制以下图时,我不理解某些线条的存在:

ggplot() + geom_line(aes(x=x, y=y, color=A), data=dat) 

我说的这几行在图中用三个黑色箭头表示: ggplot

编辑:下面是一个具有可重现模拟数据集的类似示例:

set.seed(666)
mu <- rep(c(200, 400, 600, 300, 500), each=1000)
A <- factor(rep(c(1,2,3,1,2), each=1000))
y <- rnorm(length(mu), mu, 100)
dat <- data.frame(x=1:length(mu), y=y, A=A)
ggplot() + geom_line(aes(x=x, y=y, color=A), data=dat)

最佳答案

您需要数据框中的另一个变量来表示不同的 block (即具有相同颜色的区域)和 geom_line 中的 group 参数:

dat <- data.frame(x=1:length(mu), y=y, A=A, B=gl(5, 1000))
ggplot() + geom_line(aes(x=x, y=y, color=A, group=B), data=dat)

ggplot output

关于r - ggplot2 的奇怪线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com