0

I want to plot 2 scatterplots on top of one another with ggplot but I am not very familiar with it. I have been trying to follow other examples but the layered approach to this package confuses me.

In bothfrontier_data I want the first column to be the x variable with respect to the 3rd column and the second column to be the x variable with respect to the 4th column. Also how can I add custom axis titles to this plot and add custom axis ranges? Thank you

############# GGPLOT TO SHOW BOTH PLOTS SUPERIMPOSED ###################################
bothfrontier_data <- data.frame(std_portfolios_Qts, std_portfolios_Qsi,
                                All_Portfolio_Returns_Qts, All_Portfolio_Returns_Qsi)
head(bothfrontier_data)
#   std_portfolios_Qts std_portfolios_Qsi All_Portfolio_Returns_Qts All_Portfolio_Returns_Qsi
#1          0.8273063          0.8194767                 0.3421454                 0.3357710
#2          0.8272188          0.8196555                 0.3421551                 0.3357853
#3          0.8273064          0.8192980                 0.3421648                 0.3357996
#4          0.8271314          0.8194769                 0.3421744                 0.3358139
#5          0.8272191          0.8194770                 0.3421840                 0.3358281
#6          0.8272193          0.8194772                 0.3421935                 0.3358423

dim(bothfrontier_data)
#[1] 501   4

BothFrontiers <- ggplot(bothfrontier_data, aes(x=std_portfolios_Qts)) +
  geom_point(aes(y=All_Portfolio_Returns_Qts), color = "blue") +
  geom_point(aes(y=All_Portfolio_Returns_Qsi), color = "red")
plot(BothFrontiers)
eruiz
  • 59
  • 9
  • Youn doesn't need this `plot()` function. Also, you may pass all `aes` directly within `geom_point`. `BothFrontiers <- ggplot(bothfrontier_data ) + geom_point(aes(x=std_portfolios_Qts, y=All_Portfolio_Returns_Qts), color = "blue") + geom_point(aes(x=std_portfolios_Qts, y=All_Portfolio_Returns_Qsi), color = "red")` But I'm not sure if you want both in the same plot or different layers. To plot, call only the `BothFrontiers` without the `plot()` – Aureliano Guedes Jun 27 '20 at 20:57
  • @AurelianoGuedes Hello. Yes I wanted both in the same plot not next to each other – eruiz Jun 27 '20 at 21:43

1 Answers1

1

You can try:

library(ggplot2)
library(patchwork)
#Plot 1
g1 <- ggplot(bothfrontier_data,aes(x=std_portfolios_Qts,y=All_Portfolio_Returns_Qts))+geom_point(color='blue')+
  ggtitle('Plot 1')
#Plot 2
g2 <- ggplot(bothfrontier_data,aes(x=std_portfolios_Qsi,y=All_Portfolio_Returns_Qsi))+geom_point(color='red')+
  ggtitle('Plot 2')
#Final plot
g1/g2

enter image description here

You can modify axis with scale_x_continuous() and scale_y_continuous(). Labels can be added with xlab() and ylab(). I hope this can help.

Duck
  • 39,058
  • 13
  • 42
  • 84