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

咨询:如何在RStudio默认直方图中按生殖状态(RPRO)着色

How to Color RStudio's Default Base R Histogram by Reproductive Status (RPRO)

Hey there! I totally get what you're going for—you built a great filled histogram with ggplot2, but now you want to switch to the base R histogram (the default one in RStudio) and color it based on your RPRO reproductive status variable. Let's break down how to do this, since base R doesn't have a direct fill argument like ggplot2 does.

Option 1: Overlapping Histograms with Transparency

This mimics the grouped look where each reproductive status's histogram overlays the others, using transparency to keep all groups visible:

# First, ensure RPRO is a factor for clean grouping
manze$RPRO <- as.factor(manze$RPRO)

# Split your DAGE data into groups based on RPRO
dage_groups <- split(manze$DAGE, manze$RPRO)

# Get a consistent x-axis range so all histograms align perfectly
hist_details <- lapply(dage_groups, hist, plot = FALSE)
x_minmax <- range(sapply(hist_details, function(h) h$breaks))

# Pick a color palette (adjust the number of colors to match your RPRO categories)
group_colors <- c(rgb(0.8, 0.2, 0.2, 0.5), rgb(0.2, 0.8, 0.2, 0.5), rgb(0.2, 0.2, 0.8, 0.5))

# Draw the first group's histogram
hist(dage_groups[[1]], 
     breaks = seq(x_minmax[1], x_minmax[2], length.out = 21), # Matches your ggplot2 bins=20
     col = group_colors[1],
     main = "DAGE Distribution by Reproductive Status",
     xlab = "DAGE",
     ylab = "Count")

# Add the remaining groups to the same plot
for (i in 2:length(dage_groups)) {
  hist(dage_groups[[i]],
       breaks = seq(x_minmax[1], x_minmax[2], length.out = 21),
       col = group_colors[i],
       add = TRUE)
}

# Add a legend to identify each reproductive status group
legend("topright", legend = names(dage_groups), fill = group_colors)

Option 2: Stacked Histogram (Like ggplot2's Fill)

If you prefer the stacked look where each bar segment represents a reproductive status, use barplot with binned DAGE data:

# Bin your DAGE variable to match the 20 bins from your original ggplot2 code
manze$DAGE_bin <- cut(manze$DAGE, breaks = 20)

# Create a frequency table of DAGE bins vs. RPRO categories
stack_counts <- table(manze$DAGE_bin, manze$RPRO)

# Draw the stacked barplot (works like a stacked histogram)
barplot(stack_counts,
        col = c("#E69F00", "#56B4E9", "#009E73"), # Custom color palette
        main = "Stacked DAGE Distribution by Reproductive Status",
        xlab = "DAGE Bins",
        ylab = "Count",
        legend.text = colnames(stack_counts), # Labels for RPRO groups
        args.legend = list(x = "topright"))

Bonus: Add Density Curves (Like Your ggplot2 Code)

If you want to include density curves similar to your original geom_density layer, add them to the overlapping histogram with:

# Add density curve for the first group
lines(density(dage_groups[[1]]), col = group_colors[1], lwd = 2)
# Repeat for other groups
lines(density(dage_groups[[2]]), col = group_colors[2], lwd = 2)

Just tweak the color palette and number of groups to match your actual RPRO categories, and you'll have a base R histogram colored exactly how you need it!

内容的提问来源于stack exchange,提问作者Bernat Sales Nogueras

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:13:26