gpt4 book ai didi

r - ggplot 更改由 x 轴值指定的线条颜色

转载 作者:行者123 更新时间:2023-12-04 11:02:55 24 4
gpt4 key购买 nike

要重现的代码:

myDat <- data.frame(Event = rep(c("Arrival", "Departure"), 3),
AtNode = c("StationA", "StationA", "Track", "Track", "StationB", "StationB"),
Lane = c("Lane1", "Lane1", "Lane2", "Lane2", "Lane1", "Lane1"),
atTime = c(10, 12, 18, 20, 34, 36),
Type = c("Station", "Station", "Track", "Track", "Station", "Station"),
Train = 1 )
ggplot(data =myDat, aes(x = atTime, y=factor(AtNode, levels = unique(paste(myDat[order(myDat$atTime),"AtNode"]))), group = Train, colour = Lane ))+
geom_point(data = myDat)+
geom_path(data = myDat[which(!grepl(pattern = "Track", myDat$Type)),])

enter image description here

现在我需要将两个绿色点(Y = “Track”)投影到橙色线上,并将投影点之间的线着色为与点相同的颜色。

预期结果:(没有分数(Y ="Track")

enter image description here

提前感谢每一个提示或技巧!

干杯

最佳答案

我认为您的输出不是展示您想要的内容的正确方式。您的 y 轴 上有 factors,这意味着它的范围在 1 到 3 之间。

因此,在 y 轴 值方面,在那里投影一条线没有任何意义。

对我来说,显示数据的正确方式是这样的

ggplot(data =myDat,
aes(x = atTime, y=factor(AtNode, levels = unique(paste(myDat[order(myDat$atTime),"AtNode"]))),
group = AtNode, colour = Lane ))+
geom_point()+
geom_line() +
labs(y = 'AtNode')

enter image description here

但是,按照你的要求,你可以做一些简单的三角函数来投影你的线段

x1 = 1 + tan(asin(2/sqrt(484)))*6 #y projection given x = 18
x2 = 1 + tan(asin(2/sqrt(484)))*8 #y projection given x = 20

foo = data.frame(x = c(18,20), y = c(x1, x2), Lane = "Lane2")

ggplot(data = myDat, aes(x = atTime, y=factor(AtNode, levels = unique(paste(myDat[order(myDat$atTime),"AtNode"]))), group = 1, colour = Lane ))+
geom_path(data = myDat[which(!grepl(pattern = "Track", myDat$Type)),]) +
geom_line(data = foo, aes(x = x, y = y, color = Lane), size = 1) +
scale_y_discrete(drop = FALSE)

enter image description here

关于r - ggplot 更改由 x 轴值指定的线条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921590/

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