Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change geom_text to bold when parse=TRUE

I am annotating faceted plots to include a superscript, yet am having trouble making the text bold. I realise that this has something to do with creating an expression outside the plot call and then specifying parse=TRUE. There is probably quite a simple solution but so far nothing I have tried has worked, including the use of bquote() and bold(). Thanks

library(ggplot2)

data(iris)

rsq<-c(.3,.6,.75)
pos<-c(5,6,7)
Species<-levels(iris$Species)

big_data<-as.data.frame(cbind(pos, rsq))
big_data$Species<-Species

lab <- paste("r^2 == ", round(big_data$rsq,2))

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  facet_wrap(~Species,scales='free_x')+
  geom_point(size=3,show.legend = F) +
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,fontface='bold')

enter image description here

like image 474
J.Con Avatar asked May 02 '17 00:05

J.Con


People also ask

How do I make text bold in R?

To make the formatted text into bold type, you can simply use a pair of ** around the marked up text with no space. For example **bold** in the . Rmd file generates bold in the output document.

What does geom_ text do?

Text geoms are useful for labeling plots. They can be used by themselves as scatterplots or in combination with other geoms, for example, for labeling points or for annotating the height of bars. geom_text() adds only text to the plot. geom_label() draws a rectangle behind the text, making it easier to read.


3 Answers

you can get r to be bold()

lab <- sprintf("bold(r)^2 == %.2f", big_data$rsq)

but that's about it. From ?plotmath

Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants.

Your best bet for finer typography might be tikzDevice.

like image 132
baptiste Avatar answered Oct 24 '22 02:10

baptiste


I know this is big time excavating, but for those who stumble upon this post like me, you can use ggtext to get the formatting you like:

library(ggplot2)
library(ggtext)
big_data <- data.frame(
    pos = 5:7, 
    Species = levels(iris$Species),
    lab = paste0("<b>r<sup>2</sup> = ", sprintf("%.2f", c(.3,.6,.75), 2), "</b>")) 
ggplot(iris, aes(x=Sepal.Length, y = Sepal.Width)) +
    facet_wrap(~Species, scales='free_x')+
    geom_point(size = 3, show.legend = FALSE) +
    geom_richtext(data = big_data, aes(x = pos, y = 1, label = lab),
        fill = NA, label.color = NA,
        label.padding = grid::unit(rep(0, 4), "pt")
    )

Created on 2021-01-25 by the reprex package (v0.3.0)

like image 4
user12728748 Avatar answered Oct 24 '22 02:10

user12728748


A little cheat - over plotting the text 3 times with slight increases in size.

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  facet_wrap(~Species,scales='free_x')+
  geom_point(size=3,show.legend = F) +
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4)+
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4.07)+
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4.08)

enter image description here

like image 3
J.Con Avatar answered Oct 24 '22 00:10

J.Con