I have a plot with many overlapping points (goes from 2-10). Adding jitter to the points makes it very noisy and unappealing. I like adding a alpha in the aesthetics. However, I'd like to have a legend where a reader can see how many points are overlapping for 开发者_开发技巧each of those transparencies. Is there such a way?
ggplot(data=mydata,aes(x=x,y=y)) + geom_point(size=3,shape=2,aes(alpha=1/3))
Let's say I use the above code. How would I incorporate a legend for the alpha?
Here is an example of how to fake this. Try it a couple times while varying alpha
.
require(ggplot2)
n = 10000
alpha = 0.01
set.seed(12345)
data = data.frame(replicate(2, rnorm(n)))
dev.new(width=4, height=3)
p = qplot(X1, X2, data=data, alpha=alpha)
fake_scale = scale_alpha('# of overlapping pts', breaks=pretty(c(alpha, 1)), labels=as.character(pretty(c(alpha, 1))/alpha))
p + fake_scale
alpha = 0.1
alpha = 0.01
Not exactly what you want, but how about geom_hex()?
If you don't discretize (bin) it, I think R would need to calculate the overlapped area and the number of overlappedness(sp?) (which would also depend on point size), and that sounds hard.
library(hexbin)
mydata <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(data=mydata,aes(x=x,y=y)) + geom_hex()
Taking cues from other answers, it seems like you may want to bin the x-y points into unique values, and then sum up the number of unique values per each x-y coordinate bin.
Using the fake data from another answer,
library(ggplot2)
library(dplyr)
library(magrittr)
n = 10000
set.seed(12345)
data = data.frame(replicate(2, rnorm(n)))
I'm binning by the 1st decimal place, and then counting how many are in each bin
data2 <- data %>% mutate(
x = round(X1,1),
y = round(X2,1)
) %>%
group_by(x,y) %>%
tally() %>%
dplyr::filter(n<=10) %>%
mutate(transp = n/10)
I filtered out the biggest bins just for illustration purposes. Now, the transp
column has the calculation you want, which you can supply to alpha
and get it in the legend
data2 %>%
ggplot(aes(x=x,y=y)) +
geom_point(aes(alpha = as.factor(transp))) +
scale_alpha_discrete(
"Number of overlapping points",
labels = 1:10
)+
theme(legend.position = "bottom")
pic:
精彩评论