Reference one color column based on another

Asked

Viewed 83 times

4

I have a dataset with several columns, but a specific column is reference for the colors of the chart, as I can reference this column in scale_fill_manua ggplot automatically?

Obs. The same color can be used in more than one reference.

In the case manual would only create c("BIF"="#543005", "BRE"="#bf812d", ...), but since it’s a lot of data there would be a way to automate this?

My dataset:

> dput(dataset)
structure(list(INI = c(0, 0, 0, 0, 0.9, 15.85, 20.95, 23.05, 
27.3, 32.2, 41.5, 41.75, 45.7, 50, 72.6, 74.3, 82.65, 104.45, 
105.2, 106.15, 107.05, 121.8, 123.2, 124.7, 126.7, 128.4, 136.4, 
139.7, 151.95, 154.35, 168.2, 172.2, 174.75, 177.35, 178.3, 179.85, 
235.2, 237.1, 246.45, 249, 254.75, 255.65, 256.55, 258.1, 258.55, 
259.85, 261.6, 261.85, 261.85, 268.7, 270.4, 273.45, 275.5, 277.6, 
294.9, 295.4, 308.9, 310.3, 312.3, 321.95, 327.85, 328.9, 330.3, 
332.15, 361.65, 368.15, 372.7, 380.7, 385.4, 386, 387.7, 389.2, 
392.2, 394.55, 395.7, 396.85, 398, 400, 400.6, 402, 402.9, 405.7, 
410.95, 414.7, 435.35, 436.35, 442.85, 443.5, 457.45, 457.75, 
465.35, 468.4, 474.5, 475.6, 478.65, 480.1, 483.7, 485.6, 498.8, 
501.05, 514.2, 522), FIM = c(0.9, 15.85, 27.3, 41.5, 20.95, 72.6, 
23.05, 41.75, 32.2, 82.65, 45.7, 50, 151.95, 104.45, 74.3, 105.2, 
121.8, 107.05, 106.15, 168.2, 123.2, 295.4, 124.7, 126.7, 128.4, 
136.4, 139.7, 246.45, 154.35, 254.75, 172.2, 174.75, 177.35, 
178.3, 179.85, 235.2, 237.1, 258.55, 249, 255.65, 258.1, 256.55, 
261.85, 261.6, 259.85, 261.85, 294.9, 268.7, 270.4, 327.85, 273.45, 
275.5, 277.6, 328.9, 310.3, 308.9, 312.3, 386, 321.95, 330.3, 
332.15, 361.65, 385.4, 368.15, 400, 372.7, 380.7, 389.2, 387.7, 
405.7, 395.7, 392.2, 394.55, 396.85, 398, 400.6, 402.9, 414.7, 
402, 410.95, 436.35, 457.45, 422.45, 435.35, 442.85, 443.5, 457.75, 
454.75, 465.35, 468.4, 514.2, 474.5, 475.6, 478.65, 480.1, 483.7, 
485.6, 498.8, 501.05, 522, 537.4, 526.7), UNIDADES = structure(c(2L, 
4L, 3L, 1L, 2L, 4L, 2L, 2L, 3L, 3L, 1L, 2L, 1L, 2L, 4L, 4L, 3L, 
2L, 4L, 4L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 1L, 2L, 2L, 1L, 4L, 4L, 1L, 4L, 2L, 
4L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, 1L, 3L, 3L, 4L, 2L, 3L, 4L, 2L, 
4L, 4L, 4L, 3L, 1L, 3L, 4L, 4L, 4L, 3L, 4L, 3L, 2L, 4L, 4L, 3L, 
1L, 4L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 2L), .Label = c("UND-0001", "UND-0012", "UND-0042", 
"UND-0075"), class = "factor"), REF = structure(c(8L, 5L, 5L, 
4L, 3L, 3L, 4L, 3L, 9L, 5L, 5L, 6L, 4L, 3L, 4L, 4L, 1L, 3L, 4L, 
4L, 3L, 5L, 5L, 3L, 4L, 4L, 4L, 3L, 5L, 4L, 4L, 3L, 4L, 4L, 4L, 
4L, 5L, 3L, 5L, 3L, 5L, 5L, 3L, 5L, 5L, 3L, 4L, 5L, 5L, 3L, 5L, 
4L, 5L, 4L, 5L, 5L, 5L, 4L, 7L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 3L, 
5L, 5L, 4L, 5L, 3L, 5L, 5L, 5L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 7L, 
4L, 5L, 5L, 4L, 2L, 1L, 5L, 4L, 5L, 3L, 5L, 4L, 5L, 3L, 5L, 3L, 
5L, 1L, 3L), .Label = c("BRE", "GAB", "GRA", "GRN", "HID", "RIO", 
"RIU", "SOL", "UNK"), class = "factor"), COLOR = c(26265L, 0L, 
0L, 4737279L, 215L, 215L, 4737279L, 215L, 0L, 0L, 0L, 5014527L, 
4737279L, 215L, 4737279L, 4737279L, 5014527L, 215L, 4737279L, 
4737279L, 215L, 0L, 0L, 215L, 4737279L, 4737279L, 4737279L, 215L, 
0L, 4737279L, 4737279L, 215L, 4737279L, 4737279L, 4737279L, 4737279L, 
0L, 215L, 0L, 215L, 0L, 0L, 215L, 0L, 0L, 215L, 4737279L, 0L, 
0L, 215L, 0L, 4737279L, 0L, 4737279L, 0L, 0L, 0L, 4737279L, 7602234L, 
0L, 0L, 0L, 0L, 4737279L, 4737279L, 0L, 215L, 0L, 0L, 4737279L, 
0L, 215L, 0L, 0L, 0L, 7602234L, 0L, 0L, 0L, 0L, 0L, 0L, 7602234L, 
4737279L, 0L, 0L, 4737279L, 8454016L, 5014527L, 0L, 4737279L, 
0L, 215L, 0L, 4737279L, 0L, 215L, 0L, 215L, 0L, 5014527L, 215L
)), class = "data.frame", row.names = c(NA, -102L))
> 

Entree:

library("ggplot2")
library("broman")

dataset$COLOR = paste0("#",convert2hex(dataset$COLOR))
color = unique(dataset$COLOR)
aux = unique(dataset$REF)

ggplot(dataset, aes(UNIDADES, (INI + FIM)/2*-1, fill= REF)) +
  geom_tile(aes(height = (INI - FIM)*-1, width = 0.6), colour="black", size=0.1) +
  theme_classic() +
  labs(x = "UNIDADES", y = "TOTAL") +
  labs(fill = "REF")  +
  scale_fill_manual (values =dataset$COLOR)

Exit:

> color = unique(dataset$COLOR)
> aux = unique(dataset$REF)
> color
[1] "#006699" "#000000" "#4848ff" "#0000d7" "#4c83ff" "#74003a" "#80ff80"
> aux
[1] SOL HID GRN GRA UNK RIO BRE RIU GAB
Levels: BRE GAB GRA GRN HID RIO RIU SOL UNK
  • include the dataset in the question

  • Dataset included.

  • 3

    Note that in the df provided there are no variables UNIDADES, INI and FIM.

  • 1

    Maybe color <- setNames(color, aux). And then use this new vector on scale_fill_manual.

  • Thanks for the help @Rui Barradas, but I can have equal colors for different references.

2 answers

2

As pointed out by @Rui-Arradas in the comments, you can use setNames to associate the color names with the hexa code, you only need to do this as a list and ensure that the names are associated with the correct codes.

I also understood by the title you want to order the bars in the order of REF. For this you can use reorder.

As you have not provided your complete data set, I am creating some simpler data.

# Dados
dataset <- structure(list(REF = structure(c(3L, 5L, 3L, 1L, 3L, 3L, 3L,
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L), .Label = c("BRE", "GAB",
"HID", "RIU", "UNK"), class = "factor"), COLOR = structure(c(3L,
2L, 3L, 4L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L
), .Label = c("#01665e", "#4d4d4d", "#80cdc1", "#bf812d", "#f6e8c3"
), class = "factor")), class = "data.frame", row.names = c(NA,
-17L))
set.seed(99)
dataset$UNIDADES <- LETTERS[1:nrow(dataset)]
dataset$var <- sample(1:100, nrow(dataset))
dataset$peso <- sample(1:100, nrow(dataset))
#------------------------------------------------------------

library(ggplot2)

cores <- setNames(
  as.list(as.character(unique(dataset$COLOR))),
  unique(dataset$REF))

ggplot(dataset, aes(reorder(UNIDADES, as.numeric(REF)), var)) +
  geom_tile(aes(height = peso, fill = REF), width = .6) +
  theme_classic() +
  labs(x = "UNIDADES", y = "TOTAL") +
  scale_fill_manual(values = cores)

inserir a descrição da imagem aqui

  • Thanks for the help @Carlos Eduardo Lagosta, but in my dataset there can be equal colors for different references so the setNames function hangs on the same problem for a solution that had thought, concatenate REF and COLOR in a vector.

  • Edit your question to include this detail, along with a sample complete of your data. Your question should be self sufficient; users should not read all comments to know what they need.

  • Yeah, I get it. At the time I asked the question, color repetition was not a possibility, but as the database is fed daily this unforeseen arose. I will edit the question to make it clearer and complete, but it is already solved.

2


Apparently one of the problems is the possibility of having equal colors for different references. The function fun below solves this problem by creating a color array in which the names are unique values of ref and the values of cor correspondent.

fun <- function(ref, cor){
  cor <- as.character(cor)
  x <- unique(ref)
  i <- match(x, ref)
  setNames(cor[i], ref[i])
}

To test the function, a data set is created dataset2 with two new REF, of values BRC and BRD. The colors are those of BRE.

dataset2 <- rbind(dataset, data.frame(REF = c("BRC", "BRD"), COLOR = rep("#bf812d", 2)))
dataset2$REF <- factor(as.character(dataset2$REF))

Now assign colors using the function fun.

cores <- with(dataset2, fun(REF, COLOR))

And the rest of the code is the same as the Reply by @Carlos Eduardo Lagosta, only changes the basis.

The chart will be as follows, note the repeated color.

inserir a descrição da imagem aqui


Link code above.

set.seed(99)
dataset2$UNIDADES <- LETTERS[1:nrow(dataset2)]
dataset2$var <- sample(1:100, nrow(dataset2))
dataset2$peso <- sample(1:100, nrow(dataset2))

library(ggplot2)

ggplot(dataset2, aes(reorder(UNIDADES, as.numeric(REF)), var)) +
  geom_tile(aes(height = peso, fill = REF), width = .6) +
  theme_classic() +
  labs(x = "UNIDADES", y = "TOTAL") +
  scale_fill_manual(values = cores)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.