Skip to content

Adjustment of geom_area #6153

Closed
Closed
@Breeze-Hu

Description

@Breeze-Hu

ggplot2 has a very elegant way of plotting graphs, especially statistical graphs. However, I have encountered some difficulties in adjusting the area graph.

I want to plot areas of two colors, red for parts greater than 0, and blue for parts less than 0. After trying a few approaches (see code below), it was possible to approximate the desired result in a large dataset, but there were still some problems.
Is there a suggestion or a better way to present this part of the code?

Some some representations of matplotlib would be more concise and predictable, so I hope there is some reference:
[matplotlib](https://matplotlib.org/stable/gallery/lines_bars_and_markers/fill_betweenx_demo.html
https://matplotlib.org/stable/gallery/lines_bars_and_markers/fill_between_demo.html (interpolate parameter))

test = tibble(x=1:5,
              y = c(-1,1,3,-4,2)) |> 
  mutate(y_na = ifelse(y<0, y, 0), y_po = ifelse(y>0, y, 0))

ggplot(test)+
  geom_area(aes(x = x, y = y_po),fill='red')+
  geom_area(aes(x = x, y = y_na),fill='blue')

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions