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

R语言直方图绘图代码优化:匹配指定柱状图样式

修改R语言直方图代码匹配目标样式

我有一段可正常运行的R语言直方图绘图代码:

fun_hist <- function(c, E, HS, log_EC_50) {
  df <- data.frame(log_c = c, response = V({{c}}, {{E}}, {{HS}}, {{log_EC50}}))
  ggplot2::ggplot(df, aes( response)) +  geom_histogram(binwidth=0.03)
}

希望修改该直方图,使其样式与下图一致:
目标直方图样式


修改后的代码

fun_hist <- function(c, E, HS, log_EC_50) {
  df <- data.frame(log_c = c, response = V({{c}}, {{E}}, {{HS}}, {{log_EC50}}))
  ggplot2::ggplot(df, aes(response)) +
    # 匹配目标图的白柱黑边样式
    geom_histogram(binwidth = 0.03, fill = "white", color = "black") +
    # 切换为白底主题
    ggplot2::theme_bw() +
    # 调整网格线为浅灰色,贴合目标图的细微网格
    ggplot2::theme(
      panel.grid.major = ggplot2::element_line(color = "#f0f0f0"),
      panel.grid.minor = ggplot2::element_line(color = "#f0f0f0"),
      # 优化坐标轴文字样式
      axis.title = ggplot2::element_text(size = 10, face = "bold"),
      axis.text = ggplot2::element_text(size = 8)
    ) +
    # 添加坐标轴标签(对应目标图的标注)
    ggplot2::xlab("Response") +
    ggplot2::ylab("Count")
}

核心修改点

  • 给柱状图设置fill = "white"(白色填充)和color = "black"(黑色边框),完全匹配目标图的柱状外观
  • 使用theme_bw()替换默认的灰色背景主题,实现白底效果
  • 将网格线调整为浅灰色,和目标图的淡网格线一致
  • 添加清晰的坐标轴标签,并调整文字大小和粗细,贴合目标图的排版风格

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 09:15:47