0

What I've generated so far..... [What i've generated so far(https://i.stack.imgur.com/0G8j5.jpg)

VERSUS

What needs to be recreated [the original plot(https://i.stack.imgur.com/kVmZx.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)
user438383
  • 5,716
  • 8
  • 28
  • 43
Jimi13 ABC
  • 19
  • 7
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Nov 11 '22 at 14:37
  • how about I just attach the file itself....the data frame isn't huge either – Jimi13 ABC Nov 11 '22 at 14:43
  • 1
    You can't attach files to Stack Overflow posts. The link I provided shows many ways to include data in your question. The most useful is generally a `dput()`. If the dataset is large, usually a subset is sufficient just make sure it reproduces the issue you are having. – MrFlick Nov 11 '22 at 14:46
  • here's a link to the spreadhseet. https://docs.google.com/spreadsheets/d/1keKE-_rYkFG_GrR5cxezlTcKxdUbKOdSRx6IUhiI4cw/edit?usp=sharing. Thank you for taking the trouble! – Jimi13 ABC Nov 11 '22 at 14:56
  • 1
    Use `+ geom_line(aes(x=..., y=....), data=df)` where `df` holds the values for the line. – January Nov 11 '22 at 15:20

2 Answers2

0

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

Miff
  • 7,486
  • 20
  • 20
0

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")
Selcuk
  • 86
  • 4