Ggplot2 scatter plot6/8/2023 Let’s restrict it to something better: ggplot(d, aes(a, b, color = pc, alpha = 1/density)) + By default, this range is 0 to 1, making the most distant points have an alpha close to 1. Our final fix is to use scale_alpha to tweak the alpha range. You can see that distant points are now too vibrant. Now plot with alpha mapped to 1/density: ggplot(d, aes(a, b, color = pc, alpha = 1/density)) + That is, turn down alpha wherever there are lots of points! The trick is to use bivariate density, which can be added as follows: # Add bivariate density for each point To solve this, we’ll map alpha to the inverse point density. 05) +īetter, except it’s now hard to see extreme points that are alone in space. Geom_point(shape = 16, size = 5, show.legend = FALSE, alpha =. What if we take alpha down really low to. However, what if you have many points? Let’s try with 5,000 points: # Simulate data We could adjust it to be the same for every point: ggplot(d, aes(a, b, color = pc)) + Now it’s time to get rid of those offensive mushes by adjusting the transparency with alpha. For example: ggplot(d, aes(a, b, color = pc)) + Now we can add color, let’s pick something nice with the help of the scale_color_gradient functions and some nice hex codes (check out color-hex for inspriation). Add it to the data frame as a variable pc and use it to color like so: d$pc <- predict(prcomp(~a+b, d)) To do this, we can color points by the first principal component. Instead, we want the color to change in a direction that characterises the correlation - diagonally in this case. Geom_point(shape = 16, size = 5, show.legend = FALSE) +Īlthough it’s subtle in this plot, the problem is that the color is changing as the points go from left to right. For example, color by a (and hide legend): ggplot(d, aes(a, b, color = a)) + One option is to color by one of the variables. We want to color the points in a way that helps to visualise the correlation between them. Here’s the combination I settled on for this post: ggplot(d, aes(a, b)) + There are many ways to tweak the shape and size of the points. Using ggplot2, the basic scatter plot (with theme_minimal) is created via: library(ggplot2) In a ame d, we’ll simulate two correlated variables a and b of length n: set.seed(170513) We’ll learn how to create plots that look like this: Pretty scatter plots with here to make pretty scatter plots of correlated variables with ggplot2!
0 Comments
Leave a Reply. |