Sharklady
Sharklady

Reputation: 57

How to set factor order and colours in ggplot2

I am trying to assign colors to each of my 7 variables and set my variables in a specific order. However 'my_scale' in the geom_col code seems to override my factor order and puts them back in alphabetical order. They are ordered correctly when I run the script without the 'my_scale' line.

my_colors <- c("#1b9e77", "#d95f02", "#66A628", "#E81E89", "#7570b3", "#FFC000", "#0E85F2")
names(my_colors) <- levels(factor(c(levels(behavssec$Behaviour), levels(behavssec$Behaviour))))
my_scale <- scale_fill_manual(name = "Behaviour", values = my_colors)
behavssec$Behaviour <- factor(behavssec$Behaviour,levels = c("Burst","High energy swimming"   , "Medium energy swimming", "Low energy swimming", "Travel", "Ascending", "Descending"))

ggplot(behavssec, aes(fill=Behaviour, y=n, x= SharkID)) +
    geom_col(position = position_dodge2(width = 0.9, preserve = "single"))+
  theme_classic ()+ my_scale +
  theme (axis.text.x=  element_text( size = 12))+
theme (axis.text.y = element_text( size=16))+
  labs(x= "SharkID", y= "Time (second)")

Graph with my_scale - colors assigned to factor but in the wrong order:

Graph with my_scale - colors assigned to factor but in the wrong order

Factors in the right order but without my assigned colors:

Factors in the right order but without my assigned colors

This is the data I am working with:

SharkID Behaviour   n
2   High energy swimming    2067
2   Medium energy swimming  3413
3   High energy swimming    16473
3   Medium energy swimming  15191
4   Burst   11
4   High energy swimming    825
4   Low energy swimming 24855
4   Medium energy swimming  465
5   High energy swimming    38
5   Low energy swimming 20063
5   Medium energy swimming  12
6   Burst   102
6   High energy swimming    21262
6   Medium energy swimming  6233
1   Ascending   1654
1   Burst   19
1   Descending  1211
1   Low energy swimming 5357
1   Travel  10685

Upvotes: 1

Views: 1366

Answers (1)

Roman
Roman

Reputation: 4989

You had the right idea. For scale_fill_manual the colors need to be supplied in a named vector. Slightly simplified example:

Code

my_colors <- c(
    "#1b9e77", "#d95f02", "#66A628", "#E81E89", 
    "#7570b3", "#FFC000", "#0E85F2"
)
names(my_colors) <- levels(behavssec$Behaviour)

ggplot(behavssec, aes(x = SharkID, y = n, fill = Behaviour)) +
    geom_col(position = position_dodge2(width = 0.9, preserve = "single")) +
    scale_fill_manual(name = "Behaviour", values = my_colors)

enter image description here

Data

library(ggplot2)
behavssec <- structure(list(SharkID = c(2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L), Behaviour = c("High energy swimming", 
"Medium energy swimming", "High energy swimming", "Medium energy swimming", 
"Burst", "High energy swimming", "Low energy swimming", "Medium energy swimming", 
"High energy swimming", "Low energy swimming", "Medium energy swimming", 
"Burst", "High energy swimming", "Medium energy swimming", "Ascending", 
"Burst", "Descending", "Low energy swimming", "Travel"), n = c(2067L, 
3413L, 16473L, 15191L, 11L, 825L, 24855L, 465L, 38L, 20063L, 
12L, 102L, 21262L, 6233L, 1654L, 19L, 1211L, 5357L, 10685L)), class = "data.frame", row.names = c(NA, 
-19L))

behavssec <- read.table(text = "
SharkID,Behaviour,n
2,High energy swimming,2067
2,Medium energy swimming,3413
3,High energy swimming,16473
3,Medium energy swimming,15191
4,Burst,11
4,High energy swimming,825
4,Low energy swimming,24855
4,Medium energy swimming,465
5,High energy swimming,38
5,Low energy swimming,20063
5,Medium energy swimming,12
6,Burst,102
6,High energy swimming,21262
6,Medium energy swimming,6233
1,Ascending,1654
1,Burst,19
1,Descending,1211
1,Low energy swimming,5357
1,Travel,10685
", header = TRUE, sep = ",")

behavssec$Behaviour <- factor(
    behavssec$Behaviour,
    levels = c(
        "Burst","High energy swimming",
        "Medium energy swimming",
        "Low energy swimming",
        "Travel",
        "Ascending",
        "Descending"
    )
)

Upvotes: 0

Related Questions