R Normalizing a dataset in a specific way - r
I have a dataset which contains 'hits' at each position in a genome. I want to normalize it in a very specific way:
When the column df$HC contains the value 'HC',
Take the value from df$pos which contains the position in bp,
Sum up df$Hits +/-1000bp away from the one in question e.g. if df$pos = 3000, add up hits where df$pos>=2000 and <=4000,
Divide every df$Hits value for those 2000 positions by the total worked out in step 3.
So, each 2000bp patch around each instance of 'HC' (most values in the HC column are NA and don't need to be normalized), has each hit divided by the total number of hits in that patch.
I guess I might be able to do this by subsetting each block of 2000bp around each 'HC' and processing them seperately, but there are ~3000 'HC' positions.
Edit: Due to regions where 'HC+/-1000bp' regions overlap, I think now that I need to extract and process each region seperately, so regions of overlap would be repeated in each subset.
Thanks for any help with this, it's so confusing I have a headache!
dput sample dataframe (due to the character limit it only contains 1000 lines, so try a smaller window than 2000bp):
structure(list(chr = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), HC = structure(c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), .Label = "HC", class = "factor")), .Names = c("chr",
"pos", "Hits", "HC"), class = "data.frame", row.names = c(NA,
-1000L))
A smaller sample dataset and expected output:
pos <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Hits <- c(0, 1, 1, 2, 2, 3, 2, 2, 1, 1)
HC <- c(NA, NA, NA, NA, NA, 'HC', NA, NA, NA, NA)
df <- data.frame(pos, Hits, HC)
#total hits in a +/-3bp window around HC = 13
#divide each read in the window by 13:
Hits <- c(0, 1, 0.077, 0.154, 0.154, 0.231, 0.154, 0.154, 0.077, 1)
Okay, this should cover at least the simplified problem:
n <- 3
len <- length(df[['Hits']])
for(i in which(df[['HC']] %in% 'HC')){
ran <- max(i-n,1):min(i+n,len)
reg <- df[['Hits']][ran]
s <- sum(reg)
reg <- reg / s
df[['Hits']] <- replace(df[['Hits']],ran,reg)
}
fiddle
Related
ggsurvplot can't get cumulative events table font size to change
I started with this answer as a base for my ggsurvplot graph, and I'm using their reproducible code for my question as well. My problem is I can't figure out how to change the font size for the cumulative event table. Based on this documentation, it looks like I should be able to specify cumevents.fontsize = x, but no matter what value I put in for that, the font size does not change. I'm trying to set it to teh same size as the risk.table.fontsize, but you can see that they are different in the graph below. The graph was computed with this library(survival) library(survminer) library(ggplot2) fit <- survfit(Surv(p$time.recur.months, p$recurrence) ~ p$simpson.grade, conf.type="log", data=p) ggsurvplot( fit, data = p, risk.table = TRUE, risk.table.fontsize = 2, cumevents = TRUE, cumevents.fontsize =2, pval = TRUE, pval.coord = c(0, 0.25), conf.int = F, legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3", "Simpson Grade 4"), size=c(0.7,0.7,0.7,0.7), xlim = c(0,100), alpha=c(0.7), break.time.by = 10, xlab="Time in months", #ylab="Survival probability", ggtheme = theme_gray(), risk.table.y.text.col = T, risk.table.y.text = TRUE, ylim=c(0,0.5), palette="Set1" ) My Data p <- structure(list(recurrence = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), time.recur.months = c(NA, NA, NA, NA, NA, NA, 92L, NA, NA, NA, 74L, NA, NA, NA, 2L, 8L, NA, NA, NA, NA, 58L, NA, NA, NA, NA, NA, 3L, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39L, NA, NA, NA, NA, 15L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 12L, 56L, 57L, NA, NA, 49L, 17L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9L, NA, 89L, NA, NA, NA, 8L, 6L, 8L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 60L, NA, NA, 38L, NA, NA, NA, NA, NA, 90L, NA, 58L, 54L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 53L, NA, NA, 124L, NA, NA, NA, NA, NA, NA, 7L, NA), simpson.grade = c(3L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 2L, 1L, 2L, 1L, 4L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 3L, 1L, 1L, 4L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 1L, 2L, 4L, 4L, 1L, 4L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 1L, 4L, 4L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 4L, 4L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 2L, 4L, 1L, 4L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L)), .Names = c("recurrence", "time.recur.months", "simpson.grade"), class = "data.frame", row.names = c(NA, -176L))
I can't see the documentation for a cumevents.fontsize argument, but a straightforward way to change it is to store the plot and change it directly: mygg <- ggsurvplot( fit, data = p, risk.table = TRUE, risk.table.fontsize = 2, cumevents = TRUE, cumevents.fontsize =2, pval = TRUE, pval.coord = c(0, 0.25), conf.int = F, legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3", "Simpson Grade 4"), size=c(0.7,0.7,0.7,0.7), xlim = c(0,100), alpha=c(0.7), break.time.by = 10, xlab="Time in months", #ylab="Survival probability", ggtheme = theme_gray(), risk.table.y.text.col = T, risk.table.y.text = TRUE, ylim=c(0,0.5), palette="Set1" ) So now you can do: mygg$cumevents$layers[[1]]$aes_params$size <- 2 mygg or mygg$cumevents$layers[[1]]$aes_params$size <- 8 mygg
Here is a suggestion how to get a clean table: See here https://github.com/kassambara/survminer/issues/117 p1 <- ggsurvplot( fit, data = p, risk.table = TRUE, cumevents = TRUE, risk.table.title = "No. at Risk", cumevents.title = "Cumulative No. of Events", risk.table.height = 0.15, cumevents.height = 0.15, risk.table.y.text = TRUE, cumevents.y.text = TRUE, pval = TRUE, pval.coord = c(0, 0.25), conf.int = F, legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3", "Simpson Grade 4"), size=c(0.7,0.7,0.7,0.7), xlim = c(0,100), alpha=c(0.7), break.time.by = 10, xlab="Time in months", ylab="Survival probability", ggtheme = theme_gray(), risk.table.y.text.col = T, # risk.table.y.text = TRUE, ylim=c(0,0.5), palette="Set1" ) p1$table <- p1$table + theme_cleantable() p1$cumevents <- p1$cumevents + theme_cleantable() p1
R Creating a p-value matrix with missing values
I have a dataframe with many missing NAs. I want to create a correlation matrix with a p-value matrix as shown in this link: Link I created the correlation matrix like this: as.data.frame(round(cor(df, use = "pairwise.complete.obs", method = c("spearman")), 1)) Now I am trying to create a matrix that shows the p-values for each correlation. I have used this code successfully for other dataframes, which include less NAs. cor.mtest <- function(mat) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(NA, n, n) diag(p.mat) <- 0 for (i in 1:(n - 1)) { for (j in (i + 1):n) { tmp <- cor.test(mat[, i], mat[, j]) p.mat[i, j] <- p.mat[j, i] <- tmp$p.value } } colnames(p.mat) <- rownames(p.mat) <- colnames(mat) p.mat } p.mat <- cor.mtest(df) But now I am getting an error: Error in cor.test.default(mat[, i], mat[, j]) : not enough finite observations I also tried to use the "Hmisc" package for the rcorr-function. But the package does not load correctly. Any idea how to solve this? structure(list(V1 = c(21L, 18L, 11L, 20L, 17L, 18L, 20L, 23L, 10L, 25L, 11L, 24L, 13L, 17L, 30L, 12L, 24L, 27L, 19L, 24L, 14L, 14L, 10L, 21L, 12L, 14L, 19L, 19L, 16L, 15L, 25L, 15L, 20L, 18L, 21L, 9L, 18L, 10L, 21L, 17L, 15L, 6L, 21L, 27L, 16L, 15L, 20L, 12L, 20L, 11L, 17L, 14L, 22L, 14L, 18L, 17L, 19L, 18L, 16L, 13L, 11L, 19L, 14L, 9L, 13L, 13L, 8L, 7L, 29L, 14L, 16L, 13L, 8L, 28L, 12L, 33L, 20L, 13L, 12L, 14L, 16L, 15L, 23L, 19L, 20L, 23L, 21L, 14L, 12L, 30L, 11L, 12L, 14L, 13L, 15L, 13L, 6L, 15L, 19L, 15L, 18L, 23L, 19L, 11L, 18L, 9L, 18L, 17L, 15L, 8L, 13L, 8L, 20L, 17L, 25L, 11L, 25L, 19L, 13L, 15L, 15L, 15L, 12L, 16L, 20L, 13L, 24L, 12L, 23L, 21L, 15L, 18L, 14L, 20L, 21L, 20L, 19L, 21L, 11L, 24L,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, 31.9, 8.7, 7.9, 30.6, 7.9, 17.2, 24.7, 26.1, 22, 29, -6.3, 30.9, 5.7, 11.7, 28.1, 22.9, 12.2, 29.7, 2.7, 5.5, 19.7, 17.8, 24, 28.6, 24.4, 20, 29.1, 13.7, 8.7, 12, 8.8, 10.4, 9.7, 10, 19.6, -0.5, 25.6, 17.9, 14.2, 12, 3.6, 2.9, 5.9, 26.7, 8.7, 20.9, 0.8, 10.5, 14.3, 19.5, -0.3, 28.8, 26.5, 4.9, -0.5, 23.8, -1.3, 12.1, 2.4, 17.2, 22.1, 23.5, 17, -0.9, 19.3, 4.9, 20.1, 12.2, 10.8, 31.6, 26.1, 2.5, 26.7, 7.5, 8.2, 11.8, 22.3, 28.3, 21.4, 25.4, -0.4, 11.4, 27, 9.3, 23.6, 19.9, 23.5, 19.2, 6.7, 18.9, 2.8, 28, 9.6, 15.2, 13.1, 0, 22.7, 5.7, 3, 4.7, 9.9, 21.9, -1.6, 19, 11, 17.2, 12.9, 27.4, 21.5, 14.3, 4.5, 6.1, 23.1, -0.1, 5.1, 18.7, 3.7, 10.1, 22.6, 16.1, 7.9, 0.9, 30.8, 2.6, 30.3, 25.9, 20.5, 5.2, 26.9, 22.9, 24.8, 19.6, 10.7, 14.9, 21.9, 24.5, 21, 11.3, 1.5, 17.6, -8.8, 5.3, -1.2, 29.1, 22.6, 6.7, 24.6, 22.2, 1.9, 12.8, 19.6, 20.5, 15, 2.9, 27.2, 16.5, -1.4, 17.1, 8.2, 16, 4.2, 6.6, 19.8, -4.8, 21.7, 27.7, 4.3, 0.4, 25.4, 27.2, 28.7, 17.9, 22.7, 8.9, 22.1, 16.3, 5.4, 15.3, 9.9, 30.2, 14.7, 14.2), V73 = c(NA, NA, NA, -0.09275986, NA, NA, 0.52943606, NA, NA, NA, 0.39573934, NA, NA, 0.06665112, NA, NA, NA, NA, 0.09889552, NA, NA, 0.52411667, NA, NA, 0.0786277, 0.39117113, NA, 0.30804176, NA, 0.4984171, NA, NA, 0.69054695, 0.61838979, NA, 0.49298138, NA, NA, NA, NA, NA, 0.44718356, NA, 0.24114516, 0.00855375, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.31341432, NA, NA, NA, NA, NA, NA, 0.38816502, NA, 0.69810769, NA, NA, NA, 0.46607416, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.39012246, NA, NA, NA, NA, 0.42507386, NA, NA, -0.26830461, NA, NA, 0.29439447, NA, NA, NA, 0.18582551, -0.00246774, 0.33244636, 0.26097549, NA, NA, 0.56932173, NA, 0.33573443, NA, NA, NA, NA, NA, NA, 0.74612433, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.02980432, NA, NA, NA, NA, NA, NA, 0.60470877, NA, NA, 0.29230953, NA, -0.11296095, 0.09783287, NA, NA, 0.32181372, NA, NA, NA, NA, NA, NA, 0.3255947, 0.4099077, NA, NA, NA, NA, NA, NA, 0.42345733, 0.29293533, NA, 0.52832981, NA, NA, NA, NA, NA, NA, NA, 0.55373453, NA, NA, NA, NA, NA, NA, NA, 0.4070331, NA, 0.30780722, 0.59547858, NA, NA, 0.66333634, NA, 0.38209532, NA, NA, NA, NA, NA, NA, NA, NA, 0.35778449, NA, NA), V77 = c(NA, NA, 0.45406227, NA, 0.87348132, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.78536916, NA, -0.01870051, NA, NA, NA, NA, NA, NA, -0.00150528, NA, NA, NA, NA, -0.49992833, NA, NA, NA, NA, NA, NA, NA, -0.12002325, -0.16249647, NA, 0.51132754, NA, NA, NA, -0.20643247, 0.59529347, NA, 0.32442411, NA, NA, NA, NA, NA, NA, NA, 0.80611793, NA, NA, NA, NA, NA, NA, NA, 0.75247001, 0.65079036, NA, NA, 0.29773326, -0.2164507, NA, NA, 0.36336748, NA, NA, NA, NA, 0.49664945, NA, NA, NA, 0.35610758, NA, NA, NA, 0.3734933, NA, 0.58752714, NA, NA, NA, -0.38266847, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.28871445, NA, 0.05455121, NA, NA, NA, NA, NA, NA, 0.0408944, NA, NA, NA, NA, NA, NA, 0.87592639, NA, NA, NA, NA, NA, NA, NA, 0.28923257, NA, NA, NA, -0.16730842, NA, -0.122933, 0.25704385, NA, NA, NA, NA, NA, NA, NA, 0.92475694, NA, NA, NA, 0.15886697, 0.51925536, NA, NA, 0.25372613, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.89195925, NA, NA, NA, -0.60877514, NA, 0.33866615, NA, NA, 0.60955791, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.05461735, NA, NA, 0.33697054, NA, -0.12079077, -0.14805299, -0.24541818, NA, 0.36340054, NA ), V81 = c(NA, NA, -0.08490089, NA, 0.0555794, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.22856711, NA, -0.57790508, NA, NA, NA, NA, NA, NA, 0.04856018, NA, NA, NA, NA, -0.38039271, NA, NA, NA, NA, NA, NA, NA, -0.63132241, -0.35266074, NA, 0.01961822, NA, NA, NA, -0.34551275, -0.39085104, NA, -0.27725445, NA, NA, NA, NA, NA, NA, NA, -0.21599455, NA, NA, NA, NA, NA, NA, NA, -0.19924471, -0.18365343, NA, NA, -0.53484587, -0.32543563, NA, NA, -0.19992419, NA, NA, NA, NA, -0.18500223, NA, NA, NA, -0.12990151, NA, NA, NA, -0.39083879, NA, -0.59264661, NA, NA, NA, 0.13154274, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.23261324, NA, -0.03944042, NA, NA, NA, NA, NA, NA, -0.22193873, NA, NA, NA, NA, NA, NA, -0.20022085, NA, NA, NA, NA, NA, NA, NA, 0.08615186, NA, NA, NA, -0.74607469, NA, 0.23032189, 0.0449706, NA, NA, NA, NA, NA, NA, NA, -0.04848046, NA, NA, NA, -0.6370161, -0.02900035, NA, NA, -0.23145663, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.14884929, NA, NA, NA, 0.22450133, NA, 0.24769837, NA, NA, -0.29667428, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.03071992, NA, NA, 0.07786378, NA, 0.23027039, -0.20214392, -0.3032353, NA, -0.47432158, NA), V89 = c(0.0834995, 0.00066815, NA, NA, NA, NA, NA, NA, 0.02511399, NA, NA, NA, 0.052432, NA, NA, NA, -0.14814967, NA, NA, NA, NA, NA, -0.33114922, 0.34514567, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.19468406, NA, NA, NA, -0.38972029, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.23425484, NA, -0.11003854, NA, -0.26367322, NA, NA, 0.29238575, 0.07886438, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.15248164, NA, -0.15641155, NA, NA, -0.08752716, NA, NA, NA, NA, 0.34809891, NA, NA, NA, NA, NA, NA, NA, -0.27401719, NA, NA, NA, NA, NA, -0.32273288, NA, 0.02669399, NA, 0.0727079, 0.08290143, NA, -0.16476099, NA, NA, NA, NA, -0.1029079, -0.11614262, NA, NA, -0.14913232, NA, -0.29380582, -0.537503, 0.11869562, NA, NA, NA, -0.17315201, NA, 0.10272535, 0.0932595, 0.0793467, -0.0845297, NA, NA, NA, -0.02889606, NA, NA, NA, NA, NA, 0.15552849, 0.04599214, NA, 0.19864881, NA, NA, NA, NA, NA, -0.11474285, NA, NA, NA, 0.10901186, NA, NA, NA, 0.13339891, NA, 0.07056403, NA, NA, NA, NA, NA, NA, NA, -0.25760406, 0.2062942, -0.00981489, 0.3282743, 0.06509166, NA, NA, NA, -0.26049214, NA, -0.13281234, NA, 0.32791015, -0.13518787, NA, NA, NA, NA, NA, NA, NA, NA, 0.05660112, NA, NA, 0.12368526, -0.15672689, NA, -0.42175072, NA, NA, NA, NA, NA, -0.22635573), V90 = c(-0.04245051, 0.3507695, NA, NA, NA, NA, NA, NA, 0.32893767, NA, NA, NA, -0.35288827, NA, NA, NA, -0.02734148, NA, NA, NA, NA, NA, -0.01271804, -0.26617777, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.37528838, NA, NA, NA, 0.14921273, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.46296948, NA, -0.20223671, NA, 0.12754582, NA, NA, 0.05006781, 0.22653775, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.26092513, NA, 0.54215354, NA, NA, -0.23136087, NA, NA, NA, NA, -0.04596987, NA, NA, NA, NA, NA, NA, NA, 0.14239809, NA, NA, NA, NA, NA, 0.11650203, NA, 0.17058915, NA, -0.18403288, 0.10295627, NA, -0.15530088, NA, NA, NA, NA, -0.45405281, -0.10929859, NA, NA, 0.14782657, NA, -0.15852471, -0.05266618, -0.18175069, NA, NA, NA, -0.11917474, NA, 0.16136416, -0.14499177, -0.17504283, 0.13272865, NA, NA, NA, -0.17429991, NA, NA, NA, NA, NA, -0.22030747, 0.29022488, NA, 0.05889091, NA, NA, NA, NA, NA, 0.30446594, NA, NA, NA, 0.23796595, NA, NA, NA, 0.14051101, NA, -0.05704354, NA, NA, NA, NA, NA, NA, NA, 0.25256272, -0.14193822, 0.06924969, 0.00445279, 0.29815696, NA, NA, NA, 0.25643083, NA, 0.35649173, NA, -0.25180143, -0.05787895, NA, NA, NA, NA, NA, NA, NA, NA, 0.03069952, NA, NA, -0.18662018, -0.15144552, NA, 0.06595208, NA, NA, NA, NA, NA, 0.32091592)), .Names = c("V1", "V2", "V3", "V6", "V40", "V29", "V56", "V62", "V73", "V77", "V81", "V89", "V90"), class = "data.frame", row.names = c(NA, -200L))
This error happens because you need at least 3 non NA in each pair of data. To solve this, you may want to set p-value = NA when you find an error like this. You can use this variation of the function: cor.mtest <- function(mat) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(NA, n, n) diag(p.mat) <- 0 for (i in 1:(n - 1)) { for (j in (i + 1):n) { error <- try(tmp <- cor.test(mat[, i], mat[, j]), silent =T) if (class(error) == "try-error") { p.mat[i, j] <- NA } else { p.mat[i, j] <- p.mat[j, i] <- tmp$p.value } } } colnames(p.mat) <- rownames(p.mat) <- colnames(mat) p.mat }
Graph with two y axes in r
I was searching for a code to create a nice graph with two y axes. However i couldn't find a solution which helped me. (I am not using r often) I have the time as x variable where i want to plot value1 and value2 on the right and left y achses respectively. My data are in one dataset, so that i want to graph datasat$time on x axis and data$value1 on y left axis and data$value2 on right y axis. The way to illustrate should be a line with two different colours for each (data$value1 and data$value2). In addition there should be a legend for the lines. Can someone help me with it?
Please, find my data w below. Here is a script on how a dobbelt y-axis may be integrated using ggplot ggplot() + geom_bar(mapping = aes(x = w$WHO[w$Death==1]), stat = "count", alpha=0.2, colour="#1C73C2", fill="#ECF0F9") + scale_y_continuous(name = "Number of deaths", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "Mortality rate per 100 person-yrs", breaks=yaks, labels=c("0","5","10","15","20","25","30","35","40","45"))) + scale_x_continuous(name="", breaks = c(1,2,3,4), labels =c("\nWHO-I\nn=37","\nWHO-II\nn=29","\nWHO-III\nn=19","\nUnknown\nn=25")) + coord_cartesian(ylim=c(0, 18)) + geom_point(mapping = aes(x = 1, y = 3.329993), size=5,alpha=0.7, shape=18, colour="red") + geom_point(mapping = aes(x = 2, y = 12.424504), size=5,alpha=0.7, shape=18, colour="red") + geom_point(mapping = aes(x = 3, y = 17.23519), size=5, alpha=0.7,shape=18, colour="red") + geom_point(mapping = aes(x = 4, y = 4.549763), size=5, alpha=0.7, shape=18, colour="red") + annotate("text", x = c(1,2,3,4) , y = c(3.329993+1.3, 12.424504+1.3, 17.23519+1.3,4.549763+1.3 ), label = c("8.3","31.1","43.1","11.4"), col="red", fontface=2, cex=4) + theme(axis.text.y.right = element_text(color = "red", size = 11), axis.title.y=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 15, b = 0, l = 0)), axis.text.y = element_text(color = "#1C73C2", size = 11), axis.title.y.right=element_text(color="darkgrey", size=11,face="bold", margin = margin(t = 0, r = 0, b = 0, l = 15)), axis.text.x = element_text(color = "grey20", size = 11)) My data w w <- structure(list(WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 1L, 4L, 4L, 2L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 3L, 4L, 3L, 4L, 3L), response = c(0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, NA, 1L), Death = c(0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, NA, 1L)), class = "data.frame", row.names = c(NA, -111L))
How can I add specific value on x-axis in ggsurvplot/survminer in R?
I want 56 to show on the x-axis, but I can't figure it out. I have the following script. I have tried to add the following to the script xlim = c(seq(0,100, by=10),56) but that does not seem to work. I have tried to google it and I have read on R-documentation. I hope you can help. library(survival) library(survminer) library(ggplot2) fit <- survfit(Surv(p$time.recur.months, p$recurrence) ~ p$simpson.grade, conf.type="log", data=p) j <- ggsurvplot( fit, data = p, fun="cumhaz", risk.table = TRUE, pval = TRUE, pval.coord = c(0, 0.25), conf.int = F, legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3", "Simpson Grade 4"), size=c(0.7,0.7,0.7,0.7), xlim = c(0,100), alpha=c(0.7), break.time.by = 10, xlab="Time in months", #ylab="Survival probability", ggtheme = theme_gray(), risk.table.y.text.col = T, risk.table.y.text = TRUE, ylim=c(0,0.5), cumevents=T, palette="Set1" ) # My Data p <- structure(list(recurrence = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), time.recur.months = c(NA, NA, NA, NA, NA, NA, 92L, NA, NA, NA, 74L, NA, NA, NA, 2L, 8L, NA, NA, NA, NA, 58L, NA, NA, NA, NA, NA, 3L, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39L, NA, NA, NA, NA, 15L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 12L, 56L, 57L, NA, NA, 49L, 17L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9L, NA, 89L, NA, NA, NA, 8L, 6L, 8L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 60L, NA, NA, 38L, NA, NA, NA, NA, NA, 90L, NA, 58L, 54L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 53L, NA, NA, 124L, NA, NA, NA, NA, NA, NA, 7L, NA), simpson.grade = c(3L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 2L, 1L, 2L, 1L, 4L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 3L, 1L, 1L, 4L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 1L, 2L, 4L, 4L, 1L, 4L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 1L, 4L, 4L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 4L, 4L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 2L, 4L, 1L, 4L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L)), .Names = c("recurrence", "time.recur.months", "simpson.grade"), class = "data.frame", row.names = c(NA, -176L))
j is a ggsurvplot object, which is in turn a list of other objects. You can change the ggplot object at j$plot. The following will add 56 to the x-axis labels: j$plot <- j$plot + scale_x_continuous(breaks = sort(c(seq(0, 100, 10), 56))) j Personally I don't think it's a good look, as I expect evenly spaced breaks along the x-axis to match the tables below. If you want to draw attention to the position 56, I would suggest a vertical line and/or annotated label instead: j$plot <- j$plot + geom_vline(xintercept = 56, linetype = "dashed") + annotate("text", x = 56, y = 0, label = "56", hjust = -0.5) j
Change label name in ggsurvplot
I have attached My Data below. I wish to relabel "Cumulative number of events", which seem to be the default text. I would like it to read: "Cumulative number of recurrences". I can't seem to figure out how to change it - is it even possible to change the text? My graph looks like this: The graph was computed with this library(survival) library(survminer) library(ggplot2) fit <- survfit(Surv(p$time.recur.months, p$recurrence) ~ p$simpson.grade, conf.type="log", data=p) j <- ggsurvplot( fit, data = p, fun="cumhaz", risk.table = TRUE, pval = TRUE, pval.coord = c(0, 0.25), conf.int = F, legend.labs=c("Simpson Grade 1" ,"Simpson Grade 2", "Simpson Grade 3", "Simpson Grade 4"), size=c(0.7,0.7,0.7,0.7), xlim = c(0,100), alpha=c(0.7), break.time.by = 10, xlab="Time in months", #ylab="Survival probability", ggtheme = theme_gray(), risk.table.y.text.col = T, risk.table.y.text = TRUE, ylim=c(0,0.5), cumevents=T, palette="Set1" ) My Data p <- structure(list(recurrence = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), time.recur.months = c(NA, NA, NA, NA, NA, NA, 92L, NA, NA, NA, 74L, NA, NA, NA, 2L, 8L, NA, NA, NA, NA, 58L, NA, NA, NA, NA, NA, 3L, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39L, NA, NA, NA, NA, 15L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 12L, 56L, 57L, NA, NA, 49L, 17L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9L, NA, 89L, NA, NA, NA, 8L, 6L, 8L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 60L, NA, NA, 38L, NA, NA, NA, NA, NA, 90L, NA, 58L, 54L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 53L, NA, NA, 124L, NA, NA, NA, NA, NA, NA, 7L, NA), simpson.grade = c(3L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 2L, 1L, 2L, 1L, 4L, 2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 3L, 1L, 1L, 4L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 4L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 1L, 2L, 4L, 4L, 1L, 4L, 4L, 1L, 2L, 1L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 1L, 4L, 4L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 4L, 4L, 1L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 2L, 4L, 1L, 4L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L)), .Names = c("recurrence", "time.recur.months", "simpson.grade"), class = "data.frame", row.names = c(NA, -176L))
To change title for cumevents table you need to use argument cumevents.title. ggsurvplot(fit, p, fun = "cumhaz", risk.table = TRUE, cumevents = TRUE, pval = TRUE, pval.coord = c(0, 0.25), conf.int = FALSE, legend.labs = paste("Simpson Grade", 1:4), xlab = "Time in months", cumevents.title = "Cumulative number of recurrences", size = rep(0.7, 4), xlim = c(0, 100), ylim = c(0, 0.5), alpha = 0.7, break.time.by = 10, ggtheme = theme_gray(), risk.table.y.text.col = TRUE, risk.table.y.text = TRUE, palette = "Set1")