Ggplot2 those things that decorate the legend

Time:2022-6-25

It is often necessary to fine adjust the picture in the drawing, especially the adjustment of the legend; This section provides details on adjusting the legend

Install R package

install{tidyverse}, if yestidyverseDo not understand please check the previous periodTidyverse collection

package.list=c("tidyverse")

for (package in package.list) {
  if (!require(package,character.only=T, quietly=T)) {
    install.packages(package)
    library(package, character.only=T)
  }
}

Note: the above code will download and load the R package at the same time

Still use the familiaririsData set

iris %>% as_tibble()
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 

according toirisData set plotting boxplot

p <- iris %>% pivot_longer(-Species) %>% 
  mutate(name=as.factor(name)) %>% 
  ggplot(aes(name,value,fill=Species))+
  stat_boxplot(geom="errorbar",
               position=position_dodge(width=0.8),width=0.2)+
  geom_boxplot(position=position_dodge(width =0.8))+
  labs(caption = "2021-8-26")+
  Theme (panel.grid.major=element\u blank(), \
        panel. grid. minor=element_ Blank(), \\
        panel. background = element_ Blank(), \
        axis. title. x=element_ Blank(), \\x title is set to null
        axis. title. y=element_ Blank(), \\y title is set to null
        axis. text. x=element_ Text (color= "blue", margin = margin (t = 5)), \
        axis. text. y=element_ Text (color= "black", margin = margin (r = 5)), \
        axis. ticks. x=element_ Line (color= "red", size=2), \
        axis. ticks. length. X = unit (-.2, "cm"), \
        panel. border = element_ Rect (linetype = "dashed", fill = Na), \\
        axis. line. x.bottom = element_ Line (linetype= "solid"), \x-axis line type is defined
        plot. Margin=unit (C (0.5,0.5,0.5,0.5), units=, "cm"), \
        plot. caption = element_ Text (size = 12, face = "Italic", \
          color = "#606F7B", margin = margin(t = 12)))
Ggplot2 those things that decorate the legend

In the above code, we are responsible forthemeAfter comprehensive adjustment, we will mainly adjust the legend below

Define legend location

P1 < - P + theme (legend.position = "top") + \
  Guides (fill = guide\u legend (direction = "horizontal") + \\
         Guides (fill=guide\u legend (nrow=3, byrow=true)) \

p1
Ggplot2 those things that decorate the legend

P2 < - P + theme (legend.title = element\u blank(), \
          legend. key=element_ Blank(), \\
          legend. text = element_ Text (color= "black", size=10), \define legend text
          legend. spacing. X=unit (0.1,'cm'), \defined text book level distance
          legend. key. Width=unit (0.5,'cm'), \define the horizontal size of the legend
          legend. key. Height=unit (0.5,'cm'), \define the vertical size of the legend
          legend. background=element_ Blank(), \
          legend. box. background=element_ Rect (colour = "black"), \
          legend. box. Margin = margin (1,1,1,1)) \
Ggplot2 those things that decorate the legend

p2 + theme(legend.justification = c("right","bottom"))
Ggplot2 those things that decorate the legend

Place the legend in the diagram

p2 + theme(legend.position = c(1, 1), legend.justification = c(1, 1))
Ggplot2 those things that decorate the legend

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

Recommended Today

Modul of fastems

Each module of fastems is implemented from the abstract class Fastems.Mms.Client.Infrastructure.UiModuleBase; public class DataManagerModule : UiModuleBase { public override void Initialize() { AddResourceDictionary(“/Resources/DataManagerResources.xaml”, typeof(DataManagerModule)); RegisterViewWithRegion(“DialogRegion”, typeof(DialogView)); RegisterViewWithRegion(“BusyIndicatorRegion”, typeof(BusyIndicatorView)); } } And Fastems.Mms.Client.Infrastructure.UiModuleBase inherits from Fastems.Mms.Client.Infrastructure.ModuleBase public abstract class UiModuleBase : ModuleBase { [Import] public IRegionManager RegionManager { get; set; } [Import] public IMergedDictionaryRegistry MergedDictionaryRegistry { […]