R语言中stat_cor生成的r值与p值两行显示对齐问题
问题:散点图stat_cor标签两行无法左对齐的解决办法
我使用ggplot结合stat_cor为散点图添加Pearson相关系数r值和p值,已通过label.sep = "\n"将两者分两行显示,但标签无法左对齐,尝试使用hjust参数无效。
所用数据
> dput(df) structure(list(A = c(3.24792751344359, 11.6254805504711, 9.85096815098244, 7.62570884306446, 4.6724253419715, 5.65821148275179, 14.4494388579437, 2.16992500144231, 3.85798099512757, 15.4013962116234, 7.38801728534513, 4.04439411935845, 7.10328780841202, 9.38909352190447, 9.87113518424304, 12.751439451538, 3.95419631038688, 8.44914864537544, 12.8222720641578, 9.65731844958129, 8.78626962764847, 7.94544383637791, 6.67948009950545, 11.2243032093734, 9.46454575033394, 3.52356195605701, 4.49185309632967, 11.7950249839614, 4.7279204545632, 6.876516946565, 9.59339112279174, 11.1029598859101, 3.75488750216347, 8.35974956032233, 7.14465824283188, 8.52160043972373, 5.65821148275179, 4.49185309632967, 3.85798099512757, 10.8730595476615, 7.04984854945056, 6.21916852046216, 6.34872815423108, 9.66977088852552, 15.1287394887056, 10.9833499276765, 4.93073733756289, 7.2807707701306, 3.08746284125034, 8.82495874052852, 6.53138146051631, 8.71252700043982, 13.7365604837545, 7.34429590791582, 12.4371018289084, 3.24792751344359, 4.49185309632967, 4.7279204545632, 2.4594316186373, 7.65463602852797, 4.88264304936184, 13.3752431107337, 5.4757334309664, 4.49185309632967, 7.56605403817109, 6.66533591718518, 7.14465824283188, 7.21431912080077, 8.38154295118458, 10.4120405145517, 5.14974711950468, 14.717435454024, 12.9335984552372, 6.07681559705083, 8.05799172275918, 10.4424251933316, 1.32192809488736, 8.62753388447279, 11.1147183891884, 9.17866485100647, 9.54978466794786, 2.90689059560852, 5.62935662007961, 5.06608919045777, 5.06608919045777, 3.52356195605701, 5.18982455888002, 10.0409745988173, 14.2054496369478, 5.89481776330794, 11.3909750061555, 14.4876202472075, 6.31288295528436, 10.1286388128526, 7.47167521439204, 10.9155059622319, 7.3264294871223, 3.75488750216347, 8.49385544924082, -1), B = c(14.3127766206581, 14.4475675741951, 14.0405893950661, 12.9728900634355, 13.8401386381809, 13.9348426545031, 14.0350997792022, 13.7983199091531, 12.429799009167, 13.7326444284916, 13.5774878375881, 14.1097082484888, 13.8667476715049, 14.041530822723, 14.2691632137112, 13.540915056731, 13.7404659380353, 14.0689042060648, 14.3322114183684, 13.5738246735046, 14.7685720383286, 13.760252139086, 12.9358096237511, 13.6779948548276, 13.2124203878783, 12.7894317561295, 12.5645067843387, 13.2555449905389, 13.9070314769172, 13.2381807936996, 13.1269464072255, 13.7651306721844, 13.0182871274371, 14.9231185320901, 13.4296028886726, 13.1441005563635, 12.7558264511331, 13.9863309334321, 13.3964040956219, 14.1236768648636, 13.8001415579333, 13.0215872981245, 13.3918477190335, 13.2864855672956, 13.1608956144507, 13.9778612496769, 14.2483352900365, 13.3115354705716, 14.1847591417854, 14.0328636422936, 13.922445032087, 13.9193754670122, 14.1792482216554, 14.1501836084154, 13.9839288136058, 12.9035994534655, 13.771025140805, 14.0912287527363, 13.8237155308271, 14.0047032739544, 13.8973255233125, 13.9361318028622, 13.7190144075454, 14.1789371199985, 12.9471803166342, 13.550205673278, 13.9818794039634, 14.0891607874707, 13.8560850466602, 14.2923575846826, 13.3177658951147, 13.6720385382353, 13.9022337777197, 13.6913618610263, 12.9150390555186, 14.0970033353301, 14.0115330268357, 13.2674450883881, 14.1581757415753, 14.1520074961942, 14.175120670275, 13.8670373693052, 13.7511778994712, 13.1295245193378, 13.6348677587679, 13.3826915668966, 13.1415483835874, 13.7944666359748, 13.9242093993111, 13.8643312582358, 13.5818478602248, 13.4147512917602, 13.6018288106901, 13.791722516366, 12.8720014654425, 12.9857530107518, 13.4543635604993, 13.1279137392443, 14.1402306532103, 13.6308927640127)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))
原代码
library(ggplot2) library(ggpubr) formula <- y ~ x ggplot(data = df, aes(x = A, y = B)) + geom_point(alpha = 0.7) + geom_smooth(method = "lm", formula = formula, se = TRUE) + stat_cor(method = "pearson", show.legend = FALSE, cor.coef.name = "r", label.sep = "\n", label.x = 10, label.y = 12.6, size = 7) + theme_bw()
当前效果

解决方法
方法一:通过geom.text.args传递左对齐参数
stat_cor底层依赖geom_text渲染标签,但直接设置hjust参数无法生效,需要通过geom.text.args将参数传递给底层的geom_text。修改后的代码如下:
library(ggplot2) library(ggpubr) formula <- y ~ x ggplot(data = df, aes(x = A, y = B)) + geom_point(alpha = 0.7) + geom_smooth(method = "lm", formula = formula, se = TRUE) + stat_cor(method = "pearson", show.legend = FALSE, cor.coef.name = "r", label.sep = "\n", label.x = 10, label.y = 12.6, size = 7, geom
相关产品推荐
相关产品推荐

