How to remove the default "variable name= " label, and instead retain only value of variable

126 views Asked by At

I want to have cleaner names for the Y axes in my stratified graph below

Eg: Gender variable called "gender2", has two levels "Male", "Female". Upon plotting,

AGsex=Recur(exityrs,id,as.integer(anyhpv))~gender2
plotEvents(AGsex,xlab="Time in years",base_size=5,)

i get Recur object of subjects followup trajectories in study, with a reccurent event marked by a green dot I would like the stratified y axes to say "Male" and "female" only..would this be a feature of the plot function? I.e is there a easy way of doing this in the plot code itself instead of having to create temporary data frames etc

1

There are 1 answers

0
nrennie On

This functionality seems to be hard coded into the plotEvents() function so I don't think there's a built-in option for this. However, since it returns a ggplot object, you can modify the facetting layer and edit the labels:

# minimal example
library(reda)
library(reReg)
gender2 <- rep(c("Male", "Female"), 5)
ex1 <- Recur(1:10)~gender2
g = plotEvents(ex1) 

# function to remove the gender2 part of the label from a string
rm_label <- function(string, to_rm = "gender2 = ") {
  string <- stringr::str_remove(string, to_rm)
  string
}

# remake the plot and add your own facets
g + 
  facet_wrap(~gender2, ncol = 1, strip.position = "left",
             labeller = labeller(gender2 = rm_label))

gives:

plot