R语言 使用斜体和下标格式化geom_text字符串

llycmphe  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(442)

我想格式化我的geom_text字符串,使它更好。
1.我需要把“max”和“min”作为Tmax和Tmin的下标。
1.我需要在每个方面的方程中斜体显示两个变量(Tmax/Tmin和Year)。
1.我还需要图例中的下标。
我知道T[max]加上Parse = TRUE会给予我下标,但是正如你在代码中看到的,我使用paste0在我的字符串中添加其他细节,所以当我尝试创建一个复杂的字符串时,我不能这样做。
我也知道italic(Tmax)会使它变成Parse = TRUE的斜体,但是我有一个复杂的字符串,所以这也不起作用。
会很感激你的帮助。
最小可复制代码:

library(ggplot2)

################### Statistics Data ##############################

fitted2<-structure(list(Zone = structure(c(3L, 3L, 3L, 2L, 2L, 2L, 1L, 
                                           1L, 1L), .Label = c("Low", "Intermediate", "High"), class = "factor"), 
                        Variable = c("Precipitation", "Tmax", "Tmin", "Precipitation", 
                                     "Tmax", "Tmin", "Precipitation", "Tmax", "Tmin"), mean = list(
                                       532, 14.5, 2.7, 457, 14.7, 2.36, 364, 17, 2.8), sd = list(
                                         108, 1.15, 0.724, 107, 1.17, 1.13, 85.4, 1.13, 0.801), 
                        W = c(0.98, 0.91, 0.96, 0.98, 0.91, 0.97, 0.99, 0.85, 0.94
                        ), Shapiro_p_value = c(0.11, 0, 0.02, 0.41, 0, 0.05, 0.59, 
                                               0, 0), intercept = c(2070, -0.139, -7.69, 754, -1.96, 13.5, 
                                                                    -478, 17, 14.8), slope = c(-0.78, 0.01, 0.01, -0.15, 0.01, 
                                                                                               -0.01, 0.43, 0, -0.01), LM_p_value = c(0.117, 0.1636, 0.1141, 
                                                                                                                                      0.7621, 0.1189, 0.2791, 0.2799, 0.9987, 0.0976), Adj_r_squared = c(0.0181, 
                                                                                                                                                                                                         0.0118, 0.0186, -0.0112, 0.0178, 0.0023, 0.0022, -0.0123, 
                                                                                                                                                                                                         0.0216), MK_tau = c(-0.149, 0.228, 0.196, -0.021, 0.24, -0.043, 
                                                                                                                                                                                                                             0.117, 0.122, -0.093), MK_p_value = c(0.047, 0.002, 0.009, 
                                                                                                                                                                                                                                                                   0.783, 0.001, 0.571, 0.118, 0.103, 0.214), sen_slope = c(-0.92, 
                                                                                                                                                                                                                                                                                                                            0.014, 0.007, -0.152, 0.016, -0.003, 0.707, 0.007, -0.004
                                                                                                                                                                                                                                                                   ), plotlevels = c("(c)", "(c)", "(c)", "(b)", "(b)", "(b)", 
                                                                                                                                                                                                                                                                                     "(a)", "(a)", "(a)")), row.names = c(NA, -9L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                              "tbl", "data.frame"))

############################################################

######### Plot points data ####################################

t_yearly_agg_long<-structure(list(Wateryear = c(2016, 2016, 2017, 2017, 2018, 2018, 
                                                2019, 2019, 2020, 2020, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 
                                                2019, 2020, 2020, 2016, 2016, 2017, 2017, 2018, 2018, 2019, 2019, 
                                                2020, 2020), Zone = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                                                3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
                                                                                1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Low", "Intermediate", "High"
                                                                                ), class = "factor"), Temp_variable = c("Tmax", "Tmin", "Tmax", 
                                                                                                                        "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", 
                                                                                                                        "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", 
                                                                                                                        "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", "Tmin", "Tmax", 
                                                                                                                        "Tmin", "Tmax", "Tmin"), `Temperature (C)` = c(16, 4, 15, 3, 
                                                                                                                                                                       15, 3, 15, 3, 15, 3, 16, 3, 15, 3, 15, 3, 15, 3, 16, 3, 19, 4, 
                                                                                                                                                                       16, 1, 16, 1, 17, 3, 18, 3)), row.names = c(NA, -30L), class = c("tbl_df", 
                                                                                                                                                                                                                                        "tbl", "data.frame"))

##########################################################

#### Separate precipitation and temperature from statistics table ####
fitted_temp<-fitted2[fitted2$Variable!="Precipitation",]

fitted_temp_max<-fitted2[fitted2$Variable=="Tmax",]
fitted_temp_min<-fitted2[fitted2$Variable=="Tmin",]
nms<-colnames(fitted_temp)
nms[2]<-"Temp_variable"

nms_max<-colnames(fitted_temp_max)
nms_max[2]<-"Temp_variable"

nms_min<-colnames(fitted_temp_min)
nms_min[2]<-"Temp_variable"
colnames(fitted_temp)<-nms
colnames(fitted_temp_max)<-nms_max
colnames(fitted_temp_min)<-nms_min
####################################################################

#### Color specification
colls <- c("Tmax" ="#252525","Tmin"="#969696" )

my.formula<-(y~x)

zone_ord<-c("Low", "Intermediate","High")
t_yearly_agg_long$Zone<-factor(t_yearly_agg_long$Zone, levels = zone_ord)

### Plot starts here ###############

ggplot(t_yearly_agg_long, aes(Wateryear,`Temperature (C)`, color = Temp_variable))+
  geom_point(size = 1.9)+
  geom_line(size = 1.1)+
  scale_color_manual(values=colls)+
  ylab("Temperature (C)")+
  scale_y_continuous(limits=c(-5,40), n.breaks = 5)+
  facet_wrap(~Zone,ncol=1)+
  
#### Add facet labels myself ###  
geom_text(data=fitted_temp_max,
          aes(x = 2015, y = 38, label = plotlevels), size = 7, fontface = "bold", show.legend = FALSE)+
##################################
theme(legend.direction = "vertical",
      legend.position = c(.94, .94),
      legend.text = element_text(size = 15),
      strip.text.x = element_blank())+
  theme(panel.background = element_rect(fill = "white", color = "#FAC213"),
        plot.background = element_rect(fill = "transparent", color = NA_character_))+ 

######## NEED HELP HERE ##############
   
  geom_text(data = fitted_temp_max,
            aes(x=2016, y = 38, label = paste0("Tmax, Mean = ",mean,", Tmax =",intercept, ifelse(slope>=0, "+",""),slope,"Year"), color = Temp_variable), hjust=0, size = 5.5, show.legend = FALSE)+
  geom_text(data = fitted_temp_min,
            aes(x=2016, y = 38-10, label = paste0("Tmin, Mean = ",mean,", Tmin =",intercept, ifelse(slope>=0, "+",""),slope,"Year"), color = Temp_variable), hjust=0, size = 5.5, show.legend = FALSE)
  
############################################
dzjeubhm

dzjeubhm1#

下面是一个可以节省大量代码的解决方案。
为了在图例中使用下标,我将scale_color_manual替换为:

scale_color_manual(labels = c(
    Tmax = expression(T['max']),
    Tmin = expression(T['min'])),
    values = colls, name=""
  )

为了在geom_text中使用 * 下标 * 和 * 斜体 *,我在你的基础上做了2个新的DF,并添加了一个名为label_maxlabel_min的列,其中包含正确的表达式。

df.annotation_max <- fitted_temp_max |>
  mutate(
    label_max =
      paste(
        "list(italic(T[max]), Mean==", mean,
        ",T[max] ==", intercept,
        "+", slope, " ~italic(Year)",
        ")"
      )
  )

df.annotation_min <- fitted_temp_min |>
  mutate(
    label_min =
      paste(
        "list(italic(T[min]), Mean==", mean,
        ",T[min] ==", intercept,
        "+", slope, " ~italic(Year)",
        ")"
      )
  )

ggplot-call中,我已将您的geom_text交换为:

geom_text(data = df.annotation_max,
          aes(x=2016, y = 38, label = label_max), 
          hjust = 0, parse = T, show.legend = F)+
geom_text(data = df.annotation_min,
          aes(x = 2016, y = 38 - 10, label = label_min),
          hjust = 0, parse = T, show.legend = F)

这里geom_text指的是上述DF。
结果你得到这个:

brccelvz

brccelvz2#

您需要的是fontface=“italic”参数R cookbook提供以下内容:

dat <- data.frame(
y = 1:3,
text = c("This is text", "Text with\nmultiple lines", "Some more text"))

library(ggplot2)
p <- ggplot(dat, aes(x=1, y=y)) + 
   scale_y_continuous(limits=c(0.5, 3.5), breaks=NULL) +
   scale_x_continuous(breaks=NULL)

p + geom_text(aes(label=text))

p + geom_text(aes(label=text), family="Times", fontface="italic", lineheight=.8) +
annotate(geom="text", x=1, y=1.5, label="Annotation text", colour="red",
         size=7, family="Courier", fontface="bold", angle=30)

相关问题