首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在geom_text中同时显示科学数字和非科学数字?

如何在geom_text中同时显示科学数字和非科学数字?
EN

Stack Overflow用户
提问于 2021-06-12 17:43:06
回答 1查看 52关注 0票数 2

我想在非科学记数法中显示R,而在科学记数法中显示MBE。我正在使用下面的代码

代码语言:javascript
运行
复制
library(tidyverse)
library(hydroGOF)

summ <- df %>% 
  group_by(Date) %>% 
  summarise(R = cor(x, y, use="pairwise.complete.obs"),
            MBE = me(obs=x, sim=y, na.rm=TRUE)) %>%
  mutate(across(MBE, ~round(., 5))) %>%
  mutate(across(R, ~round(., 2)))

p <- ggplot(data=df, aes(x = x, y = y)) +
  geom_point(fill="lightgray", color = "black",alpha = 1/3) + 
  facet_wrap(Date ~ ., scales="free") +
  geom_smooth(method=lm, fill="lightgray", formula = y ~ x) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

# Here we create our annotations data frame.
df.annotations <- data.frame(
  Date = rep(summ$Date, ncol(summ)-1),#Number depends on the no. of model evaluation parameters
  label = c(
    paste0("~R == ", summ$R),
    paste0("~MBE == ", summ$MBE)
  )
)

vertical_adjustment = ifelse(grepl("R",df.annotations$label),1.5,3)

p +   
  geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
            hjust = 0, vjust = vertical_adjustment, size= 4, parse = TRUE)

从图中可以看到,对于右侧面板,MBE使用科学记数法,而左侧面板使用非科学记数法。当R应该在非科学记数法中时,我如何在科学记数法中进行MBE?

数据

代码语言:javascript
运行
复制
df = structure(list(Date = structure(c(18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614), class = "Date"), 
    x = c(0.0673029521110164, 0.0854163780659409, 0.134210186321464, 
    0.0643945521023818, 0.0534072925548605, 0.120585968026977, 
    0.171766787541948, 0.0529322841140877, 0.0389151124667283, 
    0.065932816556064, 0.0716104225646635, 0.0480367073546261, 
    0.0537788251427076, 0.100325805042952, 0.162239359083627, 
    0.103732829973447, 0.0970357338162893, 0.10603901085665, 
    0.0519899928560691, 0.0881320707200356, 0.0367170059526139, 
    0.121845733235219, 0.122459218176886, 0.0425359089683653, 
    0.0666875634912124, 0.0464243831679725, 0.0664988270937391, 
    0.0777430803707978, 0.0573249856899528, 0.0888259984184133, 
    0.0520454150074231, 0.0633639832058288, 0.0756093060434679, 
    0.0907648362839996, 0.0409180444460348, 0.117772012019805, 
    0.160504051083046, 0.10242517144965, 0.104937932574452, 0.0527662976624684, 
    0.0566003076702597, 0.14336552685814, 0.0338041955729706, 
    0.0364078719337135, 0.0296346837015176, 0.0405151452292794, 
    0.0326617631513927, 0.0383537061987076, 0.0560937860237775, 
    0.0662505348148382, 0.130741306761004, 0.0382284958039881, 
    0.0377009798726168, 0.047621161734172, 0.0356923968905919, 
    0.0516794984004681, 0.0777089536097421, 0.0879614340700801, 
    0.0664604119732582, 0.0951825936041895, 0.0581670432364461, 
    0.0614073175261606, 0.0621596045142233, 0.125973936545876, 
    0.0518735765008042, 0.0582112269727801, 0.0541847270530605, 
    0.0970492801742658, 0.0855089208077924, 0.0900662233655072, 
    0.0553091996406284, 0.045398746284043, 0.0747212626723072, 
    0.0468059595214467, 0.0715252644180638, 0.0392926648647701, 
    0.0401031176420515, 0.0393664661556453, 0.0454529539530989, 
    0.0432804905060738, 0.0928468023207638, 0.0814411322724173, 
    0.0826901703538189, 0.0394121791543224, 0.0846930951222448, 
    0.0851496310212468, 0.0800223696662629, 0.0739057775607218, 
    0.0654718923457129, 0.0597189681733721, 0.0518238375517588, 
    0.0811966642411631, 0.0484268967172088, 0.0398812336217731, 
    0.0408737243973982, 0.0722437508279481, 0.0567807507661876, 
    0.108055811705416, 0.0455713198571915, 0.0911207949375208, 
    0.0562274957486631, 0.0453506947793071, 0.0643281114150967, 
    0.0451974642392855, 0.0583516194132318, 0.0603141601370974, 
    0.0474342976516535, 0.0665490689121699, 0.0656781730798187, 
    0.0809471751886296, 0.0580192064282883), y = c(0.06919, 0.06378, 
    0.1046, 0.0963, 0.08835, 0.14248, 0.13096, 0.09829, 0.04209, 
    0.05926, 0.06029, 0.06975, 0.07061, 0.09682, 0.14988, 0.13158, 
    0.04941, 0.06607, 0.08155, 0.09832, 0.05956, 0.14679, 0.08054, 
    0.04512, 0.0705, 0.05925, 0.08821, 0.09841, 0.08305, 0.07885, 
    0.05957, 0.05032, 0.06938, 0.04775, 0.0528, 0.11204, 0.12149, 
    0.065, 0.09006, 0.05126, 0.05475, 0.11013, 0.06336, 0.04926, 
    0.03237, 0.04511, 0.04268, 0.04304, 0.0537, 0.07389, 0.10452, 
    0.05579, 0.05091, 0.0618, 0.04891, 0.05373, 0.07665, 0.07958, 
    0.05465, 0.08265, 0.06703, 0.04129, 0.05235, 0.11724, 0.06877, 
    0.07079, 0.06365, 0.10306, 0.07579, 0.07818, 0.04264, 0.05456, 
    0.07916, 0.05683, 0.06361, 0.05798, 0.06058, 0.04364, 0.05638, 
    0.05134, 0.07837, 0.08028, 0.07875, 0.06031, 0.09279, 0.07135, 
    0.07104, 0.06089, 0.04861, 0.05516, 0.05096, 0.07485, 0.06711, 
    0.06142, 0.05964, 0.06668, 0.06389, 0.08934, 0.03986, 0.08812, 
    0.05291, 0.04459, 0.04609, 0.05507, 0.04233, 0.04497, 0.05065, 
    0.05746, 0.08027, 0.06171, 0.05248)), row.names = c(NA, 111L
), class = "data.frame")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-12 18:48:28

使用包scales并保留parse = F。我还调整了注释的文本格式。

代码语言:javascript
运行
复制
library(scales)
# Here we create our annotations data frame.
df.annotations <- data.frame(
  Date = rep(summ$Date, ncol(summ)-1),#Number depends on the no. of model evaluation parameters
  label = c(
    paste0("  R = ", summ$R),
    # paste0("~MBE == ", summ$MBE)
    paste0("  MBE = ", scientific(summ$MBE, digits = 3)) # set format
  )
)

vertical_adjustment = ifelse(grepl("R",df.annotations$label),1.5,3)

p +   
  geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
            hjust = 0, vjust = vertical_adjustment, size= 4, parse = F)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67947567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档