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()
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.
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).
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.
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))
The function geom_point() adds a layer of points to your plot, which creates a scatterplot.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With