there
I am new on R. I want to plot a graph like this.
The curves are created by these equations :
(log(0.4)-(0.37273*log(x)-1.79389))/0.17941
(log(0.5)-(0.37273*log(x)-1.79389))/0.17941
(log(0.6)-(0.37273*log(x)-1.79389))/0.17941
etc. The equations are similar, the only difference is the first log(XXX). I already manually draw the graph by repeating plot() for each equation.
But I think there must be a way to just assign a simple variable like
x<-c(0.4,0.5,0.6,0.7)
and then plot all the curves automatically. I tried to use data frame to make a set of equations, but failed.
You can create a function-generating function and then loop over values of interest. For example
# takes a value, returns a function
logfn <- function(b) {
function(x) (log(b)-(0.37273*log(x)-1.79389))/0.17941
}
x <- c(0.4,0.5,0.6,0.7)
# empty plot
plot(0,0,type="n", ylim=c(-5,5), xlim=c(1,8), xlab="Lenght", ylab="Z-score")
# add plots for questions with `curve()`
for(v in x) {
curve(logfn(v)(x),add=T)
}
Related
I am looking for a way to find the number of generated plots from a function in R. Here is a dummy code for elaboration:
ploting = function(x)
{
for(i in 1:x){
print(plot(rnorm(10)))
}
}
How can I manage to obtain the number of generated plots from this function? (which is equal to x in this case assuming x is a positive integer).
P.s: I'm using ggplot2 for my actual function if that is of any importance.
You could plot to a PDF device:
pdf("E:/temp/test.pdf")
ploting(3)
dev.off()
The number of pages in the PDF is the number of plots and the plots are archived as an additional benefit.
You could also trace plot (or/and any functions used for plotting in your function):
trace(plot, quote(nplot <<- nplot + 1))
nplot <- 0
ploting(3)
nplot
#[1] 3
untrace(plot)
Preferably, you would just modify ploting to return or message the number of plots.
Im trying to solve this question with the dataframe stackloss:
Use the pairs() function to obtain all pairwise scatterplots among the
four variables.
However when i use the pairs function I get a graph with all the variables plotted together. How can i make sure that i only get the variables pairwise so only two variables will appear per graph window?
My code is:
pairs(stackloss,pch=21,bg=c("red","green","yellow","blue"))
Thank you
It was not quite clear how you want to obtain all plots. I put the plot() function in two loops and use the Sys.sleep() function to have a small break between every call of the command. If you use R-studio you can switch between the last shown plots.
for(ii in 1:(ncol(stackloss)-1) ){
begin <- ii + 1
for(i in begin:ncol(stackloss)){
plot(x=stackloss[,ii], y=stackloss[,i], xlab=colnames(stackloss)[ii], ylab=colnames(stackloss)[i])
Sys.sleep(1)
}
}
when using the "curve" function in R, how do you suppress/stop the plot from showing up? For example, this code always plots the curve
my_curve = curve(x)
Is there a parameter to do this or should I being using a different function? I just want the x y points as a dataframe from the curve.
curve() is from the graphics library and is unhandy for generating lists.
Just try using:
x = seq(from, to, length.out = n)
y = function(x)
If you stick to the curve function, the closest to a solution I know is adding dev.off() after the curve() statement!
Here's a way to take advantage of the part of curve that you want without generating a plot.
I made a copy of the curve function (just type curve in the console); called it by a new name (curve2); and commented out the four lines at the end starting with if (isTRUE(add)). When it's called and assigned, I had a list with two vectors—x and y. No plot.
I want to plot a curve that has a change point at x=5
Until now I am using the code
curve(exp(0.68+0.92*x), from=0,to=5, xlim=c(0,12), ylim=c(0,500))
curve(exp(0.68+0.92*x-0.7*(x-5)), from=5,to=12, add=T)
Is it possible to write it in one line (one curve command)? I was thinking
something like this
curve(exp(0.47+0.8*x-0.7*(x-5)*if(x<5,0,1)), from=0,to=12, xlim=c(0,12), ylim=c(0,500))
but it doesn't work for R
Using ifelse you can create one data series:
values = ifelse(x <= 5, exp(0.68+0.92*x), exp(0.68+0.92*x-0.7*(x-5))
and plot them:
curve(values)
and if you insist on a one-liner you can combine the ifelse and the call to curve:
curve(ifelse(x <= 5, exp(0.68+0.92*x), exp(0.68+0.92*x-0.7*(x-5)))
although separating the code into two lines makes it easier to read imo.
You could just write a function that plots both curves:
myfun <- function(...) {
plot(...)
lines(...)
}
You have to give the right arguments of course. The result is two curves in one plot
I am trying to do PCA with R.
My Data has 10,000 columns and 90 rows
I used the prcomp function to do PCA.
Trying to prepare a biplot with the prcomp results, I ran into the problem that the 10,000 plotted vectors cover my datapoints. Is there any option for the biplot to hide the vectors' representation?
OR
I can use plot to get the PCA results. But I am not sure how to label these points according to my datapoints, which are numbered 1 to 90.
Sample<-read.table(file.choose(),header=F,sep="\t")
Sample.scaled<-data.frame(apply(Sample_2XY,2,scale))
Sample_scaled.2<-data.frame(t(na.omit(t(Sample_2XY.scaled))))
pca.Sample<-prcomp(Sample_2XY.scaled.2,retx=TRUE)
pdf("Sample_plot.pdf")
plot(pca.Sample$x)
dev.off()
If you do a help(prcomp) or ?prcomp, the help file tells us all the things contained in the prcomp() object returned by the function. We just need to pick which things we want to plot and do it with some function that gives us more control than biplot().
A more general trick for cases when the help file doesn't clarify things is to do a str() on the prcomp object (in your case pca.Sample) to see all its parts and find what we want ( str() compactly displays the internal structure of an R object. )
Here is an example with some of R's sample data:
# do a pca of arrests in different states
p<-prcomp(USArrests, scale = TRUE)
str(p) gives me something ugly and too long to include, but I can see that p$x has the states as rownames and their locations on the principal components as columns. Armed with this, we can plot it any way we want, such as with plot() and text() (for labels):
# plot and add labels
plot(p$x[,1],p$x[,2])
text(p$x[,1],p$x[,2],labels=rownames(p$x))
If we are making a scatterplot with many observations, the labels may not be readable. We therefore might want to only label more extreme values, which we can identify with quantile():
#make a new dataframe with the info from p we want to plot
df <- data.frame(PC1=p$x[,1],PC2=p$x[,2],labels=rownames(p$x))
#make sure labels are not factors, so we can easily reassign them
df$labels <- as.character(df$labels)
# use quantile() to identify which ones are within 25-75 percentile on both
# PC and blank their labels out
df[ df$PC1 > quantile(df$PC1)["25%"] &
df$PC1 < quantile(df$PC1)["75%"] &
df$PC2 > quantile(df$PC2)["25%"] &
df$PC2 < quantile(df$PC2)["75%"],]$labels <- ""
# plot
plot(df$PC1,df$PC2)
text(df$PC1,df$PC2,labels=df$labels)