Using element_ Textcustomize text in ggplot2

Ggplot2’s theme system allows us to better control graphicsNon data elementTo enhance the beauty of the image through more subtle modifications,Theme system of ggplot2Self contained multipleelement_function

  • element_text( )
  • element_line( )
  • element_rect( )
  • element_blank( )

This section describes the topic element element_ Text (), which controls many parts of text elements in a drawing, such as font size, color, and font type.

Element of ggplot2_ Text() parsing

Using element_ Textcustomize text in ggplot2

element_ List of elements controlled by text()

  • axis. title. x: Custom X-axis label / Title
  • axis. title. Y: Custom Y-axis label / Title
  • axis. text. X: Custom X-axis scale labels
  • axis. text. Y: Custom Y-axis scale labels
  • legend. Title: custom legend title text
  • legend. Text: Custom legend text
  • plot. Title: custom image main title
  • plot. Subtitle: custom image subtitle
  • plot. Caption: custom image footnotes
  • plot. Tag: custom drawing labels

Load R package

library(tidyverse)
library(palmerpenguins)

Still use the penguin data set. Next, use element_ Text() function to adjust the text element of the image

p<- penguins %>%
  drop_na() %>%
  ggplot(aes(x=flipper_length_mm,
             y=bill_length_mm, 
             color=species,
             shape=sex))+
  geom_point()+
  labs(title="Palmer Penguins",
       subtitle="Flipper Length vs Bill Length",
       caption="cmdlinetips.com",
       tag = 'A'
  )

p
Using element_ Textcustomize text in ggplot2

1. axis. title.* (): Customx&yLabel text

Through element_ Text() to change the text, color, size and angle

p + theme(axis.title.x = element_text(size=16, color="purple", 
                                      face="bold",angle=0),
          axis.title.y = element_text(size=16, color="purple", 
                                      face="bold",angle=90))
Using element_ Textcustomize text in ggplot2

2. axis. text.* () customx&yScale text

p + theme(axis.text.x=element_text(family = "Tahoma",face="bold",
                                   colour="black",size=10),
          axis.text.y = element_text(family = "Tahoma",face="bold",
                                     colour="black",size=10))
Using element_ Textcustomize text in ggplot2

3. legend. Title() custom legend title text

p + theme(legend.title=element_text(color="purple",
                                    face="bold",size=12))
Using element_ Textcustomize text in ggplot2

4. legend. Text() custom legend text

p + theme(legend.text=element_text(face="bold", color="red",size=10))
Using element_ Textcustomize text in ggplot2

5. plot. Title() custom main title

p + theme(plot.title= element_text(size=15,color="blue",hjust = 0.5,
                                   face="bold",family = "Tahoma"))
Using element_ Textcustomize text in ggplot2

6. plot. Subtitle() custom subtitle

p + theme(plot.subtitle= element_text(size=13,
                                      color="red",
                                      face="bold"))
Using element_ Textcustomize text in ggplot2

7. plot. Caption() custom footnote

p + theme(plot.caption= element_text(size=12,
                                     color="blue",
                                     face="bold"))
Using element_ Textcustomize text in ggplot2

8. plot. Tag() custom label

p + theme(plot.tag = element_text(size=16,
                                   color="red",
                                   face="bold"))
Using element_ Textcustomize text in ggplot2

Welcome to my official account

The R language data analysis guide continuously shares the classic cases of data visualization and some students’ knowledge, hoping to help you

Read more here: Source link