I like to add the following caption to my graph:
Note: Market concentration averages in the United States, United Kingdom, and the Netherlands are, respectively, 1920, 1388, and 1244
Where 'Note:' needs to be italicized, and 'Netherlands are, respectively, 1920, 1388, and 1244' should be placed on a new line.
Using the paste
function, I can italize a part. But using the \n
within paste
, mixes every thing up as you can see here (this is an edited image, made using Paul's advice below):
I tried various other solutions, but without succes. Here is the code I am using:
library(ggplot2)
note = expression(paste(italic("Note: "), "Market concentration averages in the United States, United Kingdom, and the \nNetherlands are, respectively, 1920, 1388, and 1244"))
gg <- ggplot(mtcars, aes(wt, mpg)) + geom_point()+
# Title
labs(caption=note)
gg + theme(plot.caption=element_text(size=7.5, hjust=0, margin=margin(t=15)))
ggp + # Add italic text element to plot annotate ("text", x = 4.5, y = 2.2, size = 5 , label = "My Italic Text" , fontface = "italic") Figure 3 shows the output of the previous R syntax: A ggplot2 plot with italic text.
First, I’ll show how to annotate text in one line to a ggplot2 scatterplot using the annotate function: ggp + # Add text in one line annotate ("text" , x = 4.5 , y = 6 , label = "This is my text!") Figure 2 illustrates the output of the previous R syntax – A ggplot2 plot with one line of text.
Have a look at the following R code: ggp + # Add text in multiple lines annotate ("text" , x = 4.5 , y = 6 , label = "This is my text!") In Figure 3 you can see that we have created a ggplot2 graph containing a text element with several lines. Do you need further information on the topics of this article?
In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. This is needed when we want to highlight the title by making it different. In ggplot2, we can do this by using expression.
Based on this answer,
library(grid)
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_line() +
labs(caption= "First~line \n italic('and a second,')~bold('fancy one') \n
'also,'~integral(f(x)*dx, a, b)~'for good measure'")+
(theme_grey() %+replace% theme(plot.caption = element_custom()))
element_custom <- function() {
structure(list(), class = c("element_custom", "element_text"))
}
element_grob.element_custom <- function(element, label="", ...) {
disect <- strsplit(label, "\\n")[[1]]
labels <- lapply(disect, function(x) tryCatch(parse(text=x),
error = function(e) x))
hl <- unit(rep(1, length(labels)), 'strheight', data=labels) + unit(0.1,"line")
yl <- c(list(unit(0,"line")),
lapply(seq_along(labels[-length(labels)]), function(ii) sum(hl[1:ii])))
cl <- do.call(gList, Map(function(label, ii)
textGrob(label, y = unit(1,"npc")-yl[[ii]], hjust=0, x=0, vjust=1),
label = labels, ii = seq_along(labels)))
gTree(children = cl, cl="sl", heights = hl, gp=gpar(col="grey50",fontsize=8))
}
heightDetails.sl <- function(x) sum(x$heights)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With