Skip to content
Prev 395208 / 398502 Next

Is it possible to get a downward pointing solid triangle plotting symbol in R?

Sadly, no.? Still shows the same legend with both sets of fill 
mappings.? I have found a workaround, sadly
much longer than yours (!) that does get me what I want but it is a real 
bodge.? Still interested to see
if there is a way to create a downward pointing solid symbol but here is 
my bodge using new_scale_fill()
and new_scale_color() from the ggnewscale package (many thanks to Elio 
Campitelli for that).

library(tidyverse)
library(ggnewscale) # allows me to change the scales used
tibble(x = 2:9, y = 2:9,
 ?????? ### I have used A:C to ensure the changes sort in the correct 
order to avoid the messes of using shape to scale an ordinal variable
 ?????? ### have to say that seems a case where it is perfectly sensible 
to map shapes to an ordinal variable, scale_shape_manual() makes
 ?????? ### this difficult hence this bodge
 ?????? c = c(rep("A", 5), "B", rep("C", 2)),
 ?????? change = c(rep("Deteriorated", 5), "No change", rep("Improved", 
2))) %>%
 ? ### this is just keeping the original coding but not used below
 ? mutate(change = ordered(change,
 ????????????????????????? levels = c("Deteriorated", "No change", 
"Improved"))) -> tmpTibPoints
### create the area mapping
tibble(x = c(1, 5, 5, 1), y = c(1, 1, 5, 5), a = rep("a", 4)) -> tmpTibArea1
tibble(x = c(5, 10, 10, 5), y = c(1, 1, 5, 5), a = rep("b", 4)) -> 
tmpTibArea2
tibble(x = c(1, 5, 5, 1), y = c(5, 5, 10, 10), a = rep("c", 4)) -> 
tmpTibArea3
tibble(x = c(5, 10, 10, 5), y = c(5, 5, 10, 10), a = rep("d", 4)) -> 
tmpTibArea4
bind_rows(tmpTibArea1,
 ????????? tmpTibArea2,
 ????????? tmpTibArea3,
 ????????? tmpTibArea4) -> tmpTibAreas
### now plot
ggplot(data = tmpTib,
 ?????? aes(x = x, y = y)) +
 ? geom_polygon(data = tmpTibAreas,
 ?????????????? aes(x = x, y = y, fill = a),
 ?????????????? alpha = .5) +
 ? scale_fill_manual(name = "Areas",
 ??????????????????? values = c("orange", "purple", "yellow", "brown"),
 ??????????????????? labels = letters[1:4]) +
 ? ### next two lines use ggnewscale functions to reset the scale mappings
 ? new_scale_fill() +
 ? new_scale_colour() +
 ? ### can now use the open triangles and fill aesthetic to map them
 ? geom_point(data = tmpTibPoints,
 ???????????? aes(x = x, y = y, shape = c, fill = c, colour = c),
 ???????????? size = 6) +
 ? ### use the ordered variable c to get mapping in desired order
 ? ### which, sadly, isn't the alphabetical order!
 ? scale_shape_manual(name = "Change",
 ?????????????????? values = c("A" = 24,
 ????????????????????????????? "B" = 23,
 ????????????????????????????? "C" = 25),
 ?????????????????? labels = c("Deteriorated",
 ????????????????????????????? "No change",
 ????????????????????????????? "Improved")) +
 ? scale_colour_manual(name = "Change",
 ?????????????????? values = c("A" = "red",
 ????????????????????????????? "B" = "grey",
 ????????????????????????????? "C" = "green"),
 ?????????????????? labels = c("Deteriorated",
 ????????????????????????????? "No change",
 ????????????????????????????? "Improved")) +
 ? scale_fill_manual(name = "Change",
 ?????????????????? values = c("A" = "red",
 ????????????????????????????? "B" = "grey",
 ????????????????????????????? "C" = "green"),
 ?????????????????? labels = c("Deteriorated",
 ????????????????????????????? "No change",
 ????????????????????????????? "Improved"))

That gives the attached plot which is really what I want.? Long bodge 
though!*
*
On 06/10/2023 11:50, Jan van der Laan wrote: