Neerav - 1 year ago 81

R Question

For eg. You can draw box plot as below:

`qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot")`

and point as:

`qplot(factor(cyl), mpg, data = mtcars, geom = "point")`

How would you combine both - but just to show a few specific points(say when wt is less than 2) on top of the box?

Answer Source

Use `+ geom_point(...)`

on your `qplot`

(just add a `+ geom_point()`

to get all the points plotted).

To plot selectively just select those points that you want to plot:

```
n <- nrow(mtcars)
# plot every second point
idx <- seq(1,n,by=2)
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(cyl)[idx],y=mpg[idx]) ) # <-- see [idx] ?
```

If you know the points before-hand, you can feed them in directly e.g.:

```
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(c(4,6,8)),y=c(15,20,25)) ) # plot (4,15),(6,20),...
```