> For the complete documentation index, see [llms.txt](https://zpliu.gitbook.io/booknote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zpliu.gitbook.io/booknote/ggplot2/untitled-folder-1/zhi-fang-tu-hui-zhi.md).

# 直方图绘制

## 1.频数直方图

### 数据准备

```bash
> head(pacBioData)
               V1 V2
1 Ghir_A01G000010  1
2 Ghir_A01G000030  1
3 Ghir_A01G000040  3
4 Ghir_A01G000070  3
5 Ghir_A01G000080  1
6 Ghir_A01G000100  2
```

如果你同时含有两个样本的频数信息，需要针对不同的样本画不同的的柱子；只需要在数据框中添加一个字段即可。例如我的数据中含有PacBio测序数据和参考基因组数据两个文件

* PacBio
* regerence

```r
pacBioData<- read.table("PacBio文件")
referenceData <- read.table("reference文件")
##添加分类信息
pacBioData$type <- "PacBio"
referenceData$type <- "reference"
## 合并两个数据框
mergeData <- rbind(pacBioData, referenceData)
```

### 绘制直方图

* `x=V2`表示x轴数据使用V2字段进行映射
* `fill = type`针对type字段使用不同颜色进行填充
* `stat = "count"`统计x轴中每个值出现的次数，用作柱子的高度

```r
library(ggplot2)
ggplot(data = mergeData, aes(x = V2, fill = type)) +
  geom_bar(stat = "count")
```

![堆积直方图](https://s1.ax1x.com/2020/04/12/GO7SiV.png)

#### 调整

* `position = "dodge"`将柱子调整为不堆积状态
* `width = 0.5`调整柱子宽度&#x20;

这里由于x轴的坐标轴范围比较大，柱子缩放了看不清

```r
ggplot(data = mergeData, aes(x = V2, fill = type)) +
  geom_bar(stat = "count", position = "dodge", width = 0.5)
```

![不堆积](https://s1.ax1x.com/2020/04/12/GOH6nH.png)

## 2.频率直方图

### 数据准备

使用R中的`table`函数和`prop.table`函数计算频率

```r
PacBioFrequent <- as.data.frame(prop.table(table(pacBioData$V2)))[1:10, ]
referenceFrequent <- as.data.frame(prop.table(table(referenceData$V2)))[1:10, ]
## 样本分类
PacBioFrequent$type <- "PacBio"
referenceFrequent$type <- "reference"
## 合并数据
mergeData <- rbind(PacBioFrequent, referenceFrequent)
```

处理后的数据

```r
Var1       Freq   type
1    1 0.28121310 PacBio
2    2 0.21962588 PacBio
3    3 0.15747934 PacBio
4    4 0.10686098 PacBio
5    5 0.07342614 PacBio
```

### 绘制图形

参数和之前的都是一样的

```r
ggplot(data = mergeData, aes(x =Var1, y=Freq,fill = type)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.5)
```

![频率直方图](https://s1.ax1x.com/2020/04/12/GOqkLQ.png)

## 3.美化图片

### 不涉及数据层的美化

* 图片背景色
* 图片网格
* 坐标轴刻度线
* 坐标轴刻度文字
* 坐标轴label文字
* 图例标题
* 图例位置

```r
p=ggplot(data = mergeData, aes(x =Var1, y=Freq,fill = type)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.5)  

p+theme_bw() +
  theme(
    panel.grid = element_blank(), #图片网格线
    panel.background = element_blank(), #图片背景色为空
    axis.line = element_line(size = 0.5, color = "black"),#坐标轴线条
    axis.text.x = element_text(size = "15"), #x坐标轴刻度文字
    axis.text.y = element_text(size = "10"), #y坐标轴刻度文字
    axis.title.y = element_text(size = "15"), #x坐标轴label文字
    legend.title = element_blank(), #图例标题
    legend.position = c(0.8, 0.8), #图例位置
  )
```

![美化1](https://s1.ax1x.com/2020/04/12/GOLE6O.png)

### 自定义填充色

```r
p+  scale_fill_manual(values = c(
    "#f6e58d", "#ffbe76", "#686de0", "#4834d4"
  ))
```

![美化2](https://s1.ax1x.com/2020/04/12/GOLYng.png)

### 调整柱子离坐标轴位置

这里我将柱子与x轴进行贴近，其他的可以类似

* `expand`贴近坐标轴的位置
* `limits`设置显示的范围

```r
p+  scale_y_continuous(expand = c(0, 0), limits = c(0, 1))
```

![美化3](https://s1.ax1x.com/2020/04/12/GOOFEj.png)

### 添加自定义文字

通过使用`geom_text`函数，并且选择不继承原有的图片数据

* `data = labelData`要显示的注释信息
* `mapping`指定显示的位置和字段
* `inherit.aes`是否继承图形数据，如果是使用自定义数据，这里一定要`FALSE`

```r
#将要展示的注释文字
labelData <- data.frame(text1 = "3.39", text2 = "/", text3 = "2.01")
p+geom_text(
    data = labelData,
    mapping = aes(x = 8, y = 0.5, label = text1),
    inherit.aes = FALSE,
    show.legend = NA,
    color = "#ffbe76"
  ) +
  geom_text(
    data = labelData,
    mapping = aes(x = 8.5, y = 0.5, label = text2),
    inherit.aes = FALSE,
    show.legend = NA,
    color = "black"
  ) +
  geom_text(
    data = labelData,
    mapping = aes(x = 9, y = 0.5, label = text3),
    inherit.aes = FALSE,
    show.legend = NA,
    color = "#4834d4"
  )
```

![美化4](https://s1.ax1x.com/2020/04/12/GOzsjx.png)

### 参考

1. CDSN[博客](https://blog.csdn.net/Bone_ACE/article/details/47427453)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zpliu.gitbook.io/booknote/ggplot2/untitled-folder-1/zhi-fang-tu-hui-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
