Home > database >  Need to recreate the given chart. How can I overlap the trend line and also highlight GoDigit?
Need to recreate the given chart. How can I overlap the trend line and also highlight GoDigit?

Time:11-17

What I've generated so far..... [What i've generated so far(https://img.codepudding.com/202211/6fd9fa22ec8b49359aaec58f5f3ee0e7.jpg)

VERSUS

What needs to be recreated [the original plot(https://img.codepudding.com/202211/0be04e2a925641d4b1f4cbe10557051f.png)

my code so far:

Recreated_figure_DHRP %>% 
  ggplot(aes(x = Insurers, y =`INR BN`,fill = FY,group=FY))  
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75,) 
  theme(axis.text.x = element_text(angle = 60, hjust = 1))  
  theme(legend.position="top")  
  ylim(0,400) 
  geom_text(aes(label = `INR BN`), fontface = "bold", vjust = 1.5,
            position = position_dodge(.9), size = 2.25)

CodePudding user response:

Additional data can be added with suitable geom_XYZ functions, overwriting the aes values that change from the ggplot call. So for this case adding the line is as follows:

Recreated_figure_DHRP <- read.csv("C:\Users\te01\Downloads\Dataframe - sheet1.csv")

Recreated_figure_DHRP %>% 
  ggplot(aes(x = Insurers, y =`INR.BN`,fill = FY,group=FY))  
  geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75,) 
  theme(axis.text.x = element_text(angle = 60, hjust = 1))  
  theme(legend.position="top")  
  ylim(0,400) 
  geom_text(aes(label = `INR.BN`), fontface = "bold", vjust = 1.5,
            position = position_dodge(.9), size = 2.25)   
  geom_line(aes(y=`YOY.Growth`), col="red")

(note very slight differences in the dataframe column names)

That gives:

Output plot

CodePudding user response:

You can use geom_rect function to highlight the specific regions of the plot.

  Recreated_figure_DHRP %>% 
    ggplot(aes(x = Insurers, y =`INR.BN`,fill = FY,group=FY))  
    geom_bar(stat = "identity", position = position_dodge(), alpha = 0.75) 
    geom_rect(data = Recreated_figure_DHRP %>% filter(Insurers == "GoDigit"),
              aes(xmin = Insurers, 
                  xmax = Insurers,
                  ymin = 0,
                  ymax = max(`INR.BN`)),
              color = "red",
              fill = NA,
              linetype = 2,#dashed line
              alpha = 0.5,
              show. Legend = FALSE)
     geom_text(aes(label = `INR.BN`), 
        fontface = "bold", 
        vjust = 1.5,
        position = position_dodge(.9), 
        size = 2.25)   
     geom_line(aes(y=`YOY.Growth`), col="red") 
     ylim(0,400) 
     theme(axis.text.x = element_text(angle = 60, hjust = 1),
           legend. Position="top")
  • Related