I have a problem with gradient fill of geom_polygon(). I created a triangle, and I need to fill it with three colors. Each color should start in each angle of a triangle, and then they mix in the middle as gradient. I have this code below, but the triangle is fully filled with black. I also need to insert text labels next to each angle.
triangle_data <- data.frame(
x = c(0, 1, 0.5),
y = c(0, 0, sqrt(3)/2),
value = c(0, 0.5, 1)
)
# Calculate the equilateral triangle vertices
side_length <- 1 # Length of each side of the equilateral triangle
height <- side_length * sqrt(3) / 2 # Height of the equilateral triangle
triangle_data$x <- c(0, side_length, side_length/2)
triangle_data$y <- c(0, 0, height)
triangle_plot <- ggplot() +
geom_polygon(data = triangle_data, aes(x = x, y = y, color = value)) +
scale_fill_gradient2(position="bottom" , low = "blue", mid = scales::muted("green"), high = "red") +
theme_void() # Remove axis and background
triangle_plot
There is an example of the result that I want to obtain. The only difference that the triangle should be on the white background + text labels in each angle.
I tried the code that I posted. But the result looks like that.