Samrat
Samrat

Reputation: 89

Adding subscript to the text in stat_poly_eq

I am making a graph where i need to add the text label to my stat values in stat_poly_eq().

  1. I need to add Tmax and Tmin, but i need the "max" and "min" as a subscript which i am not able to achieve.
  2. I also need space between the words which i am obtaining using ~' '~ but when i do this the space between words is slightly larger than using (see image). Is there better way to provide the space?
  3. In my graph, how do i make my whole legend "hollow" so that it doesnt sit over my line graph. I cannot move the legend to the corner because i need to align the legend lines with the three statistics text.

I would appreciate any help regarding these issues.

Reproducible example:


### creating fake data ####
Year<-seq(1914,2020,1)
PRCP<-runif(107, min=700, max=1100)
TMIN<-runif(107,min=5,max=8)
TMAX<-runif(107,min=15,max=18)

####matrix ####
mat<-as.data.frame(cbind(Year,PRCP,TMAX,TMIN))

##### factor for equation ####
cofact<-0.014
add<-1400
my.formula<- y~x
####################

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

p<-  ggplot(data = mat, mapping = aes(x = Year, y = PRCP, group = 1)) + 
  geom_line(aes(y= PRCP,color="PRCP"),alpha=0)+
  geom_line(mapping = aes(y = ((TMAX/cofact)+add),color="TMAX"), size=0.35) +
  geom_line(mapping = aes(y = ((TMIN/cofact)+add),color="TMIN"), size=0.35) +
  scale_color_manual(NA,values=c("seagreen","darkred","blue4"))+ 
  ##scale data to desired scale for secondary axis ##
  geom_point(mapping = aes(y = ((TMAX/cofact)+add),color="TMAX"), size=1) +
  geom_point(mapping = aes(y = ((TMIN/cofact)+add),color="TMIN"), size=1) +
  guides(colour = guide_legend(override.aes = list(shape = c(NA,16,16), size = 1)),byrow=T)+
  geom_bar(stat = "identity", color='seagreen', fill="seagreen", width = 0.1) +
  scale_x_continuous(n.breaks = 10)+

  scale_y_continuous("Precipitation (mm)",expand = c(0,0),limits = c(0,3000), n.breaks = 8,
                     sec.axis = sec_axis(~ (. - add) * cofact,
                                         breaks = seq(-20,20,5),
                                         name = "Temperature (°C)"))+ # Reverse transformation to match data
  theme_bw()+
  theme(axis.text.y.right = element_text(size = 20),
                         legend.position = c(0.30,0.75),
                         legend.direction = "vertical",
                         # legend.text = element_text(size = 16),
                         legend.text = element_blank(),
                         legend.key = element_rect(colour = "transparent"),
                         # legend.key.size = 6,
                         legend.spacing.y = unit(1, "cm"))

p + 
  stat_poly_eq(aes(y = PRCP, label = paste("Ann.~` `~Precip",..rr.label.., ..p.value.label..,
                                           sep = "*\", \"*")),
               geom = "text_npc",
               formula = my.formula,
               size = 5.4,
               label.x = 0.35,
               label.y = 0.735,
               hjust = "left", vjust = "center")  +
stat_poly_eq(aes(y = ((TMAX/cofact)+add), label = paste("Average~` `~Daily~` `~Tmax",..rr.label.., ..p.value.label..,
                                                        sep = "*\", \"*")),
             geom = "text_npc",
             formula = my.formula,
             size = 5.4,
             label.x = 0.35,
             label.y = 0.685,
             hjust = "left", vjust = "center")+
  stat_poly_eq(aes(y = ((TMIN/cofact)+add),
                   label = paste("Average~` `~Daily~` `~Tmin",..rr.label.., ..p.value.label..,
                                 sep = "*\", \"*")),
               geom = "text_npc",
               formula = my.formula,
               size = 5.4,
               label.x = 0.35,
               label.y = 0.65,
               hjust = "left", vjust = "center")

Graph

Upvotes: 0

Views: 383

Answers (1)

Quinten
Quinten

Reputation: 41387

To achieve your first problem, you can just use [] for your subscript in your labels.

For your second point you could change the legend.spacing.x for space.

For your third point, you can set the legend keys to transparent using this code: legend.key = element_rect(colour = "transparent", fill = "transparent") and set the legends background to element_blank.

You can use the following code:

library(tidyverse)
library(ggpmisc)
p<-  ggplot(data = mat, mapping = aes(x = Year, y = PRCP, group = 1)) + 
  geom_line(aes(y= PRCP,color="PRCP"),alpha=0)+
  geom_line(mapping = aes(y = ((TMAX/cofact)+add),color="TMAX"), size=0.35) +
  geom_line(mapping = aes(y = ((TMIN/cofact)+add),color="TMIN"), size=0.35) +
  scale_color_manual(NA,values=c("seagreen","darkred","blue4"))+ 
  ##scale data to desired scale for secondary axis ##
  geom_point(mapping = aes(y = ((TMAX/cofact)+add),color="TMAX"), size=1) +
  geom_point(mapping = aes(y = ((TMIN/cofact)+add),color="TMIN"), size=1) +
  guides(colour = guide_legend(override.aes = list(shape = c(NA,16,16), size = 1)),byrow=T)+
  geom_bar(stat = "identity", color='seagreen', fill="seagreen", width = 0.1) +
  scale_x_continuous(n.breaks = 10)+
  
  scale_y_continuous("Precipitation (mm)",expand = c(0,0),limits = c(0,3000), n.breaks = 8,
                     sec.axis = sec_axis(~ (. - add) * cofact,
                                         breaks = seq(-20,20,5),
                                         name = "Temperature (°C)"))+ # Reverse transformation to match data
  theme_bw()+
  theme(axis.text.y.right = element_text(size = 20),
        legend.position = c(0.30,0.75),
        legend.direction = "vertical",
        # legend.text = element_text(size = 16),
        legend.text = element_blank(),
        legend.key = element_rect(colour = "transparent", fill = "transparent"),
        # legend.key.size = 6,
        legend.spacing.y = unit(1, "cm"),
        legend.spacing.x = unit(-1, "cm"),
        legend.background = element_blank()) 

p + 
  stat_poly_eq(aes(y = PRCP, label = paste("Ann.~` `~Precip",..rr.label.., ..p.value.label..,
                                           sep = "*\", \"*")),
               geom = "text_npc",
               formula = my.formula,
               size = 5.4,
               label.x = 0.35,
               label.y = 0.735,
               hjust = "left", vjust = "center")  +
  stat_poly_eq(aes(y = ((TMAX/cofact)+add), label = paste("Average~` `~Daily~` `~T[max]",..rr.label.., ..p.value.label..,
                                                          sep = "*\", \"*")),
               geom = "text_npc",
               formula = my.formula,
               size = 5.4,
               label.x = 0.35,
               label.y = 0.685,
               hjust = "left", vjust = "center")+
  stat_poly_eq(aes(y = ((TMIN/cofact)+add),
                   label = paste("Average~` `~Daily~` `~T[min]",..rr.label.., ..p.value.label..,
                                 sep = "*\", \"*")),
               geom = "text_npc",
               formula = my.formula,
               size = 5.4,
               label.x = 0.35,
               label.y = 0.65,
               hjust = "left", vjust = "center")

Output:

enter image description here

Upvotes: 1

Related Questions