1

Original data as below:

df<-structure(list(AN = c(2017, 2017, 2017, 2017, 2017, 2017, 2018, 
2018, 2018, 2018, 2018, 2018, 2018), MOIJOUR = c("6/6", "6/7", 
"6/8", "6/9", "6/10", "6/11", "6/1", "6/2", "6/3", "6/4", "6/5", 
"6/6", "6/7"), VPD = c(0.741228595402395, 0.699187195061414, 
1.12965748245911, 0.54394053002255, 1.23866162348333, 1.51945826486904, 
0.717749850765411, 0.561711392605254, 0.410696309022948, 0.450476445623015, 
0.225679384729934, 0.236211890223451, 0.428751867004609)), row.names = c(NA, 
-13L), class = c("tbl_df", "tbl", "data.frame"))

I tried to connect points of two years separately.My code as below:

library(ggplot2)
library(dplyr)
library(readxl)
library(ggpmisc)
library("RColorBrewer")

ggplot(df, aes(MOIJOUR,VPD)) + 
  geom_point(aes(shape=as.factor(AN),color=as.factor(AN)))+
  geom_line(aes(color=as.factor(AN)),group=1) +
  scale_color_manual(values = c("#E41A1C","#377EB8"))+
  theme(axis.text.x = element_text(vjust=0.5, angle=75, colour = "black"))

Here is the figure I got: enter image description here

How can remove the red line circled in black? There are from 2 separate years, so I don't want them to be connected.

Chouette
  • 153
  • 7

2 Answers2

2

Essentially grouping by same groups as you use for color would work::

library(ggplot2)

ggplot(df, aes(MOIJOUR,VPD)) + 
  geom_point(aes(shape=as.factor(AN),color=as.factor(AN)))+
  geom_line(aes(color=as.factor(AN), group = as.factor(AN))) +
  scale_color_manual(values = c("#E41A1C","#377EB8"))+
  theme(axis.text.x = element_text(vjust=0.5, angle=75, colour = "black"))

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22
1

Grouping by AN will do the trick:

ggplot(df, aes(MOIJOUR, VPD)) + 
  geom_point(aes(shape=as.factor(AN), color=as.factor(AN)))+
  geom_line(aes(color=as.factor(AN), group=AN)) +
  scale_color_manual(values = c("#E41A1C", "#377EB8")) +
  theme(axis.text.x = element_text(vjust=0.5, angle=75, colour = "black"))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66