Plot triangle with shaded area

Viewed 1361

I would like to plot a (shaded) triangle with a subarea of the triangle shaded in another color as depicted below

desiredOutput

So far I have only been able to plot either the triangle:

library(ggplot2)
library(data.table)

dt.triangle <- data.table(group = c(1,1,1), polygon.x = c(1,2,1.75), polygon.y = c(1,1,2))
p <- ggplot()
p <- p + geom_polygon(
  data = dt.triangle
  ,aes(
     x=polygon.x
    ,y=polygon.y
    ,group=group
  )
)
p

My problem is mainly adding the area shaded in white (and also having the triangle of a similar shape, that is wider than in my code).

1 Answers

Inspired by the link given by @desc, this is my solution thus far

library(ggplot2)

col_col <- c("#000000",'#000000')
col_fill <- c("#5cb85c","#f9f9f9")
d=data.frame(x=c(1,2,2, 1.5,1.5,2,2), y=c(1,1,2, 1.375,1.25,1.25,1.75), t=c('a', 'a', 'a',  'b', 'b', 'b', 'b'), r=c('x','z','y', 4,5,6,7))
p <- ggplot(data = d, aes(x = x, y = y, col = factor(t), fill = factor(t))) + geom_polygon(data = d, alpha = .75) + # geom_point() +
       scale_color_manual(values = col_col) + scale_fill_manual(values = col_fill) 
p + geom_point(data = d[1:3,]) + geom_text(data = d[1:3,], aes(x=x, y=y, label=r), hjust=0, vjust=1, size=4) +
    theme(axis.title.x = element_blank(),
          axis.text.x  = element_blank(),
          axis.ticks.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text.y  = element_blank(),
          axis.ticks.y = element_blank(),
          legend.position = 'none')

solution

I am still figuring out how to properly draw a non-right rectangle while keeping the points of the second polygon on the edge. Further tips are still appreciated.

Related