let's say I have the following code:
library(boot)
samplemean <- function(x, d) {
return(mean(x[d]))
}
results_qsec <- boot(data=mtcars$qsec, statistic = samplemean, R=1000)
results_wt <- boot(data=mtcars$wt, statistic = samplemean, R=1000)
plot(results_qsec)
plot(results_wt)
for plot(results_wt) I get the following:
can I edit what is plotted? for instance, I'd like to get rid of the graph on the right, change the title from 'Histogram of t' to 'Histogram of bananas', and have the histograms for results_qsec and results_wt on the same graph.
Can this be done? I looked at the boot doc but couldn't find anything helpful.
Thanks
Tony
How about this? You could, of course, change the bin width, etc., to make it look more like the original.
wt_t <- plot(results_wt)$t
sec_t <- plot(results_qsec)$t
par(mfrow = c(1, 2))
hist(wt_t, main = "banana")
hist(sec_t, main = "apple")
thanks, looks good.
I found another way, using ggplot:
library(ggplot2)
boot_qsec <- as.data.frame(results_qsec$t)
boot_wt <- as.data.frame(results_wt$t)
ggplot() + geom_histogram(data=boot_qsec,aes(V1)) +
geom_histogram(data=boot_wt ,aes(V1))
Related
I am trying to plot a specific R-square metric, the R2 relative to the 1:1 line. Here are some code to generate data and a plot. (I realize I am only calculating standard r-squared here, but thats fine for the purposes of working out the code).
#generate data.
set.seed(1234)
x <- rnorm(100)
y <- x*0.7 + rnorm(100)
mod <- lm(y~x)
#develop rsq label.
rsq <- round(summary(mod)$r.squared, 2)
rsq.1.lab <- bquote(R^2 [1:1] == .(rsq))
#drop plot and rsq label.
plot(y ~ x)
mtext(rsq.1.lab, side = 3, line = -2, adj = 0.05)
The plot looks like this:
This is pretty close, but the subscript is actually relative to the superscript, rather than being relative to the letter R. How can I change this? Looking for solutions that use base R, ideally keeping bquote().
Use {/} grouping:
rsq.1.lab <- bquote({R^2} [1:1] == .(rsq))
or
rsq.1.lab <- bquote({R [1:1]}^2 == .(rsq))
or even a somewhat ridiculous
rsq.1.lab <- bquote(R * atop(2, "1:1") == .(rsq))
though we can reduce the font size a little using
rsq.1.lab <- bquote(R * scriptstyle(atop(2, "1:1")) == .(rsq))
Much of this is suggested/documented in ?plotmath.
I want to graph 0.2*ln(x/40)-0.02(x-40)=-0.04ln(y/100)+0.004(y-100) in RStudio. I installed 'manipulate' package and used plotFun but it didnt work. I tried using plot.function but I couldnt find a solution. please help
Not sure what you've tried since you didn't provide any code. Perhaps this helps: Solve your equation for z (or set it = 0), then give plotFun your new expression:
library(manipulate)
library(mosaic)
plotFun(0.2*log(x/40)- 0.02*(x-40) + 0.04*log(y/100) - 0.004*(y-100) ~
y+x, surface=TRUE, ylim=c(0,5), xlim=c(0,5))
If you just want for z=0, you can use contour:
xyFun <- function(x,y){
0.2*log(x/40)- 0.02*(x-40)+0.04*log(y/100) - 0.004*(y-100)
}
x <- y <- seq(0,5, by=0.1)
z <- outer(x,y,xyFun)
contour(x,y,z, levels=0)
or just
contour(x,y,z)
I'm plotting some Q-Q plots using the qqplot function. It's very convenient to use, except that I want to color the data points based on their IDs. For example:
library(qualityTools)
n=(rnorm(n=500, m=1, sd=1) )
id=c(rep(1,250),rep(2,250))
myData=data.frame(x=n,y=id)
qqPlot(myData$x, "normal",confbounds = FALSE)
So the plot looks like:
I need to color the dots based on their "id" values, for example blue for the ones with id=1, and red for the ones with id=2. I would greatly appreciate your help.
You can try setting col = myData$y. I'm not sure how the qqPlot function works from that package, but if you're not stuck with using that function, you can do this in base R.
Using base R functions, it would look something like this:
# The example data, as generated in the question
n <- rnorm(n=500, m=1, sd=1)
id <- c(rep(1,250), rep(2,250))
myData <- data.frame(x=n,y=id)
# The plot
qqnorm(myData$x, col = myData$y)
qqline(myData$x, lty = 2)
Not sure how helpful the colors will be due to the overplotting in this particular example.
Not used qqPlot before, but it you want to use it, there is a way to achieve what you want. It looks like the function invisibly passes back the data used in the plot. That means we can do something like this:
# Use qqPlot - it generates a graph, but ignore that for now
plotData <- qqPlot(myData$x, "normal",confbounds = FALSE, col = sample(colors(), nrow(myData)))
# Given that you have the data generated, you can create your own plot instead ...
with(plotData, {
plot(x, y, col = ifelse(id == 1, "red", "blue"))
abline(int, slope)
})
Hope that helps.
In the following reproducible example I try to create a function for a ggplot distribution plot and saving it as an R object, with the intention of displaying two plots in a grid.
ggplothist<- function(dat,var1)
{
if (is.character(var1)) {
var1 <- which(names(dat) == var1)
}
distribution <- ggplot(data=dat, aes(dat[,var1]))
distribution <- distribution + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
output<-list(distribution,var1,dat)
return(output)
}
Call to function:
set.seed(100)
df <- data.frame(x = rnorm(100, mean=10),y =rep(1,100))
output1 <- ggplothist(dat=df,var1='x')
output1[1]
All fine untill now.
Then i want to make a second plot, (of note mean=100 instead of previous 10)
df2 <- data.frame(x = rep(1,1000),y = rnorm(1000, mean=100))
output2 <- ggplothist(dat=df2,var1='y')
output2[1]
Then i try to replot first distribution with mean 10.
output1[1]
I get the same distibution as before?
If however i use the information contained inside the function, return it back and reset it as a global variable it works.
var1=as.numeric(output1[2]);dat=as.data.frame(output1[3]);p1 <- output1[1]
p1
If anyone can explain why this happens I would like to know. It seems that in order to to draw the intended distribution I have to reset the data.frame and variable to what was used to draw the plot. Is there a way to save the plot as an object without having to this. luckly I can replot the first distribution.
but i can't plot them both at the same time
var1=as.numeric(output2[2]);dat=as.data.frame(output2[3]);p2 <- output2[1]
grid.arrange(p1,p2)
ERROR: Error in gList(list(list(data = list(x = c(9.66707664902549, 11.3631137069225, :
only 'grobs' allowed in "gList"
In this" Grid of multiple ggplot2 plots which have been made in a for loop " answer is suggested to use a list for containing the plots
ggplothist<- function(dat,var1)
{
if (is.character(var1)) {
var1 <- which(names(dat) == var1)
}
distribution <- ggplot(data=dat, aes(dat[,var1]))
distribution <- distribution + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
plot(distribution)
pltlist <- list()
pltlist[["plot"]] <- distribution
output<-list(pltlist,var1,dat)
return(output)
}
output1 <- ggplothist(dat=df,var1='x')
p1<-output1[1]
output2 <- ggplothist(dat=df2,var1='y')
p2<-output2[1]
output1[1]
Will produce the distribution with mean=100 again instead of mean=10
and:
grid.arrange(p1,p2)
will produce the same Error
Error in gList(list(list(plot = list(data = list(x = c(9.66707664902549, :
only 'grobs' allowed in "gList"
As a last attempt i try to use recordPlot() to record everything about the plot into an object. The following is now inside the function.
ggplothist<- function(dat,var1)
{
if (is.character(var1)) {
var1 <- which(names(dat) == var1)
}
distribution <- ggplot(data=dat, aes(dat[,var1]))
distribution <- distribution + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
plot(distribution)
distribution<-recordPlot()
output<-list(distribution,var1,dat)
return(output)
}
This function will produce the same errors as before, dependent on resetting the dat, and var1 variables to what is needed for drawing the distribution. and similarly can't be put inside a grid.
I've tried similar things like arrangeGrob() in this question "R saving multiple ggplot2 plots as R-object in list and re-displaying in grid " but with no luck.
I would really like a solution that creates an R object containing the plot, that can be redrawn by itself and can be used inside a grid without having to reset the variables used to draw the plot each time it is done. I would also like to understand wht this is happening as I don't consider it intuitive at all.
The only solution I can think of is to draw the plot as a png file, saved somewhere and then have the function return the path such that i can be reused - is that what other people are doing?.
Thanks for reading, and sorry for the long question.
Found a solution
How can I reference the local environment within a function, in R?
by inserting
localenv <- environment()
And referencing that in the ggplot
distribution <- ggplot(data=dat, aes(dat[,var1]),environment = localenv)
made it all work! even with grid arrange!
At present, I generate a figure using the following script
dat <- matrix(runif(1000*99),99,1000)
dat <- rbind(rep(0.1,1000),dat)
out <- cmdscale(dist(dat),k = 2)
plot(out)
points(out[1,1],out[1,2],col = "red")
Based on the above figure, I want to connect that red point with other points, how to do that?
If you want to connect all the points to that red point, you could try...
segments(out[1,1],out[1,2],out[,1],out[,2])
Adjusting the order of the printing and the graphical characteristics could make it a little easier to look at too:
dat <- matrix(runif(1000*99),99,1000)
dat <- rbind(rep(0.1,1000),dat)
out <- cmdscale(dist(dat),k = 2)
plot(out,type="n")
segments(out[1,1],out[1,2],out[,1],out[,2],col="#cccccc")
points(out,col="black",pch=20)
points(out[1,1],out[1,2],col = "red",pch=20)