准备数据
library(tidyverse)
HIV <- tibble(Triplet = str_c('Triplet', 1:7, sep = ' '),
From = c(rep('Zambia', 4), rep('South Africa', 3)),
`Group A` = c('28/1687 (1.64)',
'33/2086 (1.57)',
'23/1695 (1.36)',
'41/2013 (2.04)',
'36/1507 (2.35)',
'26/1808 (1.43)',
'13/2195 (0.57)'),
`Group B` = c('19/1979 (0.94)',
'29/2408 (1.20)',
'22/1687 (1.30)',
'19/1698 (1.13)',
'33/1811 (1.80)',
'26/2078 (1.24)',
'10/2488 (0.40)'),
`Group C` = c('24/2054 (1.17)',
'33/2262 (1.48)',
'29/1811 (1.63)',
'37/1561 (2.39)',
'28/1304 (2.15)',
'32/1375 (2.31)',
'14/2195 (0.59)'))
#数据调整
library(tidyr)
library(stringr)
library(ggplot2)
library(cowplot)
library(ggsci)
HIV_newdata <- gather(HIV, key = Group, value = Value, 3:5) %>%
separate(col = Value, # 拆分这一列
sep = ' ', # 分割符
into = c('Number', 'Ratio')) %>% # 分割成的两列
separate(col = Number,
sep = '/', into = c('Num', 'Total')) %>% # Number分割成的两列
mutate(Ratio = as.numeric(str_remove_all(Ratio, '\\(|\\)')), #替换掉括号
Num = as.numeric(Num),
Total = as.numeric(Total)) #vaTotal转换成数值性并赋给Total自己
数据查看 HIV_newdata
image.png
绘图
filter(HIV_newdata, Group %in% c('Group A', 'Group C'))%>% ggplot(aes(x = Group, y = Ratio, color = Triplet)) +
geom_line(aes(group = Triplet)) + #映射到两组之间划线
geom_point(aes(size = Num)) + #加点,点的大小映射到Num
geom_text_repel(data = filter(HIV_newdata, Group == 'Group A'), #加组A的标签
nudge_x = -0.15, 标签左移0.15
show.legend = F,
min.segment.length = Inf, 不展示标签和点之间的连接线
color = 'black',
segment.size = 0,
aes(label = str_remove(Triplet, 'Triplet '))) + #标签,其中利用函数把Triplet字符去掉
geom_text_repel(data = filter(HIV_newdata, Group == 'Group C'),
nudge_x = 0.15, 标签右移0.15
min.segment.length = Inf, #不展示标签和点之间的连接线
show.legend = F,
color = 'black',
segment.size = 0,
aes(label = str_remove(Triplet, 'Triplet '))) + #标签,其中利用函数把Triplet字符去掉
scale_color_jco() + #利用jco杂志颜色
scale_size(name = 'No. of Envents:', #图例点大小标签
breaks = seq(10, 40, 10),
labels = seq(10, 40, 10),
limits = c(10, 50), #图例点大小阈值范围
range = c(1, 5)) + #对点大小进行调整
scale_y_continuous(expand = c(0, 0), limits = c(0, 2.5)) + #把y轴设置为0开始,且设置起始值。
labs(x = '', y = 'HIV Infections\n(per 100 persons)') +
theme_half_open()
image.png
网友评论