1

I need to combine the 2 plots from different packages in the same page?

library(treemap)

cars_mpg <- mpg

mfg <- sort(table(cars_mpg$manufacturer),decreasing = T)
par(mar= c(6,4,4,4))
barplot(mfg,las=2,ylim = c(0,40),col=cm.colors(15),yaxp= c(0,40,20),cex.axis =0.8,cex.names = 0.8,
        main = "Number of Cars per Manufacturer") + 
text(c(1:length(mfg))*1.2 -0.5, mfg+1 , mfg ,cex=0.8) +
text(14, mean(mfg) +1, paste("mean = ",round(mean(mfg),2)) ,cex=1,col = "red") +
abline(h=mean(mfg) , col="red")

#b
cars_class <- as.data.frame(table(cars_mpg$class))
cars_class$labels <- paste(cars_class$Var1, "-", (cars_class$Freq))
treemap(cars_class,index = "labels",vSize = "Freq",palette = "Pastel1",
        fontsize.labels=9,fontcolor.labels = "black",title="Cars in each class",
        border.lwds=1)

I tried grid.arrange and some other methods but none worked for me.

  • If you feel the linked post does not solve your problem fully, please comment and I can reopen the question. – missuse Dec 11 '21 at 07:59
  • Here is another approach https://statisticsglobe.com/combine-base-r-ggplot2-lattice-plots – missuse Dec 11 '21 at 08:06

0 Answers0