Color scale adjustment on R

Asked

Viewed 1,147 times

5

Good afternoon,

I created a bar chart using an indicator to fill the colors of the bars, where the nearest red is bad, and the nearest green is good. It turns out that it adjusted according to the data, so that the number 29 turned green, when it is a very bad value. I would like to manually adjust the range that will adjust the colors. How to do? Thank you!

Gráfico com a escala desajustada

graf_pecas_tipo <- dados_expandido %>% 
    filter(hierarquia0 == "PEÇAS + ACESSORIOS") %>% 
      group_by(hierarquia3) %>% 
       summarise(Qtd_busc = n(),
                Qtd_vend = sum(ind_venda == "S"),
                Tx_Conv = round(Qtd_vend / Qtd_busc * 100,2),
                Clie_dt = n_distinct(cpf_cliente),
                Preco_Med_vend = round(mean(valor_unitario_sem_acrescimo[ind_venda == "S"]),2),

            Preco_Med_sem_vend = round(mean(valor_unitario_sem_acrescimo[ind_venda == "N"]),2),
            Desc_Med = round(mean(desconto, na.rm = T)),2) %>% 

  arrange(desc(Qtd_busc))%>% 
  top_n(30, Qtd_busc) %>% 
  mutate(desc_produto = reorder(hierarquia3, - Qtd_busc),
         Tx_Conv = as.integer(Tx_Conv))%>% #ordenando as barras do gráfico

  ggplot(aes(x = desc_produto, y = Qtd_busc)) +
  geom_col(aes(fill = Tx_Conv)) +
  geom_text(aes(label = Tx_Conv, vjust = -0.5))+
  scale_fill_distiller(type = "div", palette = "RdYlGn", direction = 1) +
  labs(x = "Tipo de Peça",
       y = "Quantidade de Orçamentos",
       title = "Quantidade de buscas e Taxa de Conversão por Tipo de Peça",
       subtitle = NULL,
       fill = "Taxa de Conversão")+
  theme_bw()+
  theme(axis.text.x=element_text(angle = 45, hjust = 1))


ggplotly(graf_pecas_tipo)
  • Can you please, edit the question with the departure of dput(base)? (Or df, whatever the dataframe is called. ) Note: in the instruction ggplot the first argument is data and that argument is missing from your code.

  • Please, if the problem is in ggplot, edit the question with the result of last mutate for a dataframe. Anything like mutate(...) -> pecas_acess. And then ggplot(pecas_acess, aes(x = desc_produto, y = Qtd_busc)) +. In the end the one dput(pecas_acess). Or whatever you like. I ask this because this way we can focus on the graph and not the data processing until the plotting terms.

  • 1

    How much is a good value? How is your data? What is the scale of good/bad? What you want isn’t particularly difficult to do in ggplot, but we can’t respond properly without a sample of your data and a good idea of what you expect as the end result.

  • I am trying to extract the data but I am not cocnseguindo.. the ideal range of the scale whatever, the important thing is to know how to adjust it manually. we can work on the hypothesis of an inteval between 0 and 50.

2 answers

1

First, I created a database that resembles your:

# Criando conjunto de dados
df <- data.frame(Var1 = 9,
   Var2 = 5,
   Var3 = 7,
   Var4 = 4,
   Var6 = 9,
   Var7 = 8)
long <- reshape2::melt(df)
long
  variable value
1     Var1     9
2     Var2     5
3     Var3     7
4     Var4     4
5     Var6     9
6     Var7     8

One of the ways to find a color palette, and do it automatically is via the package RColorBrewer. When performing the function display.brewer.all() you will see different color palettes that you can use. Another option is to use their website colorbrewer2. In this case, the palette that serves in your case is "Rdylgn":

library(ggplot2)
library(RColorBrewer)
ggplot(long, x = variable, y = value) +
  geom_col(aes(x = variable, y = value, fill = variable)) +
  scale_y_continuous(labels = 0:10, breaks = 0:10) +
  geom_text(aes(label = value, x = variable, y = value)) +
  scale_fill_brewer(palette = "RdYlGn", direction = 1)

inserir a descrição da imagem aqui

If you use direction = -1, colors will appear in reverse order. The limitation of this palette is that it displays at most 11 different colors.

Alternatively, use colorRampPalette and put the number of colors you want in the function you create, this would be the "manual" mode. In this case, I left as the number of lines:

colfunc <- colorRampPalette(c("red", "green"))
ggplot(long, x = variable, y = value) +
  geom_col(aes(x = variable, y = value, fill = variable)) +
  scale_y_continuous(labels = 0:10, breaks = 0:10) +
  scale_fill_manual(values = colfunc(nrow(long)))

inserir a descrição da imagem aqui

1

  • but what I need is the opposite actually. my scale is adjusted according to the data, but I would like to manually set it.

Browser other questions tagged

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