adding data to existing ggplot2 plot

ggplot2, r

Solution

First, in your data frame `mdat` don't use the quotes around the numbers because that makes them as characters.

mdat <- data.frame(hwy = c(35, 40, 25),
                   cty = c(20, 25, 10))
mdat$class <- c("generic1", "generic2", "generic3")

If you need to represent those new point with different colours then class for existing points then it is better to use shapes that allow to set fill, so they use different scale.

p+geom_point(data=mdat,aes(hwy,cty,shape=class,fill=class),size=7)+
  scale_shape_manual(values=c(21,22,23))

Problem

I am using the ggplot2 package on the cars dataset. I have created a grid based on the manufacturer and color coded by class.. What I would like to do is: add to each plot in the grid from the dataset mdat.Data should be represented as data points. The output I would like to see is the data in mdat is to be represented in each plot as dots, the class should be added to the legend, I would like each point to be represented with a different color and shape. I am not sure how this can be done and any help is appreciated. Thanks so much! ``` ## Sample data p <- ggplot(mpg, aes(x=hwy, y=cty)) p<- p + facet_grid(. ~ manufacturer) + facet_wrap(~manufacturer) p<- p + geom_point(aes(colour = class), size = 7) p<- p + scale_colour_brewer() p<- p + geom_point(shape = 1, size = 7, alpha = I(0.7)) print(p) ##now add on the randomn portfolios on another grid hwy = c("35", "40", "25") cty = c("20", "25", "10") mdat <- data.frame(hwy,cty) mdat$class <- c("generic1", "generic2", "generic3") ```

Original source