I want to highlight selected points and encountered some strange behaviour. First some dummy data:
a <- 1:50
b <- rnorm(50)
mydata <- data.frame(a=a
If your data is different between different layers, then you need to specify the new data for each layer.
You do this with the data=...
argument for each geom
that needs different data:
set.seed(1)
mydata <- data.frame(a=1:50, b=rnorm(50))
ggplot(mydata,aes(x=a,y=b)) +
geom_point(colour="blue") +
geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5)
Another option adding the conditions for both attributes, colour and size, inside geom_point
. Then we control manually those using scale_colour_manual
and scale_size_manual
respectively.
set.seed(1)
mydata <- data.frame(a = 1:50, b = rnorm(50))
ggplot(mydata) +
geom_point(aes(x = a, y = b, colour = a > 9 & a < 14, size = a > 9 & a < 14)) +
scale_colour_manual(values = c("blue", "red")) +
scale_size_manual(values =c(1, 4))+
theme(legend.position = "none")
Another solution with gghighlight:
a <- 1:50
b <- rnorm(50)
mydata <- data.frame(a=a,b=b, type = sample(letters, 50, replace = T))
library(gghighlight)
gghighlight_point(mydata, aes(x=a, y=b), label_key = type,
a <= 14 & a >= 10 & b >= 0 , col="red")