Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ggplot2 to draw box line diagram and carry out facet processing

Shulou Source: shulou.com Published: 2022-06-01 04:58:33 10月03日 Update

This article mainly shows you "how to use ggplot2 to draw a box map and facet processing", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use ggplot2 to draw a box map and facet processing" this article.

The box line diagram is drawn by ggplot2 and faceted processing is carried out.

Load the corresponding R packets and data settings

Library ('ggplot2') # draw library (' reshape2') # meltlibrary ('grid') # facet A = rep (c ("A", "B", "C", "D") Each=2) B = c (6.332968, 6.368328, 6.674348, 4.127901, 5.192845, 6.6528655, 7.829350, 6.995062) C = c (5.367671, 7.286253, 5.217053, 3.875520, 6.679444) D = c (5.171107, 6.2327186, 5.320568, 4.924498, 7.1408834.228142, 5.793514, 6.347785) E = c (5.337546.793514, 152393,) E = (5.3357546.1523918,),), len=4 (c) = rep ("sample1", "sample2")), len=4 = (len=4) () (5.17110771) (6.271881)) (5.32056881) (4.924 E) names (dat) [1] = c ("type") names (dat) [2:5] = Fdat = melt (dat,variable.name= "Sample", value.name = "Num") head (dat)

Draw a basic box diagram

# Statistics by type P_box=ggplot (data=dat,aes (xylene type)) + geom_boxplot (fill= "grey", width=0.4,notch=F) + # fill color fill, width The median line is missing notch=T labs (x = "type", y = "Num") + theme (plot.title = element_text (size= 25 vjust = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank (), legend.text = element_text (size= 18, face = "bold"), legend.position = 'right', legend.key.size=unit (0.8) Axis.ticks.x=element_blank (), # cancels the x-axis tick line axis.text.x=element_text (size = 10 camera face = "bold", vjust = 0.5, hjust = 0.5), # scale label text size, etc. set axis.text.y=element_text (size = 10 camera face = "bold", vjust = 0.5, hjust = 0.5) Axis.title.x = element_text (size = 20 camera face = "bold", vjust = 0.5, hjust = 0.5), axis.title.y = element_text (size = 20 camera face = "bold", vjust = 0.5, hjust = 0.5), # cancel the default background color box, etc. Panel.background = element_rect (fill = "transparent", colour = "black") Panel.grid.minor = element_blank (), panel.grid.major = element_blank (), plot.background = element_rect (fill = "transparent", colour = "black") print (P_box)

Use the sample for faceting

# based on samples, faceted P_box=ggplot (data=dat,aes (xonal typeparamination num) + geom_boxplot (fill= "grey", width=0.4,notch=F) + labs (x = "type", y = "Num") + theme (plot.title = element_text (size = 25 size = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank (), legend.text = element_text (size = 18, face = "bold") Legend.position = 'right', legend.key.size=unit', axis.ticks.x=element_blank (), axis.text.x=element_text (size= 10 camera face = "bold", vjust = 0. 5, hjust = 0. 5), axis.text.y=element_text (size= 10 camera face = "bold", vjust = 0. 5, hjust = 0. 5) Axis.title.x = element_text (size = 20 transparent face = "bold", vjust = 0.5, hjust = 0.5), axis.title.y = element_text (size = 20 camera face = "bold", vjust = 0.5, hjust = 0.5), panel.background = element_rect (fill = "transparent", colour = "black"), panel.grid.minor = element_blank (), panel.grid.major = element_blank () Plot.background = element_rect (fill = "transparent", colour = "black") + facet_grid (. ~ Sample) # facet print (P_box) are all the contents of the article "how to draw a Box Line drawing and facet processing with ggplot2" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Diagram processing content article scale sample color learning help size data text box easy to understand more organization label knowledge type ribbon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft OPPO Reno Redmi MySQL