0

I would like to add an arrow after the text in xlab using ggplot2. Here is an example of a code below where I try to do it but adding dotted lines and signs but it is not very aesthetic.

ggplot(data=Q2, aes(x=month, y=dataQ)) +xlab("Mois     ------------------------------------------->") + ylab("Pr(mm)") +geom_bar(stat="identity", position = "dodge") + scale_x_discrete(limits=c("JAN", "FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"))+theme(plot.title = element_text(color="black", size=14, face="bold.italic"),axis.line = element_line(colour = "black", size = 1, linetype = "solid"),
                                                                                                                                                                                                                                                                                                                                              axis.text.x = element_text(face="bold", color=" black",size=11, angle=0),axis.text.y = element_text(face="bold", color="black",size=11, angle=90),axis.title.x = element_text(color="black", size=14, face="bold"),
                                                                                                                                                                                                                                                                                                                                              axis.title.y = element_text(color="black", size=14, face="bold") 
)

Here is the graph I get enter image description here

Falltok
  • 11
  • 3
  • 1
    You could use the Unicode right arrow: `+ labs(x=paste("Mois", "\U2192"))` – eipi10 Jan 23 '20 at 17:43
  • There's also a longer Unicode arrow but I found that it didn't display with the default font. I was able to get the arrow to display properly with the Symbola font (which you may need to install on your system if you don't already have it). `+ labs(x=paste("Mois", "\U27f6")) + theme(text=element_text(family="Symbola"))` – eipi10 Jan 23 '20 at 18:46
  • @eipi10 I downloaded and installed the "Symbola" font but when I run the code that you propose above I get the following error _"In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), ... : font family not found in Windows font database"_ – Falltok Jan 24 '20 at 13:54
  • Assuming you've installed the Symbola font on your system, another thing to try is the `extrafont` package. [Follow the instructions here](https://github.com/wch/extrafont) to import and register all available fonts for use with R. – eipi10 Jan 24 '20 at 18:37

1 Answers1

1

You can use annotate and clip = "off", like below:

library(ggplot2)

ggplot() + 
  coord_cartesian(ylim = c(0, 1), xlim = c(0, 1), clip="off") +
  xlab("x label") +
  ylab("y label") +
  annotate("segment", 
           x = 0.6, xend = 0.9, y = -0.11, yend = -0.11,
           arrow=arrow(length=unit(0.3, "cm")))

Created on 2020-01-23 by the reprex package (v0.3.0)

Dan
  • 11,370
  • 4
  • 43
  • 68
  • I tested your proposal but the arrow does not come outside the axes dial! – Falltok Jan 24 '20 at 13:57
  • @Falltok If you run the code exactly as it is given (i.e., copy-and-paste), does it give an arrow as shown? The problem is that you don't provide a [minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example), so I can't show you in the context of your exact situation. – Dan Jan 24 '20 at 15:00
  • `ggplot(data=Q2, aes(x=month, y=dataQ)) + labs(x=paste("Mois")) + ylab("Pr(mm)") +geom_bar(stat="identity", position = "dodge") + coord_cartesian(ylim = c(0,250), xlim = c(0, 12), clip="off") +annotate("segment", x =1, xend = 6, y = -7, yend = -7,arrow=arrow(length=unit(0.3, "cm")))+scale_x_discrete(limits=c("JAN", "FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"))` – Falltok Jan 27 '20 at 10:59
  • Above the code I use, the arrow appears as shown but it does not go down to the title, it stays in above the axes! – Falltok Jan 27 '20 at 11:05
  • @Falltok You change the position of the arrow with the parameters passed to the `annotate` function. – Dan Jan 27 '20 at 13:24