1

I've been trying to change the legend symbols of my plot. This question here was really helpful, however I'm trying to create symbols that go from 1-33, and whenever I have 2 digits I get an error. Is there a way to adapt this code for this purpose? Here's a figure of my plot with labels from 1-9 then random letters just so it would show.

enter image description here

Here's my code (R v3.6.2, RStudio 1.2) for this plot:

ggplot(data_plot, aes(x = EV1, y = EV2, color = local)) +
  geom_point(size = 0, stroke = 0) +
  geom_text(check_overlap = F, aes(label = map_id, colour = local), show.legend = FALSE) +
  guides(colour = guide_legend(override.aes = list(size = 5, shape = c(utf8ToInt("1"), utf8ToInt("2"), utf8ToInt("3"), utf8ToInt("4"), utf8ToInt("5"), utf8ToInt("6"), utf8ToInt("8"), utf8ToInt("9"), utf8ToInt("A"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"),utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"),utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"), utf8ToInt("K"), utf8ToInt("G"), utf8ToInt("F"))))) +
  labs(title = "Microsatellite data", x = "PC1 (14.6%)", y = "PC2 (10.0%)", color = "Locality") +
  theme_bw()
Rachel
  • 73
  • 7
  • Can you try something like`guides(colour = guide_legend(override.aes = list(size = 5, shape = c(1:33))))` or just `shape=33`? – char Nov 08 '21 at 15:09
  • Thanks for the suggestion, but that doesn't really work. I've tried it and it actually gives me different shapes instead of numbers, and the default only has 25 different shapes, so some of them do not show up! :( Also, just a particular thing about my dataset is that I'm missing #7, so I have to skip that number too. – Rachel Nov 08 '21 at 17:54
  • What's the error you get with double digits? – char Nov 08 '21 at 18:01
  • ```Error in `[[<-.data.frame`(`*tmp*`, i, value = c(49L, 50L, 51L, 52L, 53L, : replacement has 35 rows, data has 34``` I think it's considering as a new entry. – Rachel Nov 08 '21 at 19:43
  • If I don't use the option ```utf8ToInt```, and just make a list of numbers, like this ```shape = c("1", "2", "3", "4", "5", "6", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")``` the error is different: ```Error: Can't find shape name: * '10'``` – Rachel Nov 08 '21 at 19:46
  • [This](https://cran.r-project.org/web/packages/ggplot2/vignettes/ggplot2-specs.html) says you can either do single characters or pre-existing shapes (unicode). So you could either change the key-glyph to a symbol or try the slightly hacky workaround from [here](https://stackoverflow.com/questions/50273824/ggplot2-add-a-guide-for-abbreviations). – char Nov 09 '21 at 08:35
  • Ah, thank you so much! I'm gonna try those solutions and see if they work for me! – Rachel Nov 09 '21 at 17:53

0 Answers0