ggplot2中柱状图叠加成功巢数据的实现问题咨询
First off, let's clear up that misconception: integer data types are totally fine for this task—your issue isn't with the data type, it's just about structuring your ggplot code correctly to display stacked values or additional labels. Below are two straightforward approaches to achieve what you're looking for:
Approach 1: Stacked Bar Chart (Total vs. Successful Nests)
This method reshapes your wide-format data into long-format (the standard for stacked bars in ggplot2) using tidyr::pivot_longer:
Step 1: Reshape the Data
# Load required packages library(ggplot2) library(tidyr) # Convert from wide to long format Success_long <- Success %>% pivot_longer( cols = c(n, Success), names_to = "Nest_Type", values_to = "Count" )
Step 2: Build the Stacked Plot
ggplot(Success_long, aes(x = Species, y = Count, fill = Nest_Type)) + # Create stacked bars geom_bar(stat = "identity", position = "stack") + # Custom x-axis labels matching your original plot scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) + # Y-axis breaks consistent with your initial setup scale_y_continuous(breaks = seq(0, 600, by = 50)) + # Custom fill colors (adjust to your preference) scale_fill_manual( values = c("gray80", "dodgerblue"), labels = c("Total Nests", "Successful Nests") ) + # Plot title and axis label ggtitle("Number of nests by species (Total vs. Successful)") + ylab("Number of nests") + # Clean theme and legend placement theme_minimal() + theme(legend.position = "top") + # Add centered labels for each stack segment geom_text(aes(label = Count), position = position_stack(vjust = 0.5))
Approach 2: Annotate Successful Counts on Top of Total Nest Bars
If you prefer to keep your original total nest bars and just overlay successful counts (either as a stacked segment or text label), you can do this without reshaping the data:
Option A: Stacked Successful Segment + Dual Labels
ggplot(Success, aes(x = Species)) + # Base bar for total nests geom_bar(aes(y = n, fill = "Total Nests"), stat = "identity") + # Stacked segment for successful nests geom_bar(aes(y = Success, fill = "Successful Nests"), stat = "identity", position = "stack") + scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) + scale_y_continuous(breaks = seq(0, 600, by = 50)) + scale_fill_manual(values = c("dodgerblue", "gray80")) + ggtitle("Number of nests by species") + ylab("Number of nests") + theme_minimal() + theme(legend.position = "top") + # Label total nests above the bar geom_text(aes(y = n, label = n), vjust = -0.25) + # Label successful nests centered in their segment geom_text(aes(y = Success, label = Success), position = position_stack(vjust = 0.5))
Option B: Text Only for Successful Counts
If you don't want a stacked segment and just want to display successful counts inside the total bars:
ggplot(Success, aes(Species, n, fill = Species)) + geom_bar(stat = "identity") + scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) + scale_y_continuous(breaks = seq(0, 600, by = 50)) + scale_fill_manual(values=c("dodgerblue", "gray", "chartreuse4", "red", "lightgoldenrod"))+ # Replace theme(element_blank()) to keep axes/titles visible theme_minimal() + ggtitle("Number of nests by species")+ ylab("Number of nests")+ theme(legend.position = "none")+ # Total nest count above bar geom_text(aes(label=n), position=position_dodge(width=0.9), vjust=-0.25) + # Successful nest count inside the bar (adjust vjust to reposition) geom_text(aes(label = paste("Success:", Success)), vjust = 1.5, color = "white")
Quick Fix for Your Original Code
Note that your original theme(element_blank()) was removing all theme elements (including axes, titles, etc.), which likely caused visibility issues. Replace it with a targeted theme like theme_minimal() or specify only the elements you want to hide (e.g., theme(panel.grid = element_blank())).
内容的提问来源于stack exchange,提问作者McMahok

