Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Two text labels (one for bar and one for line)

$
0
0

I have the following graph, I want to display percentage along side the line and numbers along side the bar & also text labels above each bar. How do I achieve this?

enter image description here

Below is the code (R Shiny)

ggplot(df_leakage_subset(),aes_string(x=input$cut_selection, y="count_policies",fill="YTD_filter"))+
  geom_bar(stat="identity", position="dodge")+
  geom_label_repel(aes_q(label=~(paste0(count_policies,"(",round(perc_policies/800,2),"%)"))),color="black",fill='white',size=3)+
  geom_line(aes_string(x=input$cut_selection, y="perc_policies",group="YTD_filter",color="YTD_filter"),size=1.5)+
  scale_y_continuous(
    sec.axis = sec_axis(~./8000, name="Percentage")
  )+
  theme(
    axis.title.y = element_text(color = "black"),
    axis.title.y.right = element_text(color = "blue"),
    axis.text.x = element_text(angle = 90, hjust = 1))+
  xlab(input$cut_selection)+
  ylab("Number of policies")

As the above is coded in RShsiny I have provided below a subset of data which feeds into ggplot function

structure(list(Channel = c("APC", "APC", "DM", "DM", "DSF", "DSF", 
"HDFC Bank", "HDFC Bank", "TPD", "TPD"), YTD_filter = c("FY_6_2019", 
"FY_6_2020", "FY_6_2019", "FY_6_2020", "FY_6_2019", "FY_6_2020", 
"FY_6_2019", "FY_6_2020", "FY_6_2019", "FY_6_2020"), total_policies = c(2301L, 
2577L, 2849L, 2208L, 34433L, 30185L, 4481L, 15319L, 6114L, 6025L
), total_premium = c(101888149.41, 117407955.18, 164657579.4, 
115295156.56, 1452258950.86, 1571286319.33, 504515794.01, 1406433296.62, 
442228231.65, 349953011.47), count_policies = c(319L, 319L, 318L, 
318L, 3069L, 3069L, 241L, 241L, 472L, 472L), sum_premium = c(20886558.32, 
20886558.32, 16606803.14, 16606803.14, 151868936.4, 151868936.4, 
25747580.75, 25747580.75, 21809165.91, 21809165.91), perc_policies = c(0.138635375923512, 
0.123787349631354, 0.111618111618112, 0.144021739130435, 0.0891296140330497, 
0.101673016398874, 0.0537826378040616, 0.0157320973953913, 0.0771998691527642, 
0.0783402489626556), perc_premium = c(0.204994971848513, 0.177897300808778, 
0.100856597069591, 0.144037300746088, 0.104574281542604, 0.0966526180058369, 
0.0510342412580441, 0.0183070045425387, 0.0493165391739639, 0.0623202692795504
)), class = "data.frame", row.names = c(NA, -10L))

You can replace input$selection with Channel

Any help is appreciated!


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>