Sum all the values of a plot in R - r

I have this plot:
pts1 <- seq(-1,4, 0.005)
plot1 <- plot(pts1,dnorm(pts1),type="l", xlim=c(-4,4),xlab="",ylab="Densidad")
I want to sum all the values that the x-axis values take in the y-axis.

Do you mean CDF?
plot(pts1, pnorm(pts1), type = "l", xlim = c(-4, 4), xlab = "", ylab = "Densidad")
or
plot(pts1, cumsum(dnorm(pts1)) * 0.005, type = "l", xlim = c(-4, 4), xlab = "", ylab = "Densidad")
Update
> pnorm(-1:4,lower.tail = FALSE)
[1] 8.413447e-01 5.000000e-01 1.586553e-01 2.275013e-02 1.349898e-03
[6] 3.167124e-05

Related

How can I show non-inferiority with a plot using R

I compare two treatments A and B. The objective is to show that A is not inferior to B. The non inferiority margin delta =-2
After comparing Treatment A - Treatment B I have these results
Mean difference and 95% CI = -0.7 [-2.1, 0.8]
I would like to plot this either with a package or manually. I have no idea how to do it.
Welch Two Sample t-test
data: mydata$outcome[mydata$traitement == "Bras S"] and mydata$outcome[mydata$traitement == "B"]
t = 0.88938, df = 258.81, p-value = 0.3746
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.133224 0.805804
sample estimates:
mean of x mean of y
8.390977 9.054688
I want to create this kind of plot:
You could abstract the relevant data from the t.test results and then plot in base R using segments and points to plot the data and abline to draw in the relevant vertical lines. Since there were no reproducible data, I made some up but the process is generally the same.
#sample data
set.seed(123)
tres <- t.test(runif(10), runif(10))
# get values to plot from t test results
ci <- tres$conf.int
ests <- tres$estimate[1] - tres$estimate[2]
# plot
plot(x = ci, ylim = c(0,2), xlim = c(-4, 4), type = "n", # blank plot
bty = "n", xlab = "Treatment A - Treatment B", ylab = "",
axes = FALSE)
points(x = ests, y = 1, pch = 20) # dot for point estimate
segments(x0 = ci[1], x1 = ci[2], y0 = 1) #CI line
abline(v = 0, lty = 2) # vertical line, dashed
abline(v = 2, lty = 1, col = "darkblue") # vertical line, solid, blue
axis(1, col = "darkblue") # add in x axis, blue
EDIT:
If you wanted to more accurately recreate your figure with the x axis in descending order and using your statement "Mean difference and 95% CI = -0.7 [-2.1, 0.8]", you can do the following manipulations to the above approach:
diff <- -0.7
ci <- c(-2.1, 0.8)
# plot
plot(1, xlim = c(-4, 4), type = "n",
bty = "n", xlab = "Treatment A - Treatment B", ylab = "",
axes = FALSE)
points(x = -diff, y = 1, pch = 20)
segments(x0 = -ci[2], x1 = -ci[1], y0 = 1)
abline(v = 0, lty = 2)
abline(v = 2, lty = 1, col = "darkblue")
axis(1, at = seq(-4,4,1), labels = seq(4, -4, -1), col = "darkblue")

How can I change the name of my x value in r plot

I have the plot:
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]"))) +
lines(Combined$TIMESTAMP, # Draw second time series
Combined$GPP_WRF_mean,
type = "l",
col = 3) +
legend("topright", # Add legend to plot
c("OBS", "WRF"),
lty = 1,
col = 2:4)
which produces the timeseries graph with the name of x values equal to gen, feb, mar and I want to convert my x values in J,F,M,A,M,J...
I tried with:
axis(1, at=1:12, labels=month.name, cex.axis=0.5)
but it doesn't work - any help?
Keep the first letter of month.name or from month.abb:
month.1st.letter <- sub("(^[[:upper:]]).*$", "\\1", month.abb)
Then use this vector as the labels argument.
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)
Edit
Start by removing the plus signs from the code, base R graphics do not add up. Then, in the plot instruction include xaxt = "n". And plot the axis right after it.
plot(Combined$TIMESTAMP, # Draw first time series
Combined$GPP_NT_VUT_REF,
type = "l",
col = 2,
ylim = c(0, 15),
xaxt = "n", # here, no x axis
xlab = "",
ylab = expression(paste("GPP [gC m"^"-2 "," day "^"-1]")))
axis(1, at = 1:12, labels = month.1st.letter, cex.axis = 0.5)

How can I make the y axis on a r plot display numbers in decimal format with commas?

I am running the following R code:
plot(datereal, casesimm8lock7,
type = "l", lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
And I want the y-axis, which currently displays 50000 100000 150000 etc. to display 50,000 100,000 150,000 etc.
I am trying to use the scales package but haven't figured it out yet.
Start by making an empty plot with no y axis. Then plot the data.
Then plot the y axis with axis(2, ...). In order to have the labels formatted like the question asks for, use help("formatC").
# test data
datereal <- Sys.Date() - 10:0
casesimm8lock7 <- seq(0,250000, length.out = 11)
# the plot
plot(datereal, casesimm8lock7, type = "n", yaxt = "n")
lines(datereal, casesimm8lock7,
lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
axis(2, at = seq(0, 250000, by = 50e3),
labels = formatC(seq(0, 250000, by = 50e3),
format = "d", big.mark = ","))
With package scales, it could be with either label_comma, like below, or label_number. Thse functions return a labeller function, to be applied to the vector of axis marks.
labeller <- scales::label_comma()
plot(datereal, casesimm8lock7, type = "n", yaxt = "n")
lines(datereal, casesimm8lock7,
lwd = 1, col = "red",
main = "a Sensitivity to time delays: 8 month immunity",
xaxt = "n", xlab = "Month",
ylab = "Daily new cases", ylim = c(0,250000))
axis(2, at = seq(0, 250000, by = 50e3),
labels = labeller(seq(0, 250000, by = 50e3)))

Adjust distance between x-axis and text in R plot

In the plot below, you probably find the distance between x-aixs and the date (Jan-01-60 to Mar-31-16) is large.
Is there anyway I could make the distance smaller?
Here is my example code:
plot(dates, data, type = "l", lwd = 3, ylab = " ",
xlab = " ", col = "gray35", xaxt='n', ann = FALSE)
axis(side=1, at=dates_ten, labels=format(dates_ten, "%b-%d-%y"), las =
1, cex.axis=0.5, las = 1, font = 2, tcl = -0.2)
Many thanks.
You can use the padj argument to axis
# make a reproducible example
dates <- seq(as.Date("2016/1/1"), as.Date("2016/4/1"), "days")
dates_ten <- seq(as.Date("2016/1/1"), as.Date("2016/4/1"), "10 days")
set.seed(42)
data <- rnorm(seq_along(dates))
Vary padj as needed:
plot(dates, data, type = "l", lwd = 3, ylab = " ",
xlab = " ", col = "gray35", xaxt='n', ann = FALSE)
axis(side=1, at=dates_ten, labels=format(dates_ten, "%b-%d-%y"), las = 1,
cex.axis=0.5, las = 1, font = 2, tcl = -0.2, padj = -2)

How to plot absolute values and differences including confidence intervals

In the followup of the discussion on stackexchange I tried to implement the following plot
from
Cumming, G., & Finch, S. (2005). [Inference by Eye: Confidence Intervals and How to Read Pictures of Data][5]. American Psychologist, 60(2), 170–180. doi:10.1037/0003-066X.60.2.170
I share some people's dislike of double axis, but I think this is a fair use.
Below my partial attempt, the second axis is still missing. I am looking for more elegant alternatives, intelligent variations are welcome.
library(lattice)
library(latticeExtra)
d = data.frame(what=c("A","B","Difference"),
mean=c(75,105,30),
lower=c(50,80,-3),
upper = c(100,130,63))
# Convert Differences to left scale
d1 = d
d1[d1$what=="Difference",-1] = d1[d1$what=="Difference",-1]+d1[d1=="A","mean"]
segplot(what~lower+upper,centers=mean,data=d1,horizontal=FALSE,draw.bands=FALSE,
lwd=3,cex=3,ylim=c(0,NA),pch=c(16,16,17),
panel = function (x,y,z,...){
centers = list(...)$centers
panel.segplot(x,y,z,...)
panel.abline(h=centers[1:2],lty=3)
} )
## How to add the right scale, close to the last bar?
par(mar=c(3,5,3,5))
plot(NA, xlim=c(.5,3.5), ylim=c(0, max(d$upper[1:2])), bty="l", xaxt="n", xlab="",ylab="Mean")
points(d$mean[1:2], pch=19)
segments(1,d$mean[1],5,d$mean[1],lty=2)
segments(2,d$mean[2],5,d$mean[2],lty=2)
axis(1, 1:3, d$what)
segments(1:2,d$lower[1:2],1:2,d$upper[1:2])
axis(4, seq((d$mean[1]-30),(d$mean[1]+50),by=10), seq(-30,50,by=10), las=1)
points(3,d$mean[1]+d$mean[3],pch=17, cex=1.5)
segments(3,d$lower[3]+d$lower[2],3,d$lower[3]+d$upper[2], lwd=2)
mtext("Difference", side=4, at=d$mean[1], line=3)
As a starting point another base R solution with Hmisc:
library(Hmisc)
with(d1,
errbar(as.integer(what),mean,upper,lower,xlim=c(0,4),xaxt="n",xlab="",ylim=c(0,150))
)
points(3,d1[d1$what=="Difference","mean"],pch=15)
axis(1,at=1:3,labels=d1$what)
atics <- seq(floor(d[d$what=="Difference","lower"]/10)*10,ceiling(d[d$what=="Difference","upper"]/10)*10,by=10)
axis(4,at=atics+d1[d1=="A","mean"],labels=atics,pos=3.5)
I would also go with base graph, as it includes the possibility to actually have two y-axis, see the answer here:
Here is my soultion that uses only d:
xlim <- c(0.5, 3.5)
plot(1:2, d[d$what %in% LETTERS[1:2], "mean"], xlim = xlim, ylim = c(0, 140),
xlab = "", ylab = "", xaxt = "n", bty = "l", yaxs = "i")
lines(c(1,1), d[1, 3:4])
lines(c(2,2), d[2, 3:4])
par(new = TRUE)
plot(3, d[d$what == "Difference", "mean"], ylim = c(-80, 130), xlim = xlim,
yaxt = "n", xaxt = "n", xlab = "", ylab = "", bty = "n")
lines(c(3,3), d[3, 3:4])
Axis(x = c(-20, 60), at = c(-20, 0, 20, 40, 60), side = 4)
axis(1, at = c(1:3), labels = c("A", "B", "Difference"))
Which gives:
To make it clearer that the difference is something different, you can increase the distance from the other two points:
xlim <- c(0.5, 4)
plot(1:2, d[d$what %in% LETTERS[1:2], "mean"], xlim = xlim, ylim = c(0, 140),
xlab = "", ylab = "", xaxt = "n", bty = "l", yaxs = "i")
lines(c(1,1), d[1, 3:4])
lines(c(2,2), d[2, 3:4])
par(new = TRUE)
plot(3.5, d[d$what == "Difference", "mean"], ylim = c(-80, 130), xlim = xlim,
yaxt = "n", xaxt = "n", xlab = "", ylab = "", bty = "n")
lines(c(3.5,3.5), d[3, 3:4])
Axis(x = c(-20, 60), at = c(-20, 0, 20, 40, 60), side = 4)
axis(1, at = c(1,2,3.5), labels = c("A", "B", "Difference"))
I think you can do that also with base R, what about:
d = data.frame(what=c("A","B","Difference"),
mean=c(75,105,30),
lower=c(50,80,-3),
upper = c(100,130,63))
plot(-1,-1,xlim=c(1,3),ylim=c(0,140),xaxt="n")
lines(c(1,1),c(d[1,3],d[1,4]))
points(rep(1,3),d[1,2:4],pch=4)
lines(c(1.5,1.5),c(d[2,3],d[2,4]))
points(rep(1.5,3),d[2,2:4],pch=4)
lines(c(2,2),c(d[3,3],d[3,4]))
points(rep(2,3),d[3,2:4],pch=4)
lines(c(1.5,2.2),c(d[2,2],d[2,2]),lty="dotted")
axis(1, at=c(1,1.5,2), labels=c("A","B","Difference"))
axis(4,at=c(40,80,120),labels=c(-1,0,1),pos=2.2)
I simplified some things and didn't wrote it as function, but I think the idea is clear and could easily be extended to a function.

Resources