Skip to content

unexpected attribute plotting with sf polygons

5 messages · Zivan Karaman, Waichler, Scott R

#
Hi, When I plot the "sf" and "data.frame" object my.poly2 below, all of the polygons are the same color, despite my setting two different values for the Attribute.  What have I done wrong?

my.text <-
c("POLYGON ((0 2.69, 4.483057 2.86, 4.483057 2.76, 0 2.59, 0 2.69))",
  "POLYGON ((0 2.59, 4.483057 2.76, 4.483057 2.26, 0 2.09, 0 2.59))",
  "POLYGON ((0 2.09, 4.483057 2.26, 4.483057 1.26, 0 1.09, 0 2.09))",
  "POLYGON ((0 1.09, 4.483057 1.26, 4.483057 -0.74, 0 -0.91, 0 1.09))")
my.poly <- st_as_sfc(my.text)
mat <- matrix(c(rep(1, 4), rep(2, 4)), byrow=F, ncol=2)
my.poly2 <- st_sf(Attribute = mat, geom = my.poly)
plot(my.poly2, breaks = c(0.5, 1.5, 2.5))

Thank you,

Scott Waichler, PhD

Pacific Northwest National Lab
#
You have defined two attributes that are constant across your geometries,
so they are shown in the same color.
The code `mat <- matrix(c(rep(1, 2), rep(2, 2)), byrow=FALSE, ncol=1)` will
define one attribute with two distinct values that will be shown in two
colors.
Hope this helps!

On Fri, Sep 27, 2024 at 4:37?PM Waichler, Scott R via R-sig-Geo <
r-sig-geo at r-project.org> wrote:

            

  
  
#
Thanks for the comments, but it's still not working.  The version below uses vectors instead of a matrix for the attributes, but it's still making two plots all of the same color.  Time1 should be one color, Time2 should be the other.   As should be clear from my attribute names, I am trying to use the lattice-like paneling capability to represent values at different times of the same variable.

my.text <-
c("POLYGON ((0 2.69, 4.483057 2.86, 4.483057 2.76, 0 2.59, 0 2.69))",
  "POLYGON ((0 2.59, 4.483057 2.76, 4.483057 2.26, 0 2.09, 0 2.59))",
  "POLYGON ((0 2.09, 4.483057 2.26, 4.483057 1.26, 0 1.09, 0 2.09))",
  "POLYGON ((0 1.09, 4.483057 1.26, 4.483057 -0.74, 0 -0.91, 0 1.09))")
my.poly <- st_as_sfc(my.text)
my.poly2 <- st_sf(Time1 = rep(1,4), Time2 = rep(2,4), geom = my.poly)
plot(my.poly2, breaks = c(0.5, 1.5, 2.5))

--Scott Waichler
#
When I run your code I get Time1 in one color and Time2 in another
color, as expected.
[image: image.png]

On Fri, Sep 27, 2024 at 8:23?PM Waichler, Scott R <Scott.Waichler at pnnl.gov>
wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20240927/f814caae/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 22014 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20240927/f814caae/attachment.png>
#
Thank you for this.  I think my software may be too old and has caused the problem.  I am running R version 4.2.2 Patched (2023-01-29 r83721) and sf_1.0-9 .  I will get it updated.

Thanks,
Scott