楼主: ly_0
218 1

[问答] ggplot画图求助 [推广有奖]

  • 0关注
  • 0粉丝

硕士生

80%

还不是VIP/贵宾

-

威望
0
论坛币
701 个
通用积分
70.5504
学术水平
6 点
热心指数
6 点
信用等级
5 点
经验
5326 点
帖子
58
精华
0
在线时间
341 小时
注册时间
2016-6-6
最后登录
2024-4-19

楼主
ly_0 发表于 2023-10-18 15:54:42 |只看作者 |坛友微信交流群|倒序 |AI写论文
50论坛币
我在使用ggplot2画分面图遇到一个问题,想要使用geom_text给每个分面添加一个不同的注释,但是这个注释没有添加到原本的图上,而是另外添加了一个分面,请问有没有大佬知道怎么回事,该怎么解决?谢谢~具体数据和代码如下:
数据
ClusteroccurrenceT_statPercentage
cluster13NonoccurrenceT1

0.014634

cluster13OccurrenceT1

0.029091

cluster19NonoccurrenceT1

0.041284

cluster19OccurrenceT1

0.007634

cluster24NonoccurrenceT1

0.016461

cluster24OccurrenceT1

0.029536

cluster13NonoccurrenceT2

0.107317

cluster13OccurrenceT2

0.141818

cluster19NonoccurrenceT2

0.178899

cluster19OccurrenceT2

0.083969

cluster24NonoccurrenceT2

0.156379

cluster24OccurrenceT2

0.097046

cluster13NonoccurrenceT3

0.834146

cluster13OccurrenceT3

0.8

cluster19NonoccurrenceT3

0.743119

cluster19OccurrenceT3

0.874046

cluster24NonoccurrenceT3

0.781893

cluster24OccurrenceT3

0.848101

cluster13NonoccurrenceT4

0.043902

cluster13OccurrenceT4

0.029091

cluster19NonoccurrenceT4

0.036697

cluster19OccurrenceT4

0.034351

cluster24NonoccurrenceT4

0.045267

cluster24OccurrenceT4

0.025316



不加注释代码:
  1. library(ggplot2)
  2. library(ggalluvial)
  3. library(ggsignif)
  4. library(RColorBrewer)

  5. hist_plot_T_stat <-  ggplot(data = CountTable_T_melt,aes(x=occurrence,y=100 * Percentage)) +
  6.   geom_stratum(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),width = 0.8, color='white')+
  7.   geom_alluvium(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),
  8.                 alpha = 0.5,
  9.                 width = 0.8,
  10.                 curve_type = "linear") +
  11.   facet_grid(.~ Cluster, switch = "both") +
  12.   coord_fixed(ratio = 0.04) +
  13.   scale_fill_manual(values=brewer.pal(9,"Set1")[4:1]) +
  14.   theme_classic() +
  15.   xlab("") +
  16.   ylab("Percentage (%)") +
  17.   theme(legend.title = element_blank(),
  18.         axis.title = element_text(size = 12, face = "bold"),
  19.         axis.text.x = element_text(size = 10, face = "bold", angle = 20, hjust = 0.5, vjust = 0.8),
  20.         axis.text.y = element_text(size = 10, face = "bold"),
  21.         strip.text = element_text(size = 10,  face = "bold"),
  22.         strip.background = element_rect(
  23.           color="lightgrey", fill="lightgrey", size = 0.5),
  24.         panel.spacing = unit(0.08, "in"),
  25.         strip.placement = "top",
  26.         strip.switch.pad.grid = unit(-13.5, "cm"),
  27.         plot.margin=margin(t = 0, r = 0, b = 0, l = 0,'in')) +
  28.   scale_y_continuous(breaks=seq(0,100,20)) +
  29.   geom_segment(x=1,y=101,xend=1,yend=103) +
  30.   geom_segment(x=1,y=103,xend=2,yend=103) +
  31.   geom_segment(x=2,y=101,xend=2,yend=103)

  32. hist_plot_T_stat
复制代码
添加注释前

然后我想加上一个注释在那个横线上面:
  1. hist_plot_T_stat <-  ggplot(data = CountTable_T_melt,aes(x=occurrence,y=100 * Percentage)) +
  2.   geom_stratum(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),width = 0.8, color='white')+
  3.   geom_alluvium(aes(fill=T_stat,stratum = T_stat, alluvium = T_stat),
  4.                 alpha = 0.5,
  5.                 width = 0.8,
  6.                 curve_type = "linear") +
  7.   facet_grid(.~ Cluster, switch = "both") +
  8.   coord_fixed(ratio = 0.04) +
  9.   scale_fill_manual(values=brewer.pal(9,"Set1")[4:1]) +
  10.   theme_classic() +
  11.   xlab("") +
  12.   ylab("Percentage (%)") +
  13.   theme(legend.title = element_blank(),
  14.         axis.title = element_text(size = 12, face = "bold"),
  15.         axis.text.x = element_text(size = 10, face = "bold", angle = 20, hjust = 0.5, vjust = 0.8),
  16.         axis.text.y = element_text(size = 10, face = "bold"),
  17.         strip.text = element_text(size = 10,  face = "bold"),
  18.         strip.background = element_rect(
  19.           color="lightgrey", fill="lightgrey", size = 0.5),
  20.         panel.spacing = unit(0.08, "in"),
  21.         strip.placement = "top",
  22.         strip.switch.pad.grid = unit(-13.5, "cm"),
  23.         plot.margin=margin(t = 0, r = 0, b = 0, l = 0,'in')) +
  24.   scale_y_continuous(breaks=seq(0,100,20)) +
  25.   geom_segment(x=1,y=101,xend=1,yend=103) +
  26.   geom_segment(x=1,y=103,xend=2,yend=103) +
  27.   geom_segment(x=2,y=101,xend=2,yend=103) +
  28.   geom_text(data = Pvalue_T_chisq_df, x=1.5, y=104, aes(label = P_label))

  29. hist_plot_T_stat
复制代码
添加注释后
其中Pvalue_T_chisq_df是这样的:
ClusterP_label
Cluster13

0.38

Cluster19

7.62E-04

Cluster24

0.1


我想把注释放在那个短横线上,不知道为啥变成这样,请问有什么解决办法,多谢~

最佳答案

千车麦穗 查看完整内容

你的Pvalue_T_chisp_df文件中Cluster列中的Cluster13、19、24首字母改为小写的,就可以了,刚替你试过了。
关键词:gplot plot GPL Occurrence percentage
沙发
千车麦穗 发表于 2023-10-18 15:54:43 |只看作者 |坛友微信交流群
你的Pvalue_T_chisp_df文件中Cluster列中的Cluster13、19、24首字母改为小写的,就可以了,刚替你试过了。
已有 3 人评分经验 论坛币 收起 理由
dxystata + 100 + 10 热心帮助其他会员
oliyiyi + 100 精彩帖子
cheetahfly + 10 热心帮助其他会员

总评分: 经验 + 200  论坛币 + 20   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 21:41