I have the following code and graph:
library(ggplot2)
library(dplyr)
dt <- data.table(cod= c(202101,202101,202101,202101,202102,202102,202102,202102,202103,202103,202103,202103,202104,202104,202104,202104),
val= c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4),
porc= c(0.01,0.03,0.06,0.07,0,0.04,0.07,0.07,0.02,0.04,0.04,0.04,0.02,0.02,0.02,0.02)
)
dt %>%
mutate(cod= as.character(cod)) %>%
ggplot(aes(x=val,y=porc))
geom_line(aes(color=cod))
geom_point(aes(color=cod))
I want to modify the results on the graph by the following rule: -max code (202104 on the example): don't modify -max code -1 (2020103 on the example): show all the results less the last one -max code -2 (2020102 on the example): show all the results less the last two -and so on
So, my final result should be something like this:
CodePudding user response:
You can goup the variables by cod
and then remove the duplicated values in porc
dt %>%
mutate(cod= as.character(cod)) %>%
group_by(cod) %>%
distinct(porc, .keep_all = TRUE) %>%
ungroup() %>%
ggplot(aes(x=val,y=porc))
geom_line(aes(color=cod))
geom_point(aes(color=cod))