wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
651 stars 37 forks source link

Title and subtitle are not working on ggplot2 #45

Closed abalvarenga closed 4 years ago

abalvarenga commented 4 years ago

Hello:

I have using ggtext to edit my ggplot titles. However, today it is not working. No error messages appear, just the command description in the plot.

I.e.

library(ggplot2)
library(wesanderson)
library(ggtext)

ggplot(yearling,aes(x = Generation, y= K, group= Gender, 
                color=Gender, linetype=Gender,shape=Gender ))+
  geom_line()+geom_point()+
  scale_color_manual(values=c("firebrick1","turquoise3"))+
  scale_linetype_discrete()+
  labs(title = "<b>A. Yearling cattle temperament: percentage of known parents per generation</b><br>
        <span style = 'font-size:13pt'>The color and shape represent <b><span style = 'color:firebrick1;'>female</span></b> and <b><span style = 'color:turquoise3;'>male</span></b></span>."
       , x = "Generations", y = "% of pedigree completeness")+
  theme_classic()+ theme(axis.text.x = element_text(angle = 90, hjust = 1))

The outcome: Rplot

I appreciate your help, Thank you!

thomas-neitmann commented 4 years ago

Please have a look at the documentation. You seem to lack a basic understanding of how {ggtext} works. You need to set the title and subtitle theme elements to element_markdown(), i.e.

library(ggplot2)
library(wesanderson)
library(ggtext)

ggplot(yearling,aes(x = Generation, y= K, group= Gender, 
                color=Gender, linetype=Gender,shape=Gender ))+
  geom_line() +
  geom_point() +

 scale_color_manual(values=c("firebrick1","turquoise3"))+
  scale_linetype_discrete()+
  labs(title = "<b>A. Yearling cattle temperament: percentage of known parents per generation</b><br>
        <span style = 'font-size:13pt'>The color and shape represent <b><span style = 'color:firebrick1;'>female</span></b> and <b><span style = 'color:turquoise3;'>male</span></b></span>."
       , x = "Generations", y = "% of pedigree completeness") +
  theme_classic() +
  theme(
    axis.text.x = element_text(angle = 90, hjust = 1), 
    title = element_markdown(), 
    subtitle = element_markdown()
  )
mariemoinet commented 3 years ago

I ran in a similar issue with the title, and to help others who would run in the same issue: The code given by @thomas-neitmann and especially theme(title = element_markdown()) wouldn't work for me, and I eventually figured that contrary to him, I had to use labs (title ="something in *italics*")+ theme(plot.title = element_markdown())