#get the colours as mentioned in your question
#and you could get the levels from the plot's data
data.frame(colours = unique(g$data[[1]]["colour"]),
label = levels(g$plot$data[, g$plot$labels$colour]))
colour label
1 #F8766D setosa
51 #00BA38 versicolor
101 #619CFF virginica
更新:
如果有
p <- p + scale_color_discrete(labels=c("sp1","sp2","sp3"))
然后你可以:
g <- ggplot_build(p)
data.frame(colours = unique(g$data[[1]]["colour"]),
label = g$plot$scales$scales[[1]]$labels)
colour label
1 #F8766D sp1
51 #00BA38 sp2
101 #619CFF sp3