Non-numeric argument to binary operator in R: Survival Analysis - r

I'm not sure why I get this error. I get the graph after, sure, but I don't know what is causing the error.
plot(survfit(Surv(time,DEATH_EVENT) ~ hypertension, data=HF), main = "Hypertension Survival Distributions", xlab = "Length of Survival",ylab="Probability of Survival",col=c("blue","red")) +
legend("topright", legend=c("Absent", "Present"),fill=c("blue","red"),bty="n")
Error in plot(survfit(Surv(time, DEATH_EVENT) ~ hypertension, data = HF), :
non-numeric argument to binary operator
This, however, works wonders:
ggsurvplot(survfit(Surv(time,DEATH_EVENT) ~ hypertension, data=HF),
data = HF,
censor.shape="|",
conf.int = FALSE,
ggtheme = theme_bw())

If you alter some of your parameters, it should work as expected:
plot(survfit(Surv(time, DEATH_EVENT) ~ hypertension, data = HF), main = "Hypertension Survival Distributions", xlab = "Length of Survival", ylab = "Probability of Survival", col = c("blue","red"))
legend(x = 1, y = 1, legend = c("Absent", "Present"), col = c("blue","red"), lty = 1)
NB. change legend(x = 1 to "whatever the max x axis value is", e.g. legend(x = 1000 to place the legend in the top right.

Related

How to format multiple ggsurvplots when using arrange_ggsurvplots?

I'm trying to create a figure in R which includes multiple kaplan-meier curves and label each plot (e.g. A, B, C). I've been able to make a figure using arrange_ggsurvplots, however, the output is very cramped appearing. How can I format the plots (e.g. change plot dimensions, font size) so that the plots in the output are appropriately sized and readable?
Appreciate your time and help.
segst_g_survfit <- survfit(Surv(time/30.4375, statuslife) ~ segst_g, data = good_data)
ehdz_survfit <- survfit(Surv(time/30.4375, statuslife) ~ Extrahepaticdz, data = good_data)
prioranyldt_survfit <- survfit(Surv(time/30.4375, statuslife) ~ prioranyldt, data = good_data)
hepaticresect <- survfit(Surv(time/30.4375, statuslife) ~ hepaticresection, data = good_data)
tsize_g_survfit <- survfit(Surv(time/30.4375, statuslife) ~ tsize_g, data = good_data)
segstx_g_survfit <- survfit(Surv(time/30.4375, statuslife) ~ segstx_g, data = good_data)
splots <- list()
splots[[1]] <- ggsurvplot(
fit = hepaticresect,
legend = c(0.75,0.8),
legend.labs=c("No Resection","Prior Resection"),
title ="OS vs Prior Hepatic Resection",
pval=TRUE,
pval.coord = c(4,0),
xlab = "Months",
ylab = "Survival Probability", conf.int=FALSE) + labs(tag='A')
splots[[2]] <- ggsurvplot(
fit = prioranyldt_survfit,
legend = c(0.75,0.8),
legend.labs=c("Prior Liver Directed Therapy","No Prior Liver Directed Therapy"),
title ="OS vs Prior Liver Directed Therapy",
pval=TRUE,
pval.coord = c(4,0),
xlab = "Months",
ylab = "Survival probability", conf.int=FALSE) + labs(tag='B')
splots[[3]] <- ggsurvplot(
fit = segst_g_survfit,
legend = c(0.75,0.8),
legend.labs=c("Greater than 2 segments","Less than or equal to 2 segments"),
title ="OS vs Number Segments with Tumor",
pval=TRUE,
pval.coord = c(4,0),
xlab = "Months",
ylab = "Survival Probability", conf.int=FALSE) + labs(tag='C')
splots[[4]] <- ggsurvplot(
fit = segstx_g_survfit,
legend = c(0.75,0.8),
legend.labs=c("Greater than 3 segments","Less than or equal to 3 segments"),
title ="OS vs Hepatic Segments Treated",
pval=TRUE,
pval.coord = c(4,0),
xlab = "Months",
ylab = "Survival Probability", conf.int=FALSE) + labs(tag='D')
splots[[5]] <- ggsurvplot(
fit = tsize_g_survfit,
pval=TRUE,
pval.coord = c(4,0),
title ="OS vs Largest Tumor Size",
legend = c(0.75,0.8),
legend.labs=c("Greater than 4 cm","Less than or equal to 4 cm"),
xlab = "Months",
ylab = "Survival probability", conf.int=FALSE) + labs(tag='E')
splots[[6]] <- ggsurvplot(
fit = ehdz_survfit,
legend = c(0.75,0.8),
legend.labs=c("No Extrahepatic Disease","Extrahepatic Disease"),
title ="OS vs Extrahepatic Disease",
pval=TRUE,
pval.coord = c(4,0),
xlab = "Months",
ylab = "Survival Probability", conf.int=FALSE) + labs(tag='F')
arrange_ggsurvplots(splots,print=TRUE,ncol=2,nrow=3)
Cramped output:
Try piping the output to ggsave, and change the size to the proportions you like
myplot %>% ggsave(device="png", filename="Figure.png", width = 15, height = 5, units = "in")
I think what you are experiencing is simply the display in rstudio being a little cramped.

Problem with my code- Univariate regression plot not showing lines

this will sound very basic, but I cannot find the solution to this problem with my code. I did a univariate regression (regr1) between the 2 variables immigrate_policy and lrgen. In plotting the commands for the lines do not show.
One problem could be the sequence maybe? Because the range for lrgen should actually be between 1 and 9, but I had to put manually 1:8 because every other sequence I put gives me an error. With this sequence, however, the lines in the plot are weird, and definitely not right
Following is my code:
regr1 <- lm(formula = ITA$immigrate_policy ~ ITA$lrgen, data = ITA)
summary(regr1)
install.packages("stargazer") library(stargazer) help(stargazer)
stargazer(regr1, type ="html",out="project.html")
stargazer(regr1, type="text",out="project/regression.html")
plot(ITA$lrgen, ITA$immigrate_policy,
xlab = "Political Stance of the party", ylab = "Position towards Immigration policies") abline(regr1, col = "red", lwd = 2)
range(ITA$lrgen)
ci <- data.frame(lrgen = seq(1:8))
sim <- predict(regr1, newdata = ci, interval = "confidence", level =
0.99)
lines(c(1:8),sim[,2], lt = "dashed", lwd = 1, col = "yellow")
lines(c(1:8),sim[,3], lt = "dashed", lwd = 1, col = "yellow")

Do we need to call dev.off() after creating a pdf file?

When I call dev.off() my pdf gets created but I get the following message "null device 1".
I don't get any warning when I remove dev.off and my pdf gets created so why do I need to call dev.off for?
plot(x # independent variable (population_density)
, y # dependent variable (case_fatality_rate)
, main = "ScatterPlot - Case Fatality Rate vs Population Density Per Square Mile" # chart
title
, xlab = "Population Density Per Square Mile" # x-axis label
, ylab = "Case Fatality Rate" # y-axis label
, pch = 19 # point shape (filled circle)
, frame = T # surround chart with a frame
, xlim = c(0, 1200), ylim = c(0, 3)
)
model <- lm(y ~ x, data = dataset) # compute the linear model
abline(model, col = "blue") # draw the model as a blue line
hist(y # depandant variable (case_fatality_rate)
, main = "Histogram - Case Fatality Rate Frequency" # chart title
, xlab = "Case Fatality Rate",
ylab = "Frequency",
col = "#f0ffff",
breaks = 15,
freq = FALSE,
prob = TRUE,
xlim = c(0.5,2.5),
ylim = c(0.0,2.0)
)
lines(density(y, adjust=1.2), col="blue", lwd=2)
grid(nx = NA, ny = NULL,
lty = 1, col = "gray", lwd = 1)
dev.off()

Y axis values missing in R scatter plot and graphs

I have been unable to get any graphs to show y-axis values in RStudio. My understanding is that the y-axis should set it self automatically. I've also attached an example of the data below. Do I need to be specifying the y-axis, or am I formatting my data incorrectly in some way?
homes = read.csv("~/Downloads/homePrice1.csv")
model = lm(homes$Price ~ homes$Size,
data = homes)
plot(homes$Price ~ homes$Size,
data = homes,
xlab = "Size",
ylab = "Price")
coeff=coefficients(model)
rSquare = summary(model)$r.squared
eq = paste0("y = ", round(coeff[2],5), "*x + ", round(coeff[1],2), "\nR^2 = ", round(rSquare*100,2), "%")
print(eq)
abline(model, lwd = 3, col = "darkorange")
text(x = 1500, y = 300000, label = eq)
I used dev.off() and that fixed the problem. I found the answer here.

Manually set fontsize of axis titles in native-R plot and lattice graphing functions

I am trying to prepare a graph for a poster presentation, but am getting very frustrated by how difficult things that should be simple are in plot. I want to plot a qq-plot of residuals from a mixed-effects model. All I want to do is change the font size of the axis title
. Here's a reproducible example.
library(lme4)
library(lattice)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
This all works fine. But when I try to increase the font size of the axis title
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
xlab = list("x-axis", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
I get
Error in qqmath.formula(x = ~x, ylab = "Standardized residuals", xlab = "Standard normal quantiles", :
formal argument "xlab" matched by multiple actual arguments
I gather from this post that this is due to competing arguments in the function call and some ellipsis in the original qqmath.formula object, but surely there has to be an easier way to set the fontsize of the axis titles than reprogramming the original function?!
The lattice system has functions trellis.par.get and trellis.par.set and this can be used to control the fontsize of the xlab and ylab components:
?trellis.par.get
names( trellis.par.get() )
trellis.par.set(par.ylab.text=list(cex=.5))
qqmath(fm1,
main = list("QQ-Plot", cex = 2), id=0.05,
cex=list(left=.55,bottom=.5),
scales = list(x = list(cex = 1), y = list(cex = 1)))
... reduces the size of the ylab. You can find a more complete list of the components and features that can be set from a chart onpage 127 in the "Lattice" book by Sarkar.

Resources