ggplot2 绘制环形图

数据可视化:堆积柱状图极坐标环化

1. 效果图

PMID=34911763
在这里插入图片描述

2. 模仿

(1) 准备数据

Input =("
  row   mapType   pct rep
  1                  'Uniquely mapped reads %' 78.74   1
  2       '% of reads mapped to multiple loci' 16.99   1
  3       '% of reads mapped to too many loci'  2.68   1
  4 '% of reads unmapped: too many mismatches'  0.00   1
  5           '% of reads unmapped: too short'  0.42   1
  6               '% of reads unmapped: other'  1.17   1
")
dat = as.data.frame(read.table(textConnection(Input),
                              header=TRUE,
                              row.names=1, sep = ""))
dat$type2=paste0("c", 1:nrow(dat))
str(dat)
dat2=dat[, c("type2", "pct")]
dat2

> dat2
  type2   pct
1    c1 78.74
2    c2 16.99
3    c3  2.68
4    c4  0.00
5    c5  0.42
6    c6  1.17

(2)整理数据:对比例列计算累计比例

dat2$fraction = dat2$pct / sum(dat2$pct)  # 计算比例
dat2$ymax = cumsum(dat2$fraction)         # 计算累积比例
dat2$ymin = c(0, head(dat2$ymax, n=-1))   # 计算最小值
dat2$label_y = (dat2$ymax + dat2$ymin) / 2  # 计算标签的 y 位置
dat2

> dat2
  type2   pct fraction   ymax   ymin label_y
1    c1 78.74   0.7874 0.7874 0.0000 0.39370
2    c2 16.99   0.1699 0.9573 0.7874 0.87235
3    c3  2.68   0.0268 0.9841 0.9573 0.97070
4    c4  0.00   0.0000 0.9841 0.9841 0.98410
5    c5  0.42   0.0042 0.9883 0.9841 0.98620
6    c6  1.17   0.0117 1.0000 0.9883 0.99415

(3) 绘制一个堆积柱状图

library(ggplot2)

colorset.RNA=c("#6E7AA3", "#68CDE0", "#FF8166", "#FFB7A2", "#C175B3", "#3CACDD", "#24C6A6")

# 分解步骤1
ggplot(dat2, aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3, fill = type2)) +
  geom_rect()+
  #coord_polar(theta = "y") +
  xlim(c(2, 4))+
  scale_fill_manual(values=colorset.RNA)  # 填充颜色

在这里插入图片描述

(4) 极坐标环化:最终效果

# 绘制环形图
p1=ggplot(dat2, aes(ymax = ymax, ymin = ymin, 
                 xmax = 4, xmin = 3, #环的x轴范围
                 fill = type2)) +
  geom_rect() +
  coord_polar(theta = "y", direction = -1) +    # 转换为极坐标
  xlim(c(2, 4.1)) +   # 设置环的大小, [1]内部的空心圆;[2]扩大外部范围,为文字留空间
  theme_void(base_size = 14) +      # 主题设置
  labs(title = "RNA percentage") +                 # 添加标题
  #scale_fill_brewer(palette = "Set3", direction = -1)  # 选择调色板
  scale_fill_manual(values=colorset.RNA)+  # 填充颜色

  ggrepel::geom_text_repel(aes(x=3.8, y = label_y, label = type2), # 添加标签
                              size = 7,  #标签字体大小
                              color = "black",                # 设置标签颜色
                              segment.color = "red",        # 设置连线颜色
                              segment.size = 2,            # 设置连线粗细
                              nudge_x = 0.3,                 # 设置连线长度
                              point.padding = 0.8); p1            # 设置点的填充

在这里插入图片描述

Ref

  • []
<think>我们之前讨论的是R中查看向量元素数量的方法,现在用户询问的是使用ggplot2绘制柱状的方法。根据引用内容,我们可以提供以下步骤: 1. 绘制基本柱状:使用geom_bar()函数,其中stat="identity"表示直接使用数据中的值作为高度。 2. 添加百分比标签:使用geom_text()在柱子上方添加标签,标签内容可以通过paste0()函数将数值转换为百分比格式。 3. 调整柱状样式:如调整柱形宽度、颜色、位置等。 4. 调整坐标轴和标题:使用labs()和xlab()、ylab()设置标题和坐标轴标签。 另外,引用[3]中提到了分组柱状绘制方法,主要是通过position=position_dodge()来调整柱子的位置,并设置width调整宽度。 我们将按照以下步骤组织答案: 1. 准备数据:创建一个示例数据框。 2. 基本柱状绘制。 3. 添加标签(包括百分比)。 4. 调整样式(颜色、宽度等)。 5. 分组柱状绘制方法。 注意:用户可能需要显示的是频率的百分比,因此我们假设数据中已经计算好了百分比(如每个类别的频率占总数的百分比),或者我们在绘制前先计算。 但引用[1]中的例子直接使用了frequency列,并在标签中加上了百分号。实际上,如果数据中已经是百分比数值,则可以直接使用;如果不是,则需要先转换。 我们将通过一个完整的例子来展示,包括数据准备和绘步骤。 注意:在绘制柱状时,如果数据是已经汇总好的,使用geom_bar(stat="identity");如果是原始数据(未汇总),则可以使用geom_bar(stat="count")来自动计数。 由于用户没有提供具体数据,我们创建一个示例数据框。 同时,根据引用[4]的内容,我们暂时不涉及环形,但用户如果感兴趣也可以扩展。 最后,生成相关问题。 现在开始组织回答: </think>下面详细介绍使用ggplot2绘制柱状的完整方法,涵盖基础绘制到高级定制技巧: ### 一、基础柱状绘制 ```r library(ggplot2) # 示例数据 data <- data.frame( category = c("A", "B", "C", "D"), frequency = c(25, 40, 30, 45) ) # 基本柱状 ggplot(data, aes(x = category, y = frequency)) + geom_bar(stat = "identity", fill = "steelblue") + # 使用实际数值绘制 geom_text(aes(label = frequency), vjust = -0.5) + # 添加数值标签 labs(title = "基础柱状", x = "类别", y = "频率") + theme_minimal() ``` ### 二、百分比柱状 ```r # 计算百分比 data$percent <- round(data$frequency / sum(data$frequency) * 100, 1) ggplot(data, aes(x = category, y = frequency)) + geom_bar(stat = "identity", fill = "#69b3a2") + geom_text( aes(label = paste0(percent, "%")), # 显示百分比 vjust = -0.5, size = 4, color = "darkred" ) + labs(title = "带百分比柱状", x = "类别", y = "频率") + theme_classic() ``` ### 三、分组柱状定制 ```r # 创建分组数据 group_data <- data.frame( category = rep(c("A", "B", "C", "D"), 2), group = rep(c("Group1", "Group2"), each = 4), value = c(25, 40, 30, 45, 35, 20, 50, 30) ) ggplot(group_data, aes(x = category, y = value, fill = group)) + geom_bar( stat = "identity", position = position_dodge(0.8), # 分组间距控制 width = 0.7, # 柱宽控制 color = "black", # 边框颜色 size = 0.5 # 边框粗细 ) + scale_fill_manual(values = c("#FF9999", "#56B4E9")) + # 自定义填充色 labs(title = "分组柱状", x = "类别", y = "值") + theme_bw() ``` ### 四、高级定制技巧 1. **坐标轴翻转**: ```r ggplot(data, aes(x = reorder(category, -frequency), y = frequency)) + geom_bar(stat = "identity") + coord_flip() # 横向柱状 ``` 2. **比例缩放**: ```r ggplot(data, aes(x = category, y = frequency)) + geom_bar(stat = "identity") + scale_y_continuous(limits = c(0, 50), breaks = seq(0, 50, 10)) ``` 3. **主题定制**: ```r ggplot(data, aes(x = category, y = frequency)) + geom_bar(stat = "identity") + theme( plot.title = element_text(size = 16, face = "bold"), axis.title = element_text(size = 12), panel.grid.major = element_blank() ) ``` ### 五、常见问题解决 1. **显示零值标签**:添加`position = position_stack(vjust = 0.5)` 2. **长标签处理**:使用`scale_x_discrete(labels = function(x) str_wrap(x, width = 10))` 3. **大数据集优化**:使用`geom_col()`替代`geom_bar(stat="identity")`提升性能
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值