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

Chris Evans chr|@ho|d @end|ng |rom p@yctc@org
Sat Oct 7 17:18:24 CEST 2023


SO helpful. Thanks to all.  I _think_ the answer to Jeff's question may 
be "It should only be problematical on R earlier than 2.10". At least,
that's how I read this:

There is a portable way to have arbitrary text in character strings 
(only) in your R code, which is to supply them in Unicode as ‘\uxxxx’
escapes (or, rarely needed except for emojis, ‘\Uxxxxxxxx’ escapes). If 
there are any characters not in the current encoding the parser
will encode the character string as UTF-8 and mark it as such. This 
applies also to character strings in datasets: they can be prepared
using ‘\uxxxx’ escapes or encoded in UTF-8 in a UTF-8 locale, or even 
converted to UTF-8 /via/ |iconv()|. If you do this, make sure you have
‘R (>= 2.10)’ (or later) in the ‘Depends’ field of the DESCRIPTION file.

(Quoting from 
https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Encoding-issues. 
Thanks for that pointer Jan.)

### using UTF to solve my issue (for R versions >= 2.10 I think)
library(tidyverse)
tibble(x = 2:9, y = 2:9, c = c(rep("A", 5), rep("B", 3))) %>%
   mutate(y1 = y + 1,
          y2 = y + 2) -> 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

# Unicode characters for black up- and down-pointing characters
pts_shapes <- c("\U25B2", "\U25BC") |> setNames(c("A", "B"))
pts_colors <- c("blue", "red") |> setNames(c("A", "B"))

pts_shapes

ggplot() +
   ### this was the suggestion from Rui Barradas
   geom_point(data = tmpTibPoints,
              aes(x = x, y = y, color = c, shape = c),
              size = 6) +
   ### checking what happens using geom_text (for amusement really)
   geom_text(data = tmpTibPoints,
             aes(x = x, y = y1, label = c)) +
   ### and checking using the UTF characters in annotate() too (ditto)
   annotate(geom = "text", x = 2.5, y = 8.5, label = paste(pts_shapes, 
collapse = "  ")) +
   scale_shape_manual(values = pts_shapes) +
   scale_color_manual(values = pts_colors)

Output attached.  Thanks to Jan and Rui particularly.  R-help providing 
wide and deep R education as ever.


On 06/10/2023 17:05, Jeff Newmiller wrote:
> Doesn't the outcome of this suggestion still depend on which fonts and output device you are using? ... and that is to some degree still system dependent...
>
> On October 6, 2023 7:50:00 AM PDT, Rui Barradas <ruipbarradas using sapo.pt> wrote:
>> Às 10:09 de 06/10/2023, Chris Evans via R-help escreveu:
>>> The reason I am asking is that I would like to mark areas on a plot using geom_polygon() and aes(fill = variable) to fill various polygons forming the background of a plot with different colours. Then I would like to overlay that with points representing direction of change: improved, no reliable change, deteriorated. The obvious symbols to use for those three directions are an upward arrow, a circle or square and a downward pointing arrow.  There is a solid upward point triangle symbol in R (ph = 17) and there are both upward and downward pointing open triangle symbols (pch 21 and 25) but to fill those with a solid colour so they will be visible over the background requires that I use a fill aesthetic and that gets me a mess with the legend as I will have used a different fill mapping to fill the polygons.  This silly reprex shows the issue I think.
>>>
>>> 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)
>>>
>>> Does anyone know a way to create a solid downward pointing symbol?  Or another workaround?
>>>
>>> TIA,
>>>
>>> Chris
>>>
>> Hello,
>>
>> Maybe you can solve the problem with unicode characters.
>> See the two scale_*_manual at the end of the plot.
>>
>>
>>
>> # Unicode characters for black up- and down-pointing characters
>> pts_shapes <- c("\U25B2", "\U25BC") |> setNames(c("A", "B"))
>> pts_colors <- c("blue", "red") |> setNames(c("A", "B"))
>>
>> ggplot(data = tmpTibAreas,
>>        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, color = c, shape = c),
>>              size = 6) +
>>   scale_shape_manual(values = pts_shapes) +
>>   scale_color_manual(values = pts_colors)
>>
>>
>>
>>
-- 
Chris Evans (he/him)
Visiting Professor, UDLA, Quito, Ecuador & Honorary Professor, 
University of Roehampton, London, UK.
Work web site: https://www.psyctc.org/psyctc/
CORE site: http://www.coresystemtrust.org.uk/
Personal site: https://www.psyctc.org/pelerinage2016/
Emeetings (Thursdays): 
https://www.psyctc.org/psyctc/booking-meetings-with-me/
(Beware: French time, generally an hour ahead of UK)
<https://ombook.psyctc.org/book>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ggnewscale_solution2.png
Type: image/png
Size: 12731 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20231007/6bede3de/attachment.png>


More information about the R-help mailing list