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 histogram

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use ggplot2 to draw a histogram, the article is very detailed, has a certain reference value, interested friends must read it!

Using ggplot2 to draw histogram, based on geometric object geom_histogram () function

Geom_histogram (mapping = NULL, data = NULL, stat = "bin", position = "stack",..., binwidth = NULL, bins = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

Among them, mapping graphic attribute mapping involves many, the data to be counted are mapped to X axis, Y axis mapping statistical types (including frequency count or frequency density, etc.), and other image attributes are also involved.

Statistical transformation stat= "bin", corresponding to set the size of the group interval binwidth, thus counting the frequency or frequency in the interval between the groups.

Now use the data to draw the histogram and add the count calculated in the drawing to the column.

Basic data:

> head (dat) ID CK1 AT1G01010 3.5815902 AT1G01020 5.0367753 AT1G01030 5.0353804 AT1G01040 2.6927165 AT1G01046 0.0000006 AT1G01050 114.336000 > dim (dat) [1] 32483 2

The second column of the data CK represents the gene expression FPKM, and then convert it to log10 (CK) for mapping.

Base code:

P=ggplot (dat,aes (x=log10 (CK) + geom_histogram (stat= "bin", binwidth = 1) + xlim (- 3mem5) print (p)

Its y-axis defaults to count type. If it is not set, it is equivalent to aes (yearly.. count..), corresponding to the frequency type.

Note that the value is derived from statistical transformation, so there are two points before and after each.

P=ggplot (dat,aes (x=log10 (CK), yearly.. count..) + geom_histogram (stat= "bin", binwidth = 1) + xlim (- 3Magne5) print (p)

For text addition, the geom_text function is used, and its aes (label) needs to be count... Mapping, at the same time, the settings of stat and binwidth must be the same as before before the calculated frequency can be added to the picture.

P=p+geom_text (aes (label=as.character (.. count..)), stat= "bin", binwidth=1,vjust=-0.5) print (p)

The process of drawing frequency diagram and adding density values is similar: 1, aes (y=..density) 2, statistical transformation, etc. 3. Density retains only two decimal places using round ()

P=ggplot (dat,aes (x=log10 (CK), density..)) + geom_histogram (stat= "bin", binwidth=1) + xlim (- 3Power5) p=p+geom_text (aes (label=as.character (round (.. density..,2)), stat= "bin", binwidth=1,vjust=-0.5) print (p) these are all the contents of the article "how to draw a histogram using ggplot2". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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