当前位置:网站首页>[data analysis and visualization] key points of data drawing 10- construction of legend
[data analysis and visualization] key points of data drawing 10- construction of legend
2022-06-13 02:34:00 【The winter holiday of falling marks】
Key points of data drawing 10- Construction of legend
Legend is a key part of data visualization to convey information , So it's important to create the right legend . This article describes how to set up ggplot2 Title in legend 、 Text 、 Location 、 Symbols etc. .
List of articles
ggplot2 Legend settings
ggplot2 Create default legend
By default , As long as the shape feature is mapped to ggplot Called aes() Variables in the section ,ggplot2 The legend will be automatically generated on the chart . therefore , If you are in ggplot2 Set colors in 、 Shape or transparency , The legend will be created by default . If you want to see ggplot2 Information that can be set , call ggplot2::theme that will do .
# load ggplot2
library(ggplot2)
# Show your own mtcars data
head(mtcars)
| mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | |
| Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
| Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
# mapping
# The horizontal axis wt, The vertical axis mpg, Color cyl, shape factor
basic <- ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), shape = factor(vs) )) +
geom_point()
basic

Change legend title labs()
labs() The function allows you to change the legend title . You can specify a title for each part of the legend , That is, every aesthetics used in the chart aesthetics .
# Set the legend title based on the default chart
basic+
labs(
colour = "colour",
shape = "shape"
)

Deletion of legend information
adopt theme() and guides() You can delete the entire legend or a specific part of the legend .
# Delete shape
basic + guides(shape=FALSE) +
labs(
colour = "colour",
shape = "shape"
)
Warning message:
"`guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> = "none")` instead."

# Delete legend
basic + theme(legend.position = "none")

Control legend position
ggplot2 Allow legend to be placed anywhere . If you put the legend around the chart , Use legend.position Options and specify top,right,bottom, or left. To place the legend in the drawing area , Specify a length of 2 Vector , Both values are in 0 and 1 Between , And give x and y Coordinates are OK .
# Place the legend on the left side of the chart
basic + theme(legend.position = "left")

# Place the legend on the left side of the chart
# legend.justification After setting the legend position , The specific location of the legend
basic + theme(legend.position = "left",legend.justification = c("left", "center"))

basic + theme(
legend.position = c(0.55, 0.95), # Set the starting position of the legend , The bottom left corner is the origin , The range of abscissa and ordinate is 0 To 1
legend.justification = c("right", "top"), # After setting the legend position , The specific location of the legend
legend.box.just = "right", # Set different legend alignment
legend.margin = margin(6, 6, 6, 6) # Set the outer margin of the legend content
)

Legend appearance settings
Here are 4 Example , Shows how to customize the main functions of the legend :
- legend.box Custom legend outline
- legend.key Set each variable in the legend key Value presentation information
- legend.text Set legend font
- legend.title Set legend title
# Custom legend outline
basic + theme(
# Set the outline color
legend.box.background = element_rect(color="red", size=2),
# Set the outline margin
legend.box.margin = margin(36, 6, 6, 6)
)

# Set each variable in the legend key Value presentation information
basic + theme(legend.key = element_rect(fill = "blue", colour = "black"))

# Custom legend font color
basic + theme(legend.text = element_text(size = 8, colour = "red"))

# Set legend title
basic + theme(legend.title = element_text(face = "bold",colour = 'red'))

Reference resources
边栏推荐
- Opencvsharp4 handwriting recognition
- For loop instead of while loop - for loop instead of while loop
- Paper reading - beat tracking by dynamic programming
- Common web page status return code crawler
- What are the differences in cache/tlb?
- 在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容
- Summary of innovative ideas of transformer model in CV
- Understand speech denoising
- An image is word 16x16 words: transformers for image recognition at scale
- Exam23 named windows and simplified paths, grayscale conversion
猜你喜欢

柏瑞凱電子沖刺科創板:擬募資3.6億 汪斌華夫婦為大股東

Paper reading - jukebox: a generic model for music

Graph theory, tree based concept

Gadgets: color based video and image cutting

Chapter7-12_ Controllable Chatbot

Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL

Priority queue with dynamically changing priority

OpenCVSharpSample04WinForms

For loop instead of while loop - for loop instead of while loop
![Leetcode 926. Flip string to monotonically increasing [prefix and]](/img/ca/d23c1927bc32393cf023c748e4b449.png)
Leetcode 926. Flip string to monotonically increasing [prefix and]
随机推荐
Branch and bound method, example sorting
Change tax for 2
Gadgets: color based video and image cutting
数仓笔记|针对客户维度建模需要关注的5个因素
Microsoft Pinyin opens U / V input mode
How did you spend your winter vacation perfectly?
Laravel 权限导出
重定向设置参数-RedirectAttributes
[pytorch] kaggle image classification competition arcface + bounding box code learning
Graph theory, tree based concept
C # illustrated tutorial (Fourth Edition) chapter7-7.2 accessing inherited members
Introduction to easydl object detection port
GMM Gaussian mixture model
Opencvsharp4 handwriting recognition
Linear, integer, nonlinear, dynamic programming
Hstack, vstack and dstack in numpy
微信云开发粗糙理解
[reading papers] deepface: closing the gap to human level performance in face verification. Deep learning starts with the face
Matlab: obtain the figure edge contour and divide the figure n equally
OpenCVSharpSample04WinForms