Home > Software design >  Formatting the geom_text string with italics and subscript
Formatting the geom_text string with italics and subscript

Time:12-13

I would like to format my geom_text string and make it nicer.

  1. I need to make 'max' and 'min' as subscript in Tmax and Tmin.
  2. I need to italicize the two variables (Tmax/Tmin and Year) in the equations in each facet.
  3. I need subscript in the legend as well.

I know T[max] with Parse = TRUE will give me the subscript. But as you can see in the code, i am using paste0 to add other detail in my string. So i am not able to do this when i try to make a complex string.

I also know italic(Tmax) will make it italic with Parse = TRUE but again i have a complex string so this doesn't work either.

Would appreciate any help with this.

Minimum reproducible code:

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)
  
############################################

CodePudding user response:

What you are looking for is fontface="italic" argument enter image description here

  • Related