Color scale adjustment in R

5

Good afternoon,

I created a bar chart using a gauge to fill in the colors of the bars, where the nearest to red is bad, and the closer to green is good. It turns out that he adjusted according to the data, so the number 29 turned green when it's a very bad value. I would like to manually adjust the range that will adjust the colors. How to make? Thanks!

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)
    
asked by anonymous 16.11.2018 / 20:31

2 answers

1

Use ..count.. as parameter for the fill attribute of the ggplot() function, as the example below:

ggplot(df) + geom_bar(aes(year,fill=..count..))

Source: link

    
16.11.2018 / 21:16
1

First, I've created a database that looks similar to yours:

# 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 way to find a color palette, and do it automatically is via the RColorBrewer package. When you run the display.brewer.all() function you will see different color palettes that you can use. Another option is to use their website colorbrewer2 . In this case, the palette that works for you 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)

Ifyouusedirection=-1,thecolorswillappearinreverseorder.Thelimitationofthispaletteisthatitdisplaysamaximumof11differentcolors.

Alternatively,usecolorRampPaletteandputthenumberofcolorsyouwantinthefunctionyoucreate,thiswouldbethe"manual" mode. In this case, I left it according to 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)))

    
21.11.2018 / 12:40