- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 701 个
- 通用积分
- 70.5504
- 学术水平
- 6 点
- 热心指数
- 6 点
- 信用等级
- 5 点
- 经验
- 5326 点
- 帖子
- 58
- 精华
- 0
- 在线时间
- 341 小时
- 注册时间
- 2016-6-6
- 最后登录
- 2024-4-19
硕士生
还不是VIP/贵宾
- 威望
- 0 级
- 论坛币
- 701 个
- 通用积分
- 70.5504
- 学术水平
- 6 点
- 热心指数
- 6 点
- 信用等级
- 5 点
- 经验
- 5326 点
- 帖子
- 58
- 精华
- 0
- 在线时间
- 341 小时
- 注册时间
- 2016-6-6
- 最后登录
- 2024-4-19
| 开心 2021-8-16 15:18:40 |
---|
签到天数: 86 天 连续签到: 1 天 [LV.6]常住居民II
|
50论坛币
我在使用ggplot2画分面图遇到一个问题,想要使用geom_text给每个分面添加一个不同的注释,但是这个注释没有添加到原本的图上,而是另外添加了一个分面,请问有没有大佬知道怎么回事,该怎么解决?谢谢~具体数据和代码如下:
数据
Cluster | occurrence | T_stat | Percentage | cluster13 | Nonoccurrence | T1 | 0.014634 | cluster13 | Occurrence | T1 | 0.029091 | cluster19 | Nonoccurrence | T1 | 0.041284 | cluster19 | Occurrence | T1 | 0.007634 | cluster24 | Nonoccurrence | T1 | 0.016461 | cluster24 | Occurrence | T1 | 0.029536 | cluster13 | Nonoccurrence | T2 | 0.107317 | cluster13 | Occurrence | T2 | 0.141818 | cluster19 | Nonoccurrence | T2 | 0.178899 | cluster19 | Occurrence | T2 | 0.083969 | cluster24 | Nonoccurrence | T2 | 0.156379 | cluster24 | Occurrence | T2 | 0.097046 | cluster13 | Nonoccurrence | T3 | 0.834146 | cluster13 | Occurrence | T3 | 0.8 | cluster19 | Nonoccurrence | T3 | 0.743119 | cluster19 | Occurrence | T3 | 0.874046 | cluster24 | Nonoccurrence | T3 | 0.781893 | cluster24 | Occurrence | T3 | 0.848101 | cluster13 | Nonoccurrence | T4 | 0.043902 | cluster13 | Occurrence | T4 | 0.029091 | cluster19 | Nonoccurrence | T4 | 0.036697 | cluster19 | Occurrence | T4 | 0.034351 | cluster24 | Nonoccurrence | T4 | 0.045267 | cluster24 | Occurrence | T4 | 0.025316 |
不加注释代码:
- library(ggplot2)
- library(ggalluvial)
- library(ggsignif)
- library(RColorBrewer)
- hist_plot_T_stat <- ggplot(data = CountTable_T_melt,aes(x=occurrence,y=100 * Percentage)) +
- geom_stratum(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),width = 0.8, color='white')+
- geom_alluvium(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),
- alpha = 0.5,
- width = 0.8,
- curve_type = "linear") +
- facet_grid(.~ Cluster, switch = "both") +
- coord_fixed(ratio = 0.04) +
- scale_fill_manual(values=brewer.pal(9,"Set1")[4:1]) +
- theme_classic() +
- xlab("") +
- ylab("Percentage (%)") +
- theme(legend.title = element_blank(),
- axis.title = element_text(size = 12, face = "bold"),
- axis.text.x = element_text(size = 10, face = "bold", angle = 20, hjust = 0.5, vjust = 0.8),
- axis.text.y = element_text(size = 10, face = "bold"),
- strip.text = element_text(size = 10, face = "bold"),
- strip.background = element_rect(
- color="lightgrey", fill="lightgrey", size = 0.5),
- panel.spacing = unit(0.08, "in"),
- strip.placement = "top",
- strip.switch.pad.grid = unit(-13.5, "cm"),
- plot.margin=margin(t = 0, r = 0, b = 0, l = 0,'in')) +
- scale_y_continuous(breaks=seq(0,100,20)) +
- geom_segment(x=1,y=101,xend=1,yend=103) +
- geom_segment(x=1,y=103,xend=2,yend=103) +
- geom_segment(x=2,y=101,xend=2,yend=103)
- hist_plot_T_stat
复制代码
然后我想加上一个注释在那个横线上面:- hist_plot_T_stat <- ggplot(data = CountTable_T_melt,aes(x=occurrence,y=100 * Percentage)) +
- geom_stratum(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),width = 0.8, color='white')+
- geom_alluvium(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),
- alpha = 0.5,
- width = 0.8,
- curve_type = "linear") +
- facet_grid(.~ Cluster, switch = "both") +
- coord_fixed(ratio = 0.04) +
- scale_fill_manual(values=brewer.pal(9,"Set1")[4:1]) +
- theme_classic() +
- xlab("") +
- ylab("Percentage (%)") +
- theme(legend.title = element_blank(),
- axis.title = element_text(size = 12, face = "bold"),
- axis.text.x = element_text(size = 10, face = "bold", angle = 20, hjust = 0.5, vjust = 0.8),
- axis.text.y = element_text(size = 10, face = "bold"),
- strip.text = element_text(size = 10, face = "bold"),
- strip.background = element_rect(
- color="lightgrey", fill="lightgrey", size = 0.5),
- panel.spacing = unit(0.08, "in"),
- strip.placement = "top",
- strip.switch.pad.grid = unit(-13.5, "cm"),
- plot.margin=margin(t = 0, r = 0, b = 0, l = 0,'in')) +
- scale_y_continuous(breaks=seq(0,100,20)) +
- geom_segment(x=1,y=101,xend=1,yend=103) +
- geom_segment(x=1,y=103,xend=2,yend=103) +
- geom_segment(x=2,y=101,xend=2,yend=103) +
- geom_text(data = Pvalue_T_chisq_df, x=1.5, y=104, aes(label = P_label))
- hist_plot_T_stat
复制代码
其中Pvalue_T_chisq_df是这样的:
Cluster | P_label | Cluster13 | 0.38 | Cluster19 | 7.62E-04 | Cluster24 | 0.1 |
我想把注释放在那个短横线上,不知道为啥变成这样,请问有什么解决办法,多谢~
|
最佳答案
千车麦穗 查看完整内容
你的Pvalue_T_chisp_df文件中Cluster列中的Cluster13、19、24首字母改为小写的,就可以了,刚替你试过了。
|