ggforce: barplot with round angle

graphics
ggplot2 extension
Published

September 24, 2024

Modified

November 10, 2024

圆角柱状图

先决条件

Show the code

geom_shape()

连续X

Show the code
shape <- data.frame(
  x = c(0.25, 0.75,0.75,0.25),
  y = c(0, 0,1,1)
)

ggplot(shape, aes(x = x, y = y)) +
  #geom_polygon(fill = 'red') +
  geom_shape(radius = unit(0.5, 'cm'))+
    scale_x_continuous(breaks = seq(0,2,1), limits = c(0,2))

分类X

Show the code
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))

width <- .9

df <- df |>
    mutate(trt = factor(trt)) |>
    dplyr::reframe(
        data.frame(
            x = rep(as.numeric(trt) + width / 2 * c(-1, 1), each = 2), # 指定条形图四个角的坐标
            outcome = c(c(0, outcome), rev(c(0, outcome)))
        ),
        .by = trt
    )
df
   trt    x outcome
1    a 0.55     0.0
2    a 0.55     2.3
3    a 1.45     2.3
4    a 1.45     0.0
5    b 1.55     0.0
6    b 1.55     1.9
7    b 2.45     1.9
8    b 2.45     0.0
9    c 2.55     0.0
10   c 2.55     3.2
11   c 3.45     3.2
12   c 3.45     0.0
Show the code
ggplot(df, aes(x, outcome, fill = trt)) +
    geom_shape(radius = .05)+
    scale_x_continuous(breaks = 1:3,labels = c("a","b","c"))+
    theme(
        axis.title.x = element_blank()
    )

Show the code
library(ggforce)
ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
  geom_point() +
  facet_zoom(x = Species == "versicolor")