0

I want to create a ggplot with 1 geom_point and 2 geom_line (calculated from my variable that is going to be represented on my geom_point)

Here is the script that I though was correct, but I cannnot make the 2 geom_line to appear on my ggplot. However, when I change them in geom_point they all appear correctly on the graph see image at the bottom.

#Acute/chronique
library(dplyr)
library(ggplot2)
library(gsheet)
library(googlesheets)
library(tidyr)


date <- c("2014-07-06", "2014-07-07","2014-07-08","2014-07-09", "2014-07-09","2014-07-10","2014-07-11","2014-07-12","2014-07-13", "2014-07-14","2014-07-15","2014-07-16","2014-07-17")
par <- c("RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE")
temp <- c(14, 10, 16, 13, 10, 9, 10, 14, 16, 4, 7, 16, 18)

df <- data.frame(date, par, temp)

lag_functions <- setNames(c(paste("(", paste("dplyr::lag(., ", 1:3, ")", collapse = ' + '), ") / 3"),
                            paste("(", paste("dplyr::lag(., ", 1:5, ")", collapse = ' + '), ") / 5")), 
                          c("acute", "chronic"))



ldf<-df%>%
  mutate_at(vars(temp), funs_(lag_functions))


ldf

ggplot(ldf, aes(x = date, y = temp)) +
  geom_point()

ggplot(ldf) + 
  geom_point(aes(x = date, y = temp)) + 
  geom_line(aes(x = date, y = acute), col = 'red') + 
  geom_line(aes(x = date, y = chronic), col = 'blue') + 
  xlab("Date") + 
  ylab("RPE")

With the geom_line

with geom_point

Thank you for your help

pat28dh
  • 77
  • 7

1 Answers1

0

As date is a character you have to add the group aesthetic, i.e. add e.g. group=1 to each of your geom_line

library(dplyr)
library(ggplot2)
library(tidyr)


date <- c("2014-07-06", "2014-07-07","2014-07-08","2014-07-09", "2014-07-09","2014-07-10","2014-07-11","2014-07-12","2014-07-13", "2014-07-14","2014-07-15","2014-07-16","2014-07-17")
par <- c("RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE", "RPE")
temp <- c(14, 10, 16, 13, 10, 9, 10, 14, 16, 4, 7, 16, 18)

df <- data.frame(date, par, temp)

lag_functions <- setNames(c(paste("(", paste("dplyr::lag(., ", 1:3, ")", collapse = ' + '), ") / 3"),
                            paste("(", paste("dplyr::lag(., ", 1:5, ")", collapse = ' + '), ") / 5")), 
                          c("acute", "chronic"))
ldf<-df%>%
  mutate_at(vars(temp), funs_(lag_functions))

ggplot(ldf) + 
  geom_point(aes(x = date, y = temp)) + 
  geom_line(aes(x = date, y = acute, group = 1), col = 'red') + 
  geom_line(aes(x = date, y = chronic, group = 1), col = 'blue') + 
  xlab("Date") + 
  ylab("RPE")
#> Warning: Removed 3 row(s) containing missing values (geom_path).
#> Warning: Removed 5 row(s) containing missing values (geom_path).

stefan
  • 90,330
  • 6
  • 25
  • 51