How to add abline, without so much programming? - r

Do you know if there is a way to reduce the programming lines.
abline(v = c(1990,1991,1992,1993,1994, 1995,1996,
1997,1998, 1999, 2000, 2001, 2002, 2003,
2004,2005,2006,2007,2008,2009,2010,
2011,2012,2013,2014,2015,2016,2017),
col = c("red","red","red","red","red","red","red",
"red","red","red","red","red","red","red",
"red","red","red","red","red","red","red",
"red","red","red","red","red","red","red"),
lty = c(2,2,2,2,2,2,2,
2,2,2,2,2,2,2,
2,2,2,2,2,2,2,
2,2,2,2,2,2,2),
lwd = c(1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1),
h = c(200,400,600,800,1000))

Like this?
abline(v = seq(1990, 2017, 1),
col = rep("red", 28),
lty = rep(2, 28),
lwd = rep(1, 28),
h = seq(200, 1000, 200))

Related

Survival Analysis in R -- why it is always a straight line

I am encountering a problem while trying to generate a survival curve. I don't know and understand why the curve which represents the survival probability of each year is sometimes a straight line. Ideally, it should be an up-and-down line, eventually lying around 55%.
I have included codes below, and please kindly refer to them. I have also attached a sample wrong plot to understand my description. Hopefully, it can help you understand what I am describing.
Any suggestions will be very appreciated. :)
Plot:
Codes:
library(survival)
library(survminer)
library(dplyr)
library(ggplot2)
library(readxl)
library(tidyverse)
data_all <- data.frame(Years_Diff_Surg_Death = c(8.919917864,
8.895277207, 8.881587953, 8.821355236, 8.728268309, 8.709103354), Survival = c(1L, 0L, 1L, 1L, 1L, 1L))
data_all <- data.frame(Years_Diff_Surg_Death = c(8.919917864,
8.895277207, 8.881587953, 8.821355236, 8.728268309, 8.709103354), Survival = c(1L, 0L, 1L, 1L, 1L, 1L))
data_2013 <- data.frame(Years_Diff_Surg_Death = c("36.99383984", "2.584531143", "36.91991786", "36.89527721", "36.88158795", "36.82135524"), YEARS_OF_SURGERY = c("2013","2013","2013","2013","2013","2013"), Survival = c("1","0", "1", "1", "1", "1"))
data_2014 <- data.frame(Years_Diff_Surg_Death = c(0.542094456, 5.196440794, 35.95619439, 35.91786448, 35.86584531, 35.8275154), YEARS_OF_SURGERY = c(2014, 2014, 2015, 2014, 2014, 2014, 2016), Survival = c(0, 0, 1, 1, 1, 1))
data_2015 <- data.frame(Years_Diff_Surg_Death = c(34.4476386, 34.25598905,0.621492129, 34.38740589, 34.33264887, 1.081451061), YEARS_OF_SURGERY = c(2015, 2015, 2015, 2015, 2015, 2015), Survival = c(1, 1, 0, 1, 1, 0))
data_2016 <- data.frame(Years_Diff_Surg_Death = c(2.902121834, 0.950034223, 33.9301848, 33.91101985, 33.87268994, 33.85352498), YEARS_OF_SURGERY = c(2016,2016,2016, 2016, 2016, 2016), Survival = c(0, 0, 1, 1, 1, 1))
data_2017 <- data.frame(Years_Diff_Surg_Death = c(32.99110198, 3.348391513, 32.95277207,32.91170431, 32.87611225, 0.791238877), YEARS_OF_SURGERY = c(2017, 2017, 2017, 2017, 2017, 2017), Survival = c(1, 0, 1, 1, 1, 0))
fit_all <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_all)
fit_2013 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ YEARS_OF_SURGERY, data = data_2013)
fit_2014 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ YEARS_OF_SURGERY, data = data_2014)
fit_2015 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ YEARS_OF_SURGERY, data = data_2015)
fit_2016 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ YEARS_OF_SURGERY, data = data_2016)
fit_2017 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ YEARS_OF_SURVERY, data = data_2017)
fit_comb <- list(s_2013 = fit_2013,
s_2014 = fit_2014,
s_2015 = fit_2015,
s_2016 = fit_2016,
s_2017 = fit_2017,
s_all= fit_all)
ggsurvplot(fit_all, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = "Years of Death After Surgery via Survival",
xlab = ("Years"),
legend = "none")
ggsurvplot(fit_2013, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none",
risk.table = F)
ggsurvplot(fit_2014, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none",
risk.table = F)
ggsurvplot(fit_2015, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none",
risk.table = F)
ggsurvplot(fit_2016, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none",
risk.table = F)
ggsurvplot(fit_2017, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none",
risk.table = F)
ggsurvplot_combine(fit_comb,
data_ECV,
xlab = ("Years"),
xlim = c(0,5),
break.x.by = 1)

Format RadarChart in R (title, subtitle and labels)

Here's the data:
df <- data.frame(Jan = 2230, Feb = 2420, Mar = 2833, Apr = 2192, May = 2332, Jun = 2577, Jul = 4363, Aug = 3747, Sep = 3176, Oct = 3988, Nov = 2144, Dec = 1896)
df <- rbind(4363, 0, df)
And here's the code to build the radar chart:
radarchart(df,
pcol = '#B62682', pfcol = alpha("#E1f56E", 0.7), plwd = 2,
axistype = 1, cglcol = "lightgrey", cglty = 1,
axislabcol = "black", cglwd = 1, caxislabels=seq(0, round(max(df[,1]), 0), round(max(df[,1])/8, 0)),
calcex = 1, palcex = 1, vlcex = 1, seg = 7,
title = "Title")
Ideally, I want to change the title's font size, add a subtitle and change the order of the labels (month labels should follow a clockwise direction). If this can't be done using radarchart(), I'm open to use other libraries. With ggplot() it would be super simple using labs() to add a title and subtitle and plot.title, plot.subtitle, etc to format.
Thanks in advance!
Complete answer:
radarchart(df,
pcol = '#B62682', pfcol = alpha("#E1f56E", 0.7), plwd = 2,
axistype = 1, cglcol = "lightgrey", cglty = 1, axislabcol = "black", cglwd = 1,
caxislabels=seq(0, round(max(df[,1]), 0), round(max(df[,1])/8, 0)),
calcex = 1, palcex = 1, vlcex = 1, seg = 7)
mtext(side = 3, line = 2.5, at = 0, cex = 1.75, "Title", font = 2)
mtext(side = 3, line = 1, at = 0, cex = 1.25, "Subtitle", col = '#666664')
line is for the height,
cex works as the size of the text,
at is the position (left - negative or right - positive, 0 center)
The order actually are very simple.
You can just change the order in the data
df <- data.frame(Jan = 2230, Feb = 2420, Mar = 2833, Apr = 2192, May = 2332, Jun = 2577, Jul = 4363, Aug = 3747, Sep = 3176, Oct = 3988, Nov = 2144, Dec = 1896)
df = df[, c(12,11,10,9,8,7,6,5,4,3,2,1)] # change the order
df <- rbind(4363, 0, df)
Insted of this title = "Title" you can use this for the title font size
title=("title", vlcex=0.5)

Count all values in a column based on string in another column in R for a Venn diagram

I have a file that I converted to a dataframe that looks as follows:
D <- data.frame(
V1 =c("B", "A_B", "A_B_C", "C_D", "A_C", "C_B_D", "C", "C_A_B_D", "B_C", "C_A_D", "A_D", "D", "A", "B_D", "A_B_D"),
V2 = c(15057, 5, 9, 1090, 4, 1250, 3943, 11, 2517, 5, 5, 2280, 5, 1735, 4))
I need to convert this dataframe into a list of numbers that I can use to create a 4-way venn plot. In this example the values are the correct values if added correctly. I did this manually but since I need to create several similar plots I would like to find a way to do this more efficiently.
library("VennDiagram")
venn.plot <- draw.quad.venn(
area1 = 48,
area2 = 20588,
area3 = 8829,
area4 = 6380,
n12 = 29,
n13 = 29,
n14 = 25,
n23 = 3787,
n24 = 3000,
n34 = 2356,
n123 = 20,
n124 = 15,
n134 = 16,
n234 = 1261,
n1234 = 11,
category = c("A", "B", "C", "D"),
fill = c("orange", "red", "green", "blue"),
lty = "dashed",
cex = 2,
cat.cex = 2,
cat.col = c("orange", "red", "green", "blue")
);
In this case I would need to count up all values from D$V2 that has an "A" in column V1 and so on. Then I would need to order appropriately for the venn plot function.
Here's what I would do
# setup
myset = LETTERS[1:4]
# create dummies
D[,myset] <- lapply(myset, grepl, D$V1)
# construct counts
myn <- length(myset)
mynums <- unlist(sapply(seq(myn), function(n)
apply(if (n==myn) matrix(seq(myn)) else combn(myn,n), 2, function(x)
with(D, sum( V2[Reduce("&", mget(myset[x]))] ))
)))
# pass counts to plotter
do.call(draw.quad.venn, c(as.list(unname(mynums)), list(
category = myset,
fill = c("orange", "red", "green", "blue"),
lty = "dashed",
cex = 2,
cat.cex = 2,
cat.col = c("orange", "red", "green", "blue")
)))

How to assign name to every circle in a Venn diagram using R (Venndiagram package)

I would assign a name for every circle in a Venn diagram. I have tried to change options in category but seems this is the only set I can use. I attach my code, please where is the wrong part?
goterm3 = c(1,2,3,4,5,6)
goterm2 =c(2,2,3,4,3,5)
goterm1=c(4,5,3,2,4,3,2,4)
int12 = intersect(goterm1, goterm2)
int13 = intersect(goterm1, goterm3)
int23 = intersect(goterm2, goterm3)
intall = intersect(int12, goterm3)
require(VennDiagram)
venn.plot = draw.triple.venn(length(goterm1), length(goterm2), length(goterm3),
length(int12), length(int23), length(int13),length(intall),
category = rep("ORG1, ORG2,Org",3) ,rotation = 1, reverse = FALSE, euler.d = FALSE,
scaled = FALSE, lwd = rep(2, 3), lty = rep("solid", 3),
col = rep("black", 3), fill = c("blue", "red", "green"),
alpha = rep(0.5, 3),
label.col = rep("black", 7), cex = rep(1, 7), fontface = rep("plain", 7),
fontfamily = rep("serif", 7), cat.pos = c(0, 0, 180),
cat.dist = c(0.05, 0.05, 0.025), cat.col = rep("black", 3),
cat.cex = rep(1, 3), cat.fontface = rep("plain", 3),
cat.fontfamily = rep("serif", 3),
cat.just = list(c(0.5, 1), c(0.5, 1), c(0.5, 0)), cat.default.pos = "outer",
cat.prompts = FALSE, rotation.degree = 0, rotation.centre = c(0.5, 0.5),
ind = TRUE, sep.dist = 0.05, offset = 0)
This is what I get and it does have the same labels as your categories (after I unmangled the string values for the categories:
category = c("ORG1", "ORG2","Org") # no rep needed and proper quotes

Combine lattice xyplot and histogram

Could someone help me please to upgrade my plot?
a) In the plot, there should be print only one y-scale per row.
b) To print a more comfortable legend, that means
1) change the order of symbols and description,
2) print line in the same x-position like superpose.symbols,
3) and print symbols for the histogram.
d1 <- data.frame(x=c(NA, 13:20, NA), y = 25, z = c(rep('march', 5),
rep("april", 5)), color = c(c(rep(c("red", "green"), 2), "red"),
c(rep(c("blue", "yellow"), 2), "blue")), stringsAsFactors = FALSE)
d2 <- data.frame(x=c(NA, 20:27, NA), y = 23, z = c(rep('may', 5),
rep("june", 5)), color = c(c(rep(c("blue", "red"), 2), "red"),
c(rep(c("blue", "yellow"), 2), "blue")), stringsAsFactors = FALSE)
d1<-rbind(d1,d2)
sup.sym <- trellis.par.get("superpose.symbol")
sup.sym$alpha<-c(1, 0, 0, 0, 0, 0, 0)
sup.sym$col<-c(1,2,3,4,5,6,7)
sup.lin <- trellis.par.get("superpose.line")
sup.lin$col<-c(1,2,7,5,5,6,7)
sup.lin$alpha<-c(0, 1, 1, 1, 0, 0, 0)
settings<-list(superpose.symbol = sup.sym,superpose.line = sup.lin)
xyplot(y ~ x | factor(z), data = d1
,ylim = list( c(22, 26),c(22, 26), c(0, 1),c(0, 1) )
,layout=c(2,2)
,scales = list(y = list( relation = "free" ))
,par.settings = settings
,auto.key = list(text = c("A","B","C", "D")
,space = "right"
,lines = TRUE
)
,panel = function(x, y, subscripts) {
if(panel.number()>2){
panel.histogram(x,breaks=3)
}else{
panel.xyplot(x = x, y = y,
subscripts=subscripts,
col = d1[subscripts, "color"])
}
})

Resources