You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言中绘制带圆角的条形图?ggchicklet已被CRAN弃用

带圆角条形图的替代方案(ggchicklet弃用后)

以下是几种无需依赖已弃用ggchicklet包的实现方法,均基于ggplot2生态:

方案1:使用ggforce包实现完整圆角条形

ggforce是ggplot2的常用拓展包,通过组合矩形和弧形元素实现圆角效果:

  1. 安装并加载依赖:
install.packages("ggforce")
library(ggforce)
library(ggplot2)
  1. 准备数据并定义参数:
df <- data.frame(category = c("A", "B", "C", "D", "E"), value = c(10, 20, 15, 30, 25))
# 为每个类别分配x轴位置,设置圆角半径
df$x_pos <- seq_along(df$category)
corner_radius <- 0.2
  1. 绘制图形:
ggplot(df, aes(x = x_pos, y = value)) +
  # 绘制条形中间的矩形主体
  geom_rect(
    aes(xmin = x_pos - 0.5 + corner_radius,
        xmax = x_pos + 0.5 - corner_radius,
        ymin = 0, ymax = value),
    fill = "#2c3e50"
  ) +
  # 绘制左侧圆角
  geom_arc_bar(
    aes(x0 = x_pos - 0.5 + corner_radius, y0 = corner_radius,
        r0 = 0, r = corner_radius, start = 0, end = pi/2),
    fill = "#2c3e50"
  ) +
  # 绘制右侧圆角
  geom_arc_bar(
    aes(x0 = x_pos + 0.5 - corner_radius, y0 = corner_radius,
        r0 = 0, r = corner_radius, start = pi/2, end = pi),
    fill = "#2c3e50"
  ) +
  # 调整x轴标签
  scale_x_continuous(breaks = df$x_pos, labels = df$category) +
  labs(x = "类别", y = "数值") +
  theme_minimal()

方案2:自定义ggplot2几何对象(无额外包依赖)

如果不想安装新包,可以直接基于grid包自定义带圆角的柱状图几何元素:

  1. 定义自定义geom函数:
library(ggplot2)
library(grid)

# 继承GeomRect,重写绘图逻辑添加圆角
GeomRoundedCol <- ggproto("GeomRoundedCol", GeomRect,
  draw_panel = function(data, panel_params, coord, radius = 0.1) {
    data <- coord$transform(data, panel_params)
    grobs <- lapply(1:nrow(data), function(i) {
      row <- data[i, , drop = FALSE]
      rectGrob(
        x = row$xmin, y = row$ymin,
        width = row$xmax - row$xmin, height = row$ymax - row$ymin,
        r = unit(radius, "snpc"), default.units = "native",
        gp = gpar(fill = row$fill, col = row$colour, lwd = row$size, lty = row$linetype)
      )
    })
    do.call(gList, grobs)
  }
)

# 封装成可直接调用的函数
geom_rounded_col <- function(mapping = NULL, data = NULL, stat = "identity",
                             position = "stack", radius = 0.1, ...,
                             na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
  layer(
    data = data, mapping = mapping, stat = stat, geom = GeomRoundedCol,
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(radius = radius, na.rm = na.rm, ...)
  )
}
  1. 使用自定义函数绘图:
df <- data.frame(category = c("A", "B", "C", "D", "E"), value = c(10, 20, 15, 30, 25))

ggplot(df, aes(x = category, y = value)) +
  geom_rounded_col(fill = "#3498db", radius = 0.2) +
  labs(x = "类别", y = "数值") +
  theme_bw()

方案3:使用ggtext的富文本元素快速实现

如果仅需要顶部圆角的简化效果,可以利用ggtext的geom_richtext实现:

  1. 安装并加载包:
install.packages("ggtext")
library(ggtext)
library(ggplot2)
  1. 绘图:
df <- data.frame(category = c("A", "B", "C", "D", "E"), value = c(10, 20, 15, 30, 25))

ggplot(df, aes(x = category, y = value)) +
  geom_col(alpha = 0) + # 占位保证坐标轴范围正确
  geom_richtext(
    aes(label = "", y = value/2, x = category),
    fill = "#e74c3c", label.r = unit(0.2, "lines"),
    label.padding = unit(c(0.5, 0.5, 0.5, 0.5), "lines"),
    width = unit(0.8, "npc"), height = unit(value, "native"),
    inherit.aes = FALSE
  ) +
  labs(x = "类别", y = "数值") +
  theme_minimal()

内容的提问来源于stack exchange,提问作者adircinho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 08:07:40