0
votes

I am trying to make a combo chart using ggplot2. However i want to add a text box sort outside my plot body. I am unable to place it at the desired location

I have used grid pack to create grob and include that in annotation in the ggplot code. Additionally i have also put the same text in geom_text. How do i ensure the text comes say below the legend. Following is my code

m <- ggplot() +
  geom_area(data= (ly_vol_ntwk %>%
                     mutate(Wk_end_d = as.factor(Wk_end_d))%>%
                     filter(!is.na(value_new))),
            aes(x = Wk_end_d, y = value_new ,group = variable,fill=variable))+ 
  geom_bar(data = (fcst_act_vol_ntwk %>% 
                     mutate(Wk_end_d = as.factor(Wk_end_d))%>%
                     filter(!is.na(value_new))),
           aes(x = Wk_end_d, y = value_new, group = variable, fill = variable), 
           stat = "identity",position = "dodge", width =0.5)+
  geom_line(data = (var_vol_ntwk %>% 
                      mutate(Wk_end_d = as.factor(Wk_end_d))%>%
                      filter(!is.na(value_new))),
            aes(x = Wk_end_d, y = value_new, 
                group = variable, fill= variable), size = 0.8)+ 
  scale_y_continuous(sec.axis = sec_axis(trans = ~./100000, 
                                         name = "Variance", breaks = waiver(),
                                         labels=function(x) paste0(x,"%")))+ 
  theme_set(theme_bw())+ 
  theme(axis.text.x = element_text(angle=65, vjust=0.5,face = "plain"),
        text = element_text(size=9), legend.position = "bottom", legend.title = element_blank())+ 
  labs(title= "Inbound - Network", x= "Week end date", y = " ")+ 
  scale_fill_manual(values = c("#C5E0B4","#7030A0", "#D9D9D9","#ED7D31","black"))+ 
  geom_text(label = "LW Variance",
            aes(x = 19, y = -1960000),
            check_overlap = TRUE) #annotation_custom(grob = textGrob("LW Variance"), xmin = 18, xmax = 18, ymin = -1030000, ymax = -1030000)+ coord_cartesian(clip = 'off')

I need to get the text box with a border outside the area of the ggplot. Can you please help me?

2
Do captions produce the effect you want? p + labs(caption = "Your text here"). You can control it further with theme(plot.caption = element_text(...)) - Gabriel Silva
what do i need to mention in the element_text() part in plot.caption? How can i say it to appear below the legend or say at the top , outside the plot? - rhitima
Your code is not reproducible, there is no data. Furthermore, the code example should be minimal, and if all you want is to display text outside the plot area, you should simplify the example. - Rui Barradas
@rhitima Check my answer for some alternatives - Gabriel Silva
Possible duplicate of ggplot2 - annotate outside of plot - tjebo

2 Answers

2
votes

You can place text below plot area with labs(caption = "text"), but you can't place captions on top of the plot. However, you could use subtitles labs(subtitle = "text") to produce a similar visual of captions on the top.

To further control the aspect of both options use theme(plot.caption = element_text(...), plot.subtitle = element_text(...)). Type ?element_text in your console to get all the options for text formatting.

For example:

library(ggplot2)

df <- data.frame(x = rnorm(50), y = rnorm(50))

ggplot(df, aes(x, y)) +
  geom_point() +
  labs(subtitle = "Your text here", caption = "Your text here") +
  theme(plot.caption = element_text(colour = "red", hjust = 0, angle = 15),
        plot.subtitle = element_text(size = 18, face = "bold", hjust = 0.8))

enter image description here

2
votes

If you want it below your current legend, you can always add a dummy legend and put your text as its name. An example:

ggplot(mtcars, aes(mpg, wt, color = gear,fill = "a")) + 
  geom_point() + 
  scale_fill_discrete(name = "Your custom caption\ngoes here", labels = "") + 
  theme(legend.key = element_rect(fill = "white")) +
  guides(color = guide_legend(order = 1),
      fill = guide_legend(order = 2, override.aes = list(linetype = 0, shape=NA))) # setting the order parameter in guide_legend will help place it below your existing legend(s)