I want to
- plot all data on some layers
(here: geom_point) - plot only a subset on some other layers
(here: geom_text for type "range")
However, I'm getting the text labels for the whole data, while they should only be added for the turquoise points.
I tried subsetting the data, but the output is not the desired. Still, the object sub_data holds only the wanted data.
Any suggestions?
R code:
library(ggplot2)
N <- 10
# create 20 = 2*10 data points
test_data <- data.frame(
idx <- c( 1:N, 1:N ),
vals <- c( runif(N, 0, 1),
rep( 0.5, N)),
type <- c( rep("range", N),
rep("const", N))
)
# this subsets to the 10 data points of type "range"
sub_data <- subset( test_data, type == "range")
ggplot( test_data, aes( x = idx, y = vals)) +
geom_point( aes( colour = type)) +
geom_text( data = sub_data, aes( x = idx + 0.1, label = idx ), size = 3.5)
output: