When ploting this data frame:
Country processing.level yend y col
<chr> <dbl> <dbl> <dbl> <chr>
1 BRA 1 957800 0 #073b4c
2 BRA 2 959258. 957800 #118ab2
3 BRA 3 538306. 959258. #06d6a0
4 BRA 4 301212. 538306. #ffd166
5 BRA 5 276294. 301212. #ef476f
6 CHN 1 1446265. 0 #073b4c
7 CHN 2 2234591. 1446265. #118ab2
8 CHN 3 2843813. 2234591. #06d6a0
9 CHN 4 2930715. 2843813. #ffd166
10 CHN 5 1309941. 2930715. #ef476f
ggplot uses the colors specified in the col column, but not in the correct order. For example, processing.level = 1 is taking the color of processing.level=2 and processing.level=3is taking the color of processing.level=1.
My color is mapped inside aes of my ggplot, and the colors in the legend are getting ploted correctly. I cannot find the problem with the code of the plot.
consumption_supplychain %>%
mutate(xval = as.numeric(factor(Country)) + scales::rescale(processing.level, to=c(-0.05, 0.05))) %>%
ggplot() +
geom_segment(aes(x = xval, xend = xval,y=y, yend = yend, color=consumption_supplychain$col),
linejoin = 'bevel',
lineend = 'butt',
linewidth = 1.5,
arrow = arrow(angle = 55, length = unit(1.5, "mm"))) +
scale_color_manual(name = "",values = consumption_supplychain$col,
labels=c("Domestically produced hides",
"Trade in raw hides",
"Trade in tanned hides",
"Trade in leather",
"Trade in consumer goods"))+
scale_x_continuous("", breaks = 1:5,
labels = c("Brazil", "China", "Eropean Union", "India", "USA")) +
scale_y_continuous(labels = scales::label_number(scale=1/1000000)) +
#theme_light(base_size = 16)+
ylab("Million tonnes per year")
Thanks a lot!
>Solution :
If you simply want to use colors from a column in your dataset I would go for scale_color_identity. However, if you want a legend and want to assign labels I would suggest to map a dataframe column on the color aes, i.e. as far as I get it you want to color by processing.level. Then use named vectors to assign your labels and colors via scale_color_manual.
library(ggplot2)
library(dplyr, warn = FALSE)
pal_color <- unique(consumption_supplychain$col)
names(pal_color) <- unique(consumption_supplychain$processing.level)
labels_color <- c(
"Domestically produced hides",
"Trade in raw hides",
"Trade in tanned hides",
"Trade in leather",
"Trade in consumer goods"
)
names(labels_color) <- 1:5
pal_color
#> 1 2 3 4 5
#> "#073b4c" "#118ab2" "#06d6a0" "#ffd166" "#ef476f"
labels_color
#> 1 2
#> "Domestically produced hides" "Trade in raw hides"
#> 3 4
#> "Trade in tanned hides" "Trade in leather"
#> 5
#> "Trade in consumer goods"
consumption_supplychain %>%
mutate(
xval = as.numeric(factor(Country)) +
scales::rescale(processing.level, to = c(-0.05, 0.05))
) %>%
ggplot() +
geom_segment(
aes(
x = xval, xend = xval, y = y, yend = yend,
color = factor(processing.level)
),
linejoin = "bevel",
lineend = "butt",
linewidth = 1.5,
arrow = arrow(angle = 55, length = unit(1.5, "mm"))
) +
scale_color_manual(
values = pal_color,
labels = labels_color,
name = NULL
) +
scale_x_continuous("",
breaks = 1:5,
labels = c("Brazil", "China", "Eropean Union", "India", "USA")
) +
scale_y_continuous(
labels = scales::label_number(scale = 1 / 1000000)
) +
ylab("Million tonnes per year")

consumption_supplychain <- structure(list(Country = c(
"BRA", "BRA", "BRA", "BRA", "BRA",
"CHN", "CHN", "CHN", "CHN", "CHN"
), processing.level = c(
1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
), yend = c(
957800, 959258,
538306, 301212, 276294, 1446265, 2234591, 2843813, 2930715, 1309941
), y = c(
0, 957800, 959258, 538306, 301212, 0, 1446265, 2234591,
2843813, 2930715
), col = c(
"#073b4c", "#118ab2", "#06d6a0", "#ffd166",
"#ef476f", "#073b4c", "#118ab2", "#06d6a0", "#ffd166", "#ef476f"
)), class = "data.frame", row.names = c(
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10"
))