Gillian
Gillian

Reputation: 49

how to fill color in specific area with R

I created a pentagram. The code is

p <- cbind(x = c(0, 1, 2,-0.5  , 2.5  ,0), y = c(0, 1, 0,0.6, 0.6,0))
plot(p)
lines(p)

But how can I fill in color like this. I tried to use polygon function but couldn't figure out how to describe the borderline.

Upvotes: 2

Views: 503

Answers (2)

Marco Sandri
Marco Sandri

Reputation: 24262

A solution based on the ggplot2 package:

df <- data.frame(x = c(0, 1, 2,-0.5  , 2.5  ,0), y = c(0, 1, 0,0.6, 0.6,0))

library(ggplot2)
ggplot(data=df, aes(x, y)) +
  geom_polygon(fill="blue") +
  theme_bw()

enter image description here

Upvotes: 1

jay.sf
jay.sf

Reputation: 72899

polygon works with fillOddEven=TRUE.

plot(p)
lines(p)
polygon(p, col="darkblue", fillOddEven=TRUE)

enter image description here


Data:

p <- structure(c(0, 1, 2, -0.5, 2.5, 0, 0, 1, 0, 0.6, 0.6, 0), .Dim = c(6L, 
2L), .Dimnames = list(NULL, c("x", "y")))

Upvotes: 3

Related Questions