for loop generates multiple plots - r

I have a question about the for loop in combination with the plot function.
I want to use a for loop function (see below) to plot multiple points in one plot. But my loop generates for each point his one plot. So with an i of 35 I generate 35 plot. My question is, is there a way to plot all the points in the same plot?
pdf("test plot.pdf")
for (i in 1:nrow(MYC)){
plot(MYC[i,1], MYC[i,2]
}
dev.off()
Thank you all!

As mentioned in the comments, you are in essence trying to do multiple plots with a loop. R doesn't understand that actually want to plot only points. There's a cure for that, and it comes in vials of points(). Before calling a loop, construct your plot using the type argument. This will make an empty plot, something along the lines of:
plot(your.data, type = "n")
You can then use your loop (with points) to add points to this existing plot.

Related

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

Obtaining all pairwise scatterplots amongst variables

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)
}
}

Plot 2 xts time series in one plot

Hi I thought this would be a simple task, 2hrs later and I am still struggling.
I was able with this code
chartSeries(snp.obj, TA=c("addTA(over,layout=NULL)"))
However it comes with a 2 paned plot but I am looking for these two xts objects overlayed with different y-axis to be in one plot not like I have it in the chartSeries plot.
Answer that works but possibly not so elegant:
over = xts(over, order.by=snp.obj[121:1730])
plot(snp.obj, main='Shiller PE Timer')
lines(2000*over+1, col= 'red')`
Use the on= argument to addTA (see ?addTA)
chartSeries(snp.obj, TA=c("addTA(over, on=1)"))

how to plot in R

I am struggling with the following R code for plotting;
set.seed(1234)
x1=rnorm(100)^2
Index=1:length(x1)
cutoff=3
plot(Index,x1,type="h")
abline(h=cutoff,lty=2)
Firstly, I want to plot also the index of those values that are greater than cutoff value?
And secondly, I have five plots similar to above, I am using
par(mfrow= c(3,2))
It give 3 x 2 space, but for fifth plot, I need to plot it in the center;
The layout() function will be your best friend in terms of arranging the plots.
Check http://www.statmethods.net/advgraphs/layout.html for a good tutorial on the function.

In R, how to plot bars only in a certain interval of data?

My problem is very simple.
I have to plot a data series in R, using bars. Data are contained in a vector vet.
I've used barplot, that plots my data from the first to the last:
barplot(vet), and everything was fine.
Now, on the contrary, I would like to plot not all my data, but just a part of them: from 10% to the end.
How could I do this with barplot()?
How could I do this with plot()?
Thanx
You need to subset your data before plotting:
##Work out the 10% quantile and subset
v = vet[vet > quantile(vet, 0.1)]
It is not clear exactly what you want to do.
If you want to plot only a subset of the bars (but the whole bars) then you could just subset the data before passing it to barplot.
If you want to plot all the bars, but only that part beyond 10% (not include 0) then you can do this by setting the ylim argument. But it is very discouraged to do a barplot that does not include 0. You may be better off using a dotplot instead of a barplot if 0 is not meaningful.
If you want the regular plot, but want to exclude plotting outside of a given window within the plot then the clip function may be what you want.
The gap.barplot function from the plotrix package may also be what you want.

Resources