This question already has answers here:
Lattice: multiple plots in one window?
(3 answers)
Closed 7 years ago.
So I've been trying to get two Dotplots (Hmisc package) to turn up in the same graph but R draws them sequentially instead.
Anybody know how to draw two Dotplots in the same graph, specifically using the Hmisc package?
I've tried somethings like
par(mfrow = c(1, 2))
Dotplot(latitude~mortality, data=USmelanoma)
Dotplot(longitude~mortality, data=USmelanoma)
But it draws them in two seperate graphs, one after the other instead of in one graph, as I had expect par(mfrow = c(1,2)) to do
You can't mix base graphics with lattice graphics which you are trying to do here (well, you can but not easily and not in the manner you are trying to do here).
One way is to use the grid.arrange() function in the gridExtra package, e.g.
## load packages required
library("HSAUR2")
library("Hmisc")
library("gridExtra")
## draw each plot separately and save to objects
plt1 <- Dotplot(latitude ~ mortality, data = USmelanoma)
plt2 <- Dotplot(longitude ~ mortality, data = USmelanoma)
## arrange the stored plots
grid.arrange(plt1, plt2, ncol = 2)
This gives:
This is an easy way to do what you want; there are other ways to do this directly with functions in the grid package itself, but they require a little more from the user.
Related
I am new to R and I am trying to plot multiple CUSUM charts in one display. I have tried par(mfrow=c(2,1)), layout(), cowplot() and it did not work.
The function mentioned does makes the 1st CUSUM chart smaller and be plotted like normal plot(); successfully plotted 1st CUSUM at top half. However, the 2nd CUSUM chart just refreshes the display automatically instead of being plot below the 1st chart in the same display. Any solution or just possible solution is welcomed, many thanks in advance.
To be clear, I am not talking about plotting 'cumsum' data but CUSUM charts with the cusum() function like the following:
cusum(data, std.dev = standard_deviation_of_data, center = center, add.stats=FALSE, xlab="Studies", title="CUSUM chart", labels=labels)
The issue is most likely associated with the fact that a plot of class "cusum.qcc" is generated.
I do not know how to make this work as a base r plot.
I tried various ways to plot in base r including: par(mfrow...), layout(...), and par(... new = TRUE)
Also tried to convert the cusum plot into a grob so as to use gridExtra::grid.arrange()
None of these efforts worked, so resorted to saving the plots as images and combining images
library(qcc)
library(magick)
data
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)
save as images
jpeg(file="q1.jpeg")
cusum(diameter[1:25,], decision.interval = 4, se.shift = 1)
dev.off()
jpeg(file="q2.jpeg")
cusum(diameter[1:25,], newdata=diameter[26:40,])
dev.off()
read and combine images
q1 <- image_read("q1.jpeg")
q2 <- image_read("q2.jpeg")
img <- c(q1, q2)
image_append(img)
Created on 2020-07-09 by the reprex package (v0.3.0)
I am ploting a multiple histogram for 5 variables, and I have the same title repeated along all of them. I didn´t find a way to personalize it for each histogram. Is there a way to do it? I am using the psych R package.
Maybe another package to recommend?
The code line:
multi.hist(AutosCompleteNorm[,11:15],main="bah")
And "bah" is repeated 5 times. I tried c("a","b",..."e") as an argument but it didn´t work.
Package documentation:
https://www.rdocumentation.org/packages/psych/versions/1.8.3.3/topics/multi.hist
Thanks!
The current psych package doesn't seem to support this, which is strange because it would have been a natural feature for such a plot.
For customizability, I recommend you to take a look at ggplot and then layout in the format you wish using gridExtra.
Here's the code to create the few histogram in ggplot:
library(ggplot2)
p1 <- ggplot(vids, aes(x=log(likes)))+geom_histogram()+labs(title="title1")
p2 <- ggplot(vids, aes(x=log(dislikes)))+geom_histogram()+labs(title="title2")
p3 <- ggplot(vids,
aes(x=log(comment_count)))+geom_histogram()+labs(title="title3")
And then laying them up in a 2 row layout (nrow=2):
library(gridExtra)
grid.arrange(p1, p2, p3, nrow = 2)
Changing the layout to nrow=1:
Following these comments, I just updated psych so that multi.hist is more useful.
You can now specify the margins for the plots, and it will, by default label each plot with the variable name.
I have not pushed the development version of psych (1.8.9) to CRAN yet, but it is available on my repository at
install.packages("psych",repos="https://personality-project.org/r",type="source")
I am trying to create a tiled plot using plot.kmeans functions from the package "useful". This can be done very easily for base plot function using par() or layout(), or in ggplot using facests. I want to visualize results of various runs of kmeans with different number of clusters using plot or plot.kmeans function from the package "useful". I have tried par() and layout(), but I only get one plot not multiple plots.
Consider the following code segment:
results1 <- kmeans(x=dataset1,centers=5,nstart = 25)
results2 <- kmeans(x=dataset2,centers=5,nstart = 25)
par(mfrow=c(2,1))
plot.kmeans(results1,dataset1)
plot.kmeans(results2,dataset2)
I have two datasets and apply kmeans on them separately. I want to draw results of both datasets side by side. plot.kmeans is good function to see results of clustering. But somehow i feel that we cannot two or more plots side by side like we do in case of base plotting facility. If instead of plot.kmeans, I use base plot functions it will work. So thats my problem in brief.
Thanks.
The function plot.kmeans from the package "useful" (not to be confused with general qualifier 'a useful package) returns a ggplot2 object. These do not work with par() or layout().
Look instead at grid.arrange from the "gridExtra" package:
results1 <- kmeans(x=dataset1,centers=5,nstart = 25)
results2 <- kmeans(x=dataset2,centers=5,nstart = 25)
library(gridExtra)
p1 <- plot.kmeans(results1,dataset1)
p2 <- plot.kmeans(results2,dataset2)
grid.arrange(p1, p2, ncol=2)
This question already has answers here:
Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function? [duplicate]
(3 answers)
R: How should I create Grid-graphics?
(2 answers)
Closed 8 years ago.
I am trying to plot a scatterplot and box plot of two continuous varaibles but am getting an error that says,
Warning message:
In par(fig = c(0, 0.8, 0.55, 1), new = TRUE) :
calling par(new=TRUE) with no plot
The code worked when I simply replaced lines 4-6 of my code with:
plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
Here's my code:
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
par(fig=c(0,0.8,0,0.8), new=TRUE)
#plot(mydata$gre, mydata$gpa, xlab="GRE",ylab="GPA")
d<-ggplot(mydata,aes(x=mydata$gre, y=mydata$gpa))
d<-d+geom_line()
d
par(fig=c(0,0.8,0.55,1), new=TRUE)
boxplot(mydata$gre, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mydata$gpa, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
Could you please shed some light on what I am doing wrong with regards to ggplot since R isn't recognizing it? What's really weird is that when I type d, the name of my ggplot, I get a plot...
You are attempting to combine grid plots (ggplot2) and base plots (boxplot), but those 2 types of plotting do not play nicely together (hence the cryptic warning).
The simplest solution is to only use one of grid or base plots by replacing the call to ggplot with a call to plot or other functions (base only option) or using grid based functions to do the boxplots (lattice package also uses grid) then use functions from the grid package to arrange the multiple plots.
If you really want to combine grid and base plots then you can use the gridBase package, but this is going to require understanding both types of graphics quite well.
I have been trying to plot simple density plots using R as:
plot(density(Data$X1),col="red")
plot(density(Data$X2),col="green")
Since I want to compare, I'd like to plot both in one figure. But 'matplot' doesn't work!! I also tried with ggplot2 as:
library(ggplot2)
qplot(Data$X1, geom="density")
qplot(Data$X2, add = TRUE, geom="density")
Also in this case, plots appear separately (though I wrote add=TRUE)!! Can anyone come up with an easy solution to the problem, please?
In ggplot2 or lattice you need to reshape the data to seupose them.
For example :
dat <- data.frame(X1= rnorm(100),X2=rbeta(100,1,1))
library(reshape2)
dat.m <- melt(dat)
Using ``lattice`
densityplot(~value , groups = variable, data=dat.m,auto.key = T)
Using ``ggplot2`
ggplot(data=dat.m)+geom_density(aes(x=value, color=variable))
EDIT add X1+X2
Using lattice and the extended formua interface, it is extremely easy to do this:
densityplot(~X1+X2+I(X1+X2) , data=dat) ## no need to reshape data!!
You can try:
plot(density(Data$X1),col="red")
points(density(Data$X2),col="green")
I must add that the xlim and ylim values should ideally be set to include ranges of both X1 and X2, which could be done as follows:
foo <- density(Data$X1)
bar <- density(Data$X2)
plot(foo,col="red", xlim=c(min(foo$x,bar$x),max(foo$x,bar$x)) ylim=c(min(foo$y,bar$y),max(foo$y,bar$y))
points(bar,col="green")
In base graphics you can overlay density plots if you keep the ranges identical and use par(new=TRUE) between them. I think add=TRUE is a base graphics strategy that some functions but not all will honor.
If you specify n, from, and to in the calls to density and make sure that they match between the 2 calls then you should be able to use matplot to plot both in one step (you will need to bind the 2 sets of y values into a single matrix).