Replace point shape with subject name on ggplot

Viewed 87

I was trying to use ggplot to make a plot with following needs.

  1. Use points to specify subjects.
  2. Use color to specify models. I have 6 models, so each subject should appear 6 times on the plot.

The plot is expected to look something like this: enter image description here

I can use color to specify models but cannot find a way to specify subjects as point shapes.

Example data

structure(list(subject = c("S1", "S8", "S3", "S9"), alphamean = c(0.224104019995071, 
                                                                       0.195354811041001, 0.5675953626788, 0.491972414993715), lambdamean = c(0.35985383877637, 
                                                                                                                                              0.268124038994992, 0.92122181060701, 0.43561465728315), model = c("a", 
                                                                                                                                                                                                                "b", "c", "d")), row.names = c(NA, -4L), class = c("data.table", 
                                                                                                                                                                                                                                                                   "data.frame"))

My attempts

data %>%
  ggplot(aes(x = alphamean, y = lambdamean)) +
  geom_point(aes(color=model,shape=subject)) +
  scale_shape_manual(values = paste0('S',1:40))
1 Answers

You could use geom_text instead of geom_point:

library(ggplot2)
library(magrittr)

data %>%
  ggplot(aes(x = alphamean, y = lambdamean)) +
  geom_text(aes(color=model,label=subject))

Related