gpt4 book ai didi

r - geom_line : How to connect only a few points

转载 作者:行者123 更新时间:2023-12-04 16:33:33 27 4
gpt4 key购买 nike

我有这个数据框和这个图:

df <- data.frame(Groupe = rep(c("A","B"),4),
Period = gl(4,2,8,c("t0","t1","t2","t3","t4")),
rate = c(0.83,0.96,0.75,0.93,0.67,0.82,0.65,0.73))

ggplot(data = df, mapping = aes(y = rate, x = Period ,group = Groupe, colour=Groupe, shape=Groupe)) +
geom_line(size=1.2) +
geom_point(size=5)

Plot

我如何组织我的数据,以便 t1 之间的点和 t2不是用线连接的吗?我要 t0t1待连接(蓝色或红色根据组别), t2t3以相同的方式连接,但 t1 之间没有线和 t2 .我通过查看类似的问题尝试了几件事,但它总是弄乱我的分组颜色:/

最佳答案

手动创建新的分组变量通常不是最好的方法。因此,一种稍微不同的方法需要较少的硬编码:

# create new grouping variable
df$grp <- c(1,2)[df$Period %in% c("t2","t3","t4") + 1L]

# create the plot and use the interaction between 'Group' and 'grp' as group
ggplot(df, aes(x = Period, y = rate,
group = interaction(Groupe,grp),
colour = Groupe,
shape = Groupe)) +
geom_line(size=1.2) +
geom_point(size=5)

这给出了与另一个答案相同的情节:

enter image description here

关于r - geom_line : How to connect only a few points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35181228/

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