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.
Related
I am writing some plotting functions for a large time series database (extract of the data). I wrote a code to plot all the parameters (columns) against the time (first column of the data frame) linked to a given cycle (data frame). I am using a loop to plot all the parameters at once. The problem is that when I call the function, that uses the package ggplot2, only 20 plots out of the needed 34 appear in different windows. I obtain all plots between plot of parameter 11 (Gas_Flow_Mon_01) and plot of the last parameter 34 (Timer_24_Resettable_Value). I don't know if the problem lies within my code or if 20 is the full capacity of windows displayed at once using ggplot2? I have heard of functions like dev.off() but don't know if they would help in dealing with this issue.
# Function plotting all the parameters for one cycle using the indexes of the parameters and the cycle :
# `plot_cycle_allparameters_interactive()`
library(plotly)
library(tidyr)
plot_cycle_allparameters_interactive <- function(datafile,Cycle_index){
Cycle_name = names(datafile)[Cycle_index]
Cycle_data = datafile[[Cycle_name]]
# Cycle_data is the datafile studied that consists of multiple columns, each one representing a parameter
for(i in 2:length(Cycle_data)){
# looping through all the columns that represent a parameter
figure_interactive <- ggplot(Cycle_data, aes(x =Time,y = Cycle_data[,i], group= 1)) +
geom_line(color='blue')
print(ggplotly(figure_interactive))
dev.off()
}
}
I'm trying to do something very simple:
I'm using the library Hmisc in order to use the Ecdf function to get the x and y values that the function returns, but even when I assign it to a variable, the default plot is displayed.
library("Hmisc")
ecdf1 <- Ecdf(F) # Plot is displayed
How to avoid displaying the plot and only get the results?
set pl=FALSE
ecdf1 <- Ecdf(F, pl=FALSE)
From Ecdf help file you can read:
set to F to omit the plot, to just return estimates
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)
}
So, I am able to use the plot() function in R to graph different functions. However, I am finding that the graphs in R do not typically show the entire curve of the function. Let me use an example:
fun <- function(x){
x^3 + 2*x^2 + 3*x + 4
}
plot(fun)
However, when I plot the same function using the Desmos Graphing Calculator it shows all four quadrants of the Cartesian graph whereas R is only showing one:
My question: How can I modify RPlot to show all four quadrants as opposed to just one as in the above case?
I think you can do this just by extending the default range (which is [0,1]):
plot(fun,from=-5,to=5,ylim=c(-8,8),col="red")
grid()
abline(v=0,h=0,lty=2)
I've added a few frills to make it look a little more like the desired plot. Adding a point on the y axis is easy; adding the x-intercept is not quite so easy.
points(0,fun(0),pch=16)
points(Re(polyroot(c(4,3,2,1))[2]),0,pch=16)
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)