1
votes

Apologies I know that there are a few threads on this issue, which I have read but cant seem to make progress

Simply I would like to add a regression line to my chart time to crack password vs age

time to crack password va age

Something similar to This example

However when I try with geom_smooth I get this error geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

Does anyone know if it possible to add a regression or trend line?

    p3 <- ggline(DF6, x = "units_of_time", y = "as.numeric.Final_DF.age.", 
       add = c("mean_se", "jitter"), 
       color =  "units_of_time",
       ylab = "Age", xlab = "Time to crack") + 
  scale_x_discrete(labels = c("instant", "seconds", "minutes", 
                              "hours", "days", "months", "years")) + 
  scale_color_manual(values = c("red", "orange", "yellow", "grey", 
                                "black", "light green", "green")) + 
  ggtitle("Time to crack password vs age") + 
  guides(color=FALSE)

p3 +  geom_smooth(aes(colour = units_of_time, group = units_of_time))

dput(DF6)
structure(list(as.numeric.Final_DF.age. = c(49, 47, 53, 45, 49, 
51, 45, 45, 51, 43, 49, 51, 45, 49, 37, 45, 47, 59, 55, 39, 53, 
51, 43, 51, 49, 47, 41, 53, 49, 39, 47, 51, 55, 43, 59, 49, 53, 
57, 47, 41, 55, 47, 53, 41, 57, 43, 49, 57, 55, 61), Final_DF.pswd_length = c(8L, 
4L, 8L, 12L, 12L, 10L, 7L, 5L, 6L, 9L, 9L, 5L, 7L, 4L, 13L, 7L, 
9L, 8L, 6L, 13L, 12L, 5L, 7L, 5L, 10L, 11L, 10L, 12L, 8L, 10L, 
10L, 4L, 6L, 10L, 6L, 10L, 14L, 6L, 10L, 11L, 4L, 9L, 8L, 11L, 
4L, 7L, 3L, 8L, 9L, 12L), units_of_time = c(1, 1, 1, 7, 2, 2, 
2, 3, 1, 7, 1, 2, 5, 2, 7, 6, 7, 1, 1, 7, 2, 2, 7, 2, 2, 7, 7, 
4, 2, 7, 7, 2, 1, 7, 1, 2, 4, 1, 7, 7, 2, 1, 2, 7, 1, 4, 1, 1, 
1, 1)), row.names = c(NA, -50L), class = "data.frame")
1

1 Answers

0
votes

Instead of package ggpubr, I will use package ggplot2 directly.
The creation of vector xlabels is not strictly necessary, but I defined it to make the plotting code simpler to read.

library(ggplot2)

xlabels <- c("instant", "seconds", "minutes", 
             "hours", "days", "months", "years")
xlabels <- setNames(sort(unique(DF6$units_of_time)), xlabels)


p3 <- ggplot(DF6, aes(x = units_of_time, y = as.numeric.Final_DF.age.)) +
  geom_jitter(aes(colour = factor(units_of_time)), width = 0.25) +
  scale_x_continuous(breaks = xlabels, labels = names(xlabels)) + 
  scale_color_manual(values = c("red", "orange", "yellow", "grey", 
                                "black", "light green", "green")) + 
  ggtitle("Time to crack password vs age") + 
  labs(x = "Units of time", y = "Age") +
  guides(color = FALSE) +
  theme_bw()

p3 + geom_smooth(method = lm, formula = y ~ x)

enter image description here