Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the shape for a subset of points with ggplot2

Tags:

r

ggplot2

I'm attempting to plot a large scatter plot that varies along a large number of dimensions.

Here's my starting plot:

 p <- ggplot(mtcars, aes(wt, mpg, shape=cyl, colour=gear, size=carb)) +
      geom_point()

Sample plot with multiple variables

Using the mtcars dataset, I'm simply including various shapes, colours, and sizes. Now what i'd like to add to this is some distinguishing mark around all points with carb==8. For example, add in a big black circle around these points.

Doing

p + geom_point() + geom_point(colour="black", shape=1, size=5)

is close in that it gets the circles as I want, but all points get circles, I want to instead limit this to just some of the points.

I would greatly appreciate any suggestions for this problem.

like image 559
fozziethebeat Avatar asked Dec 21 '11 00:12

fozziethebeat


People also ask

How do I assign shapes in ggplot2?

You can change the number to plot different shapes, i.e. geom_point(shape = x) . If you want to change point shapes based on a grouping variable, then first set the shape with the grouping variable in geom_point and then use scale_shape_manual to choose the desired shapes (optional).

Can you subset in Ggplot?

Method 1: Using subset() function Here, we use subset() function for plotting only subset of DataFrame inside ggplot() function inplace of data DataFrame. All other things are same. Parameters: It takes data object to be subsetted as it's first parameter.

What is %>% in Ggplot?

the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library. It would be something like (df %>% sample_frac(0.01))

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.


1 Answers

Instead of initially putting your variables within the ggplot function, try to map your variables individually for every layer you add.

p <- ggplot(data = mtcars)+
geom_point(aes(wt, mpg, shape=as.factor(cyl), colour=gear, size=carb))+
geom_point(aes(wt[carb==8], mpg[carb==8]), colour="black", shape=1, size=7)  

img

like image 104
oeo4b Avatar answered Oct 03 '22 05:10

oeo4b