r - ggplot2 - highlighting selected points and strange behavior

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,b=b)
ggplot(mydata,aes(x=a,y=b)) + geom_point()

This works correctly. Now,to highlight some points, I add another geom_point layer:

ggplot(mydata[20:40,],aes(x=a,y=b)) + 
    geom_point() + 

Note that I am displaying only a limited range of the data ([20:40]). Now comes the strange behavior:

ggplot(mydata[10:40,],aes(x=a,y=b)) + 
    geom_point() + 

Changing the size of the selected range, I get an error, roughly translated from German: Error...: Arguments implying different number of rows. Strangely, this varies with the selected range. [23:40] will work, [22:40] won't.

The error in English is:

Error in data.frame(x = c(19L, 21L, 22L), y = c(0.28198, -0.6215,  : 
  arguments imply differing number of rows: 3, 31


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:

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 solution with gghighlight:

a <- 1:50
b <- rnorm(50)
mydata <- data.frame(a=a,b=b, type = sample(letters, 50, replace = T))

gghighlight_point(mydata, aes(x=a, y=b), label_key = type, 
                  a <= 14 & a >= 10 & b >= 0 , col="red")

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.

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")

Need Your Help

Prevent launching multiple instances of a java application

java runtime executable-jar

I want to prevent the user from running my java application multiple times in parallel.

What is the difference between a language construct and a "built-in" function in PHP?

php function built-in language-construct

I know that include, isset, require, print, echo, and some others are not functions but language constructs.