Search code examples
rggplot2plotpie-chart

Combining 2 plots into 1 plot in R


I want to plot 2 plots on one plot. Below is the code for the creating the plots. The plots are created, but I'm trying to put them together on the same plot. I don't think these plots are [plots] or [ggplots] types. Thanks.

library("fPortfolio")
library('fPortfolio')
data = SPISECTOR.RET
asset = dim(data)[2]

constraints <- c('minW[1:asset]=0','maxW[1:asset]=0.3')
spec <- portfolioSpec()
setNFrontierPoints(spec) <- 25
setSolver(spec)<- "solveRquadprog"

frontier <-portfolioFrontier(data, spec, constraints)

Pont <- 6

#I don't know if GGplot is needed
library(ggplot2)

#plot First Point [I don't think this is a ggplot]
plot1 <- weightsPie(object = frontier, pos = Pont, labels = F, col = rainbow(asset),
                    box = F, legend = F, radius = 0.8)

#plot Second Point [I don't think this is a ggplot]
plot2 <- weightsPie(object = frontier, pos = Pont, labels = T, col = rainbow(asset),
                    box = TRUE, legend = T, radius = 0)

#I want to Put the two plots on the same plot
library("gridExtra")
grid.arrange(plot1, plot2, nrow=1, ncol=2)

Solution

  • I know it is with base R, but it shows some output at least. I used layout to arrange the plots:

    # your previous code
    
    layout(matrix(c(1, 2), nrow = 1, byrow = TRUE))
    layout.show(n=2)
    
    plot1 <- weightsPie(object = frontier, pos = Pont, labels = F, col = rainbow(asset),
                        box = F, legend = F, radius = 0.8)
    
    plot2 <- weightsPie(object = frontier, pos = Pont, labels = T, col = rainbow(asset),
                        box = TRUE, legend = T, radius = 0) 
    

    Here the output:

    "Arrangement"