Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add customization of line colors - ksh #26

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: jskm
Title: Kaplan-Meier Plot with 'ggplot2'
Version: 0.5.3
Version: 0.5.4
Date: 2024-01-24
Authors@R: c(person("Jinseob", "Kim", email = "jinseob2kim@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9403-605X")),
person("yoonkyoung", "Chun", email = "rachel200357@gmail.com", role = "aut"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# jskm 0.5.4

* Update: Add customization of line colors to `jskm` and `svyjskm`

# jskm 0.5.3

* Update: Add theme('JAMA','NEJM') to `jskm` and `svyjskm`
Expand Down
29 changes: 22 additions & 7 deletions R/jskm.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @param subs = NULL,
#' @param label.nrisk Numbers at risk label. Default = "Numbers at risk"
#' @param size.label.nrisk Font size of label.nrisk. Default = 10
#' @param linecols Character. Colour brewer pallettes too colour lines. Default ="Set1", "black" for black with dashed line.
#' @param linecols Character or Character vector. Colour brewer pallettes too colour lines. Default ="Set1", "black" for black with dashed line, character vector for the customization of line colors.
#' @param dashed logical. Should a variety of linetypes be used to identify lines. Default = FALSE
#' @param cumhaz Show cumulative incidence function, Default: F
#' @param cluster.option Cluster option for p value, Option: "None", "cluster", "frailty", Default: "None"
Expand Down Expand Up @@ -313,7 +313,7 @@ jskm <- function(sfit,
# specifying axis parameteres etc #
###################################

if (dashed == TRUE | linecols == "black") {
if (dashed == TRUE | all(linecols == "black")) {
linetype <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash", "1F", "F1", "4C88C488", "12345678")
} else {
linetype <- c("solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid")
Expand All @@ -330,7 +330,7 @@ jskm <- function(sfit,


linecols2 <- linecols
if (linecols == "black") {
if (all(linecols == "black")) {
linecols <- "Set1"
p <- ggplot2::ggplot(df, aes(x = time, y = surv, linetype = strata)) +
ggtitle(main)
Expand Down Expand Up @@ -380,24 +380,39 @@ jskm <- function(sfit,
geom_step(data = subset(df, time >= cut.landmark), linewidth = linewidth) + geom_step(data = subset(df, time < cut.landmark), linewidth = linewidth)
}

brewer.palette <- c("BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYlBu", "RdYlGn", "Spectral", "Accent", "Dark2", "Paired", "Pastel1", "Pastel2",
"Set1", "Set2", "Set3", "Blues", "BuGn", "BuPu", "GnBu", "Greens", "Greys", "Oranges", "OrRd", "PuBu", "PuBuGn", "PuRd", "Purples",
"RdPu", "Reds", "YlGn", "YlGnBu", "YlOrBr", "YlOrRd")

if (!is.null(theme) && theme == "jama") {
p <- p + scale_color_manual(name = ystrataname, values = c("#00AFBB", "#E7B800", "#FC4E07"))
col.pal <- c("#00AFBB", "#E7B800", "#FC4E07")
col.pal <- rep(col.pal, ceiling(length(ystratalabs)/3))
} else if (all(linecols %in% brewer.palette)) {
col.pal <- NULL
} else {
col.pal <- linecols
col.pal <- rep(col.pal, ceiling(length(ystratalabs)/length(linecols)))
}

if (is.null(col.pal)) {
p <- p + scale_colour_brewer(name = ystrataname, palette = linecols)
} else {
p <- p + scale_color_manual(name = ystrataname, values = col.pal)
}


# Add censoring marks to the line:
if (marks == TRUE) {
p <- p + geom_point(data = subset(df, n.censor >= 1), aes(x = time, y = surv), shape = shape, colour = "black")
}

# Add 95% CI to plot
if (ci == TRUE) {
if (linecols2 == "black") {
if (all(linecols2 == "black")) {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper), alpha = 0.25, colour = NA)
} else {
} else if (is.null(col.pal)) {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper, fill = strata), alpha = 0.25, colour = NA) + scale_fill_brewer(name = ystrataname, palette = linecols)
} else {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper, fill = strata), alpha = 0.25, colour = NA) + scale_fill_manual(name = ystrataname, values = col.pal)
}
}

Expand Down
32 changes: 24 additions & 8 deletions R/svyjskm.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @param legend logical. should a legend be added to the plot? Default: TRUE
#' @param ci logical. Should confidence intervals be plotted. Default = NULL
#' @param legendposition numeric. x, y position of the legend if plotted. Default: c(0.85, 0.8)
#' @param linecols Character. Colour brewer pallettes too colour lines. Default: 'Set1', "black" for black with dashed line.
#' @param linecols Character or Character vector. Colour brewer pallettes too colour lines. Default ="Set1", "black" for black with dashed line, character vector for the customization of line colors.
#' @param dashed logical. Should a variety of linetypes be used to identify lines. Default: FALSE
#' @param cumhaz Show cumulaive incidence function, Default: F
#' @param design Data design for reactive design data , Default: NULL
Expand Down Expand Up @@ -253,7 +253,7 @@ svyjskm <- function(sfit,
# specifying axis parameteres etc #
###################################

if (dashed == TRUE | linecols == "black") {
if (dashed == TRUE | all(linecols == "black")) {
linetype <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash", "1F", "F1", "4C88C488", "12345678")
} else {
linetype <- c("solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid", "solid")
Expand All @@ -269,7 +269,7 @@ svyjskm <- function(sfit,
p <- ggplot2::ggplot(df, aes(x = time, y = surv, colour = strata, linetype = strata)) +
ggtitle(main)
linecols2 <- linecols
if (linecols == "black") {
if (all(linecols == "black")) {
linecols <- "Set1"
p <- ggplot2::ggplot(df, aes(x = time, y = surv, linetype = strata)) +
ggtitle(main)
Expand Down Expand Up @@ -317,18 +317,34 @@ svyjskm <- function(sfit,
scale_linetype_manual(name = ystrataname, values = linetype)
}

brewer.palette <- c("BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYlBu", "RdYlGn", "Spectral", "Accent", "Dark2", "Paired", "Pastel1", "Pastel2",
"Set1", "Set2", "Set3", "Blues", "BuGn", "BuPu", "GnBu", "Greens", "Greys", "Oranges", "OrRd", "PuBu", "PuBuGn", "PuRd", "Purples",
"RdPu", "Reds", "YlGn", "YlGnBu", "YlOrBr", "YlOrRd")

if (!is.null(theme) && theme == "jama") {
p <- p + scale_color_manual(name = ystrataname, values = c("#00AFBB", "#E7B800", "#FC4E07"))
col.pal <- c("#00AFBB", "#E7B800", "#FC4E07")
col.pal <- rep(col.pal, ceiling(length(ystratalabs)/3))
} else if (all(linecols %in% brewer.palette)) {
col.pal <- NULL
} else {
col.pal <- linecols
col.pal <- rep(col.pal, ceiling(length(ystratalabs)/length(linecols)))
}

if (is.null(col.pal)) {
p <- p + scale_colour_brewer(name = ystrataname, palette = linecols)
} else {
p <- p + scale_color_manual(name = ystrataname, values = col.pal)
}

# Add 95% CI to plot
if (ci) {
if (linecols2 == "black") {
if (ci == TRUE) {
if (all(linecols2 == "black")) {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper), alpha = 0.25, colour = NA)
} else {
} else if (is.null(col.pal)) {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper, fill = strata), alpha = 0.25, colour = NA) + scale_fill_brewer(name = ystrataname, palette = linecols)
} else {
p <- p + geom_ribbon(data = df, aes(ymin = lower, ymax = upper, fill = strata), alpha = 0.25, colour = NA) + scale_fill_manual(name = ystrataname, values = col.pal)
}
}

Expand Down
Loading