Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ggplot2 to draw density Map

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use ggplot2 to draw density maps. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Use ggplot2 to draw the density map, and directly overlay the density curve of another group of data on the density map.

Library ('ggplot2') library (' reshape2') 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)

Density diagram

P_density=ggplot (dat,aes (x=Num)) + geom_density (aes (fill=as.character (dat$Sample), color=as.character (dat$Sample)), alpha = 0.5 plot.title = element_text (size= 25 (bold), vjust = 0.5, hjust = 0.5), legend.title = element_blank (), legend.text = element_text (size= 15, face = "bold"), legend.position = 'right' Legend.key.size=unit (0.5), axis.line=element_line (size= 1) color = "black"), axis.ticks.x=element_blank (), axis.text.x=element_text (size= 15) face = "bold", vjust = 0.5, hjust = 0.5), axis.text.y=element_text (size= 15 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 = NA), panel.grid.minor = element_blank (), panel.grid.major = element_blank () Plot.background = element_rect (fill = "transparent", colour = NA)) print (P_density)

Two sets of data directly overlay density map

Data dat1

A = rep (c ("A", "B", "C", "D"), each=2) B = c (6.332968, "C", "D") B = c (6.332968, 9.368328, 6.674348) E = c (5.367671, 7.28629350) C = c (5.367671, 7.286253, 3.875520, 6.679444) D = c (5.171107, 6.2318186668, 3.942029) D = c (5.171107, 2.2318186668, 2.320568, 4.9244987.140883, 4.228142last, 5.7945,146.347785) E = c (5.5337546.6.152336.1134.9134.968,968,348,3102F, rep = "sample2"). Len=4) dat1= data.frame (A dat1,variable.name= B Sample C penny Djue E) names (dat1) [1] = c ("type") names (dat1) [2:5] = Fdat1= melt (dat1,variable.name= "Sample", value.name = "Num") head (dat1)

Data dat2

A = rep (c ("A", "B", "C", "D"), each=2) B = c (9.944277, "C", "D"), B = c (9.944277, 9.245216, 8.573114, 7.953372, 10.756460) E = c (8.248881, 9.238328, 9.7771346) C = c (8.248881, 9.23328, 9.789772, 9.800550690) D = c (9.083611, 9.076143, 9.650690) D = c (9.88443, 9.87663561, 10.7525,57561,88.3614,189.404, 189.0481,3948) E = c (9.82192,199.4300, 43,333, 439.10999433, 439.5895135, 512275, 535,627,627,949F, "sample3", "sample3", "sample3"). Len=4) dat2 = data.frame (A type, C Sample, E) names (dat2) [1] = c ("type") names (dat2) [2:5] = Fdat2 = melt (dat2,variable.name= "Sample", value.name = "Num") head (dat2)

Drawing

P_density=ggplot (data=NULL) + geom_density (aes (x=dat1 $Num,fill=as.character (dat1 $Sample), color=as.character (dat1 $Sample)), alpha = 0.3 (x=dat2 $Num,fill=as.character (dat2 $Sample), color=as.character (dat2 $Sample)) + geom_density (x=dat2 $Num,fill=as.character (dat2 $Sample), color=as.character (dat2 $Sample)) + labs (x = "Num") + theme (plot.title = element_text) Legend.title = element_blank (), legend.text = element_text (size= 15 camera face = "bold"), legend.position = 'right', legend.key.size=unit (0.5 camera cm'), axis.line=element_line (size= 1 camera = "black"), axis.ticks.x=element_blank (), axis.text.x=element_text (size= 15 camera face = "bold") Vjust = 0.5, hjust = 0.5), axis.text.y=element_text (size = 15 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) Panel.background = element_rect (fill = "transparent", colour = NA), panel.grid.minor = element_blank (), panel.grid.major = element_blank (), plot.background = element_rect (fill = "transparent", colour = NA) print (P_density) Thank you for reading! This is the end of this article on "how to draw a density map using ggplot2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report