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

Jan van der Laan rhe|p @end|ng |rom eoo@@dd@@n|
Fri Oct 6 12:55:49 CEST 2023


You are right, sorry.

Another possible solution then: use geom_text instead of geom_point and 
use a triangle shape as text:

ggplot(data = tmpTibPoints,
        aes(x = x, y = y)) +
   geom_polygon(data = tmpTibAreas,
                aes(x = x, y = y, fill = a)) +
   geom_text(data = tmpTibPoints,
              aes(x = x, y = y, label = "▼", color = c),
              size = 6) + guides(color = FALSE)



On 06-10-2023 12:11, Chris Evans via R-help wrote:
> 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:
>>
>> Does adding
>>
>> , show.legend = c("color"=TRUE, "fill"=FALSE)
>>
>> to the geom_point do what you want?
>>
>> Best,
>> Jan
>>
>> On 06-10-2023 11:09, Chris Evans via R-help wrote:
>>> library(tidyverse)
>>> tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) -> 
>>> tmpTibPoints
>>> 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
>>> ggplot(data = tmpTib,
>>>         aes(x = x, y = y)) +
>>>    geom_polygon(data = tmpTibAreas,
>>>                 aes(x = x, y = y, fill = a)) +
>>>    geom_point(data = tmpTibPoints,
>>>               aes(x = x, y = y, fill = c),
>>>               pch = 24,
>>>               size = 6)
>>
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list