Home > Back-end >  How to customize dual axes barchart?
How to customize dual axes barchart?

Time:06-04

This is a follow-up question of this enter image description here

CodePudding user response:

The order of model by cost can be done by setting the levels of the factor. Changing geom_text to geom_label created boxes instead of purely displaying the text. I am not sure what you mean with ". Specify the colour of every bars manually myself". Do you mean the fill? The color is now determined by your factor and can be changed by scale_fill_manual, but you already do that.

#data
df <- data.frame (model  = c("A", "B", "C","D","E","F"),
                  share = c(12,20,15,9,60,20),
                  sale = c(16,25,18,14,67,28),
                  cost = c(14,19,28,24,57,28))

#set levels of model by cost
df$model <- factor(df$model, levels = arrange(df, desc(df$cost))$model)

#set to long format
df_long <- df %>% 
  pivot_longer(
    cols = -model
  )

#plot (changed geom_text to geom_label)
ggplot(df_long, aes(x = model, y= value, label=value)) 
  geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge()) 
  scale_fill_manual(values = c("blue", "grey")) 
  geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 0.5) 
  scale_color_manual(values = "red") 
  geom_point(data = filter(df_long, name == "cost"), size = 2) 
  geom_label(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5) 
  scale_y_continuous(
    name = "Sale and Share",
    sec.axis = sec_axis(~., name ="Cost")
  ) 
  theme_minimal() 
  theme(legend.title=element_blank())

image

CodePudding user response:

Update: Define a vector with colors you like and then use scale_fill_identity:


colors <- c("black","blue","black","green","black","orange",
            "black","pink","black", "yellow", "black","green")

library(tidyverse)

df_long <- df %>% 
  pivot_longer(
    cols = -model
  ) 


ggplot(df_long, aes(x = fct_reorder(model,desc(value)), y= value, label=value)) 
  geom_col(data = filter(df_long, name != "cost"), aes(fill=colors), position = position_dodge()) 
  scale_fill_identity() 
  geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 1) 
  scale_color_manual(values = "red") 
  geom_point(data = filter(df_long, name == "cost"), size = 2) 
  geom_label(data = filter(df_long, name == "cost"), hjust=0, vjust=-1.5) 
  scale_y_continuous(
    name = "Sale and Share",
    sec.axis = sec_axis(~., name ="Cost")
  ) 
  theme_minimal() 
  theme(legend.title=element_blank())

enter image description here

First answer: We could use fct_reorder from forcats package it is in tidyverse

library(tidyverse)

df_long <- df %>% 
  pivot_longer(
    cols = -model
  ) 


ggplot(df_long, aes(x = fct_reorder(model, desc(value)), y= value, label=value)) 
  geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge()) 
  scale_fill_manual(values = c("blue", "grey")) 
  geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 1) 
  scale_color_manual(values = "red") 
  geom_point(data = filter(df_long, name == "cost"), size = 2) 
  geom_label(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5) 
  scale_y_continuous(
    name = "Sale and Share",
    sec.axis = sec_axis(~., name ="Cost")
  ) 
  theme_minimal() 
  theme(legend.title=element_blank())

enter image description here

  • Related