Saturday, January 7, 2023

Change of colors by group

 set.seed(68117534)              # Set random seed

data <- data.frame(x = 1:15,    # Create example data frame

                   y = c(runif(15, 0, 2),

                         runif(15, 2, 6),

                         runif(15, 3, 4),

                         runif(15, 1, 3)),

                   group = rep(LETTERS[1:4], each = 15))

head(data)                      # Print head of example data frame


  x         y group

1 1 0.3478138     A

2 2 1.9822095     A

3 3 0.4259092     A

4 4 1.2099873     A

5 5 0.4440186     A

6 6 0.3353117     A

 # Change colors of lines by group

ggplot(data,                   

       aes(x = x,

           y = y,

           group = group)) +

  geom_line(aes(col = group)) +

  geom_point()


 Change colors of lines & points by group

ggplot(data,                    
       aes(x = x,
           y = y,
           col = group)) +
  geom_line() +
  geom_point()

# Specify colors manually
ggplot(data,                    
       aes(x = x,
           y = y,
           col = group)) +
  geom_line() +
  geom_point() +
  scale_color_manual(values = c("#ca7dcc",
                                "#1b98e0",
                                "#353436",
                                "#02e302"))
# Change colors of boxplots
ggplot(data,                    
      aes(y = y,
          fill = group)) +
  geom_boxplot()



No comments:

Post a Comment