Choropleth colors aren't being applied according to breaks/intervals

Hello, I created a choropleth with base R but I'm struggling with the colors. First, the colors don't follow the same order as the intervals and second, two of the intervals are using the same color. This happens regardless of how many colors I use. It also doesn't matter whether I'm using brewer.pal or base colors.

Below are the statements that I used to create the graph once data has been downloaded:

#Create palette 
cal_com_col <- brewer.pal(8, "Reds")

#Create breaks
cal_com_breaks <- c(0,20000,40000,60000,80000,100000,120000)

#Create intervals
cut(cal_pop$Pop2016, cal_com_breaks, dig.lab = 6)

#Apply colors to values according intervals
my_col <- cal_com_col[findInterval(cal_pop$Pop2016, vec = cal_com_breaks)]

#Create plot and legend
plot(cal_com,col = my_col, main = "Calgary Population by Community (2016)")

legend("topleft", fill = my_col, legend = levels(cut(cal_pop$Pop2016, cal_com_breaks, dig.lab = 6)), title = "Population")

I'm not sure if this is relevant, but when I run cut , I get the following output:

> cut(cal_pop$Pop2016, cal_com_breaks, dig.lab = 6)
 [1] (80000,100000]  (60000,80000]   (40000,60000]   (60000,80000]   (20000,40000]   (100000,120000] (100000,120000]
 [8] (100000,120000] (80000,100000]  (60000,80000]   (80000,100000]  (100000,120000] (100000,120000] (40000,60000]  
[15] (20000,40000]   (40000,60000]   (40000,60000]   (60000,80000]   (20000,40000]   (60000,80000]   (20000,40000]  
[22] (20000,40000]  
Levels: (0,20000] (20000,40000] (40000,60000] (60000,80000] (80000,100000] (100000,120000]

Below are the links to the datasets I used:

Is it possible that when I create the plot, R applies the colors based on the order of the values above and not on the order of the levels? I'm new to R and and to statistical coding in general, so I might be missing something very obvious. Any help would be greatly appreciated!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.