如何使用 ggline 更改几何点的颜色

How to change colour of geom points using ggline

我想更改图表上点的颜色

目前都是背分,我想改成 instant = red, seconds = orange, minutes = yellow, hours = grey, days = black, 月 = 浅绿色,年 = 绿色

请问有什么办法吗?

       > 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")

    ggline(DF6, x = "units_of_time", y = "as.numeric.Final_DF.age.", 
       add = c("mean_se", "jitter"), 
       order = c("1", "2", "3", "4", "5", "6", "7"),
       ylab = "Age", xlab = "Time to crack") + scale_x_discrete(labels = c("instant", "seconds", "minutes", 
                                                                           "hours", "days", "months", "years")) +
  ggtitle("Time to crack password vs age")

您可以分配 color = "units_of_time" 并使用 scale_color_manual 根据您的选择分配颜色。

library(ggpubr)
library(ggplot2)

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)