

Let's learn this with the help of an example where we will plot multiple normal distribution curves. Then we add the second data set using the points () or lines () function. This is also why the other set of points didn't appear.įinally, the recommendation to read ?plot was a bit misleading, since really ?fault is a bit more informative for beginners. To plot multiple datasets, we first draw a graph with a single dataset using the plot () function. geompoint and geomline) and define the data set we want to use within each of those geoms. For this, we have to set the data argument within the ggplot function to NULL.
#Plot two datasets on same graph r how to
That's because each time you called plot anew you were starting over from scratch, as if the previous commands never happened. This section shows how to use the ggplot2 package to draw a plot based on two different data sets. You'll notice that your resulting plot didn't end up having y axis limits of 0 to 12. So your repeated calls to plot are just going back and starting over again. In general, each time you call plot, R will start a new plotting device.

Your calls to plot.new and plot.window aren't really necessary if you're just starting with R you probably won't need to use them for a while, really. The general strategy with base graphics in R is you initialize a plot with a single call to plot and then you add to it using things like points, lines, abline etc. The reason I mentioned points and lines was because you were asking how to plot multiple sets of points on the same graph.

Xlab="Standard Deviation", ylab="Expected Return")Ībline(lm(sw_r~sw_sd),col = 'forestgreen',lwd = 3)Ībline(lm(aa_r~aa_sd),col = 'blue',lwd = 3) If there are multiple data sets being plotted on the same graph. Using base graphics, I would accomplish what you're doing via something like this: plot(c(sw_sd,aa_sd),c(sw_r,aa_r), pch = 22,Ĭol = rep(c('forestgreen','blue'),each = 2),main="Capital Allocation Lines", For example, you may overlap plots of rainfall in the desert and rainfall in the.
