I am attempting to display 4 separate plots in the same figure. I am using the following code:
library(lattice)
library(gridExtra)
p1 = dotplot(mpg ~ cyl, data = mtcars)
grid.arrange(p1,p1,p1,p1)
but this gives me a plot that is sort of smushed. How can I make this more visually pleasing? I want to make the graphs bigger so they can actually be read. I tried adding the heights argument to grid.arrange but this did not help
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)
I want to plot several variables and their respective correlation coefficients using the function pairs().
It works well, though I would like to put all the axes' legends on the bottom and left side of the plot.
By default, they are changing side every two plots as you can see on this example:
pairs(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=iris)
If anyone has an answer with and without ggplot2 R package, that would be perfect.
Use GGally
library(GGally)
ggpairs(data=iris)
Or just the continuous columns
ggpairs(data = iris[, 1:4])
How do plot multiple lattice plots onto a single lattice plot where the plots are generated using an lapply function?
The following is a demonstration of what I have tried so far using the built in mtcars dataset.
require(lattice)
response <- c("cyl","disp","hp","drat")
par(mfrow=c(2,2))
lapply(response, function(variable) {
print(xyplot(mtcars$mpg ~ mtcars[variable]))
})
This produces the plots desired. However it seems to be ignoring the par(mfrow=c(2,2)) instruction and plotting each plot separately.
If you really don't want to use the built-in facetting or viewport options of lattice, you can replicate the behavior of par(mfrow) with the following,
require(lattice)
response <- c("cyl","disp","hp","drat")
# save all plots in a list
pl <- lapply(response, function(variable) {
xyplot(mtcars$mpg ~ mtcars[variable])
})
library(gridExtra)
# arrange them in a 2x2 grid
do.call(grid.arrange, c(pl, nrow=2))
Your example is not how lattice is intended to be used (grid would be more appropriate).
Here is a lattice solution:
xyplot(mpg ~ cyl+disp+hp+drat,
data=mtcars,
groups=cyl+disp+hp+drat,
scales=list(relation="free"),
col="blue"
)
The multiplot function on this page is something I have used many times to get multiple plot objects on one page.
I've generated a set of levels from my dataset, and now I want to find a way to sum the rest of the data columns in order to plot it while plotting my first column. Something like:
levelSet <- cut(frame$x1, "cutting")
boxplot(frame$x1~levelSet)
for (l in levelSet)
{
x2Sum<-sum(frame$x2[levelSet==l])
}
or maybe the inside of the loop should look like:
lines(sum(frame$x2[levelSet==l]))
Any thoughts? I am new to R, but I can't seem to get a hang of the indexing and ~ notation thus far.
I know r doesn't work this way, but I'd like functionality that 'looks' like
hist(frame$x2~levelSet)
## Or
hist(frame$x2, breaks = levelSet)
To plot a histograph, boxplot, etc. over a level set:
Try the lattice package:
library(lattice)
histogram(~x2|equal.count(x1),data=frame)
Substitute shingle for equal.count to set your own break points.
ggplot2 would also work nicely for this.
To put a histogram over a boxplot:
par(mfrow=c(2,1))
hist(x2)
boxplot(x2)
You can also use the layout() command to fine-tune the arrangement.