Changing Perceptions of Marital Childbearing: Analyzing Data with Stacked Bar Graphs
"Do you think people should have children when they get married?"
In the past, most of us would have answered "of course" to this question, but it's a little different now. Married procreation is no longer a given. Area of selectionand we can see this change in the data.
In this guide, we covered how to install and configure Oh My OpenCode in a way that even beginners can follow easily. Data on changing perceptions of childbearing in marriagebased on the Leverage R coding to create Stacked Bar Graphsand explore what this change means. Together, we'll use data to understand how our society is changing and gain the insights we need to prepare for the future.
Survey data on marriage and fertility
Here are two years of response data to the question "If you get married, you should have children" for the population aged 13 and over, provided by the National Statistics Social Survey. The units are %.
| Year | Totally agree | Somewhat Agree | Slightly opposite | Totally opposite |
|---|---|---|---|---|
| 2018 | 25.4 | 44.1 | 21.9 | 8.6 |
| 2020 | 25.5 | 42.6 | 22.1 | 9.8 |
| 2022 | 21.6 | 43.8 | 23.9 | 10.8 |
| 2024 | 23.4 | 44.9 | 22.7 | 9.0 |
Implications of changing perceptions of marital childbearing

When you look at this data, you'll notice a few important trends.
- Reduced percentage of fully agree
- Decrease from 25.41 TP3T in 2018 to 23.41 TP3T in 2024.
- This shows that traditional values about marriage and procreation are gradually eroding.
- Increase in dissenting responses
- The percentage of both slightly opposed and totally opposed is increasing.
- In particular, totally opposed has been on a steady rise, although it has increased slightly from 8.61 TP3T in 2018 to 9.01 TP3T in 2024.
- Maintain some consent
- The slightly agree percentage changed little, from 44.11 TP3T in 2018 to 44.91 TP3T in 2024.
- This shows that many people still feel that they "need" to have children, but they don't feel strongly about it.
Create a Stacked Bar Graph with R Coding
Now let's use the data above to create a stacked bar graph in R! This graph shows at a glance how pronounced the changes are.
# Load required libraries
library(ggplot2)
library(dplyr)
# Create the dataset
data <- data.frame(
Year = c(2018, 2020, 2022, 2024),
Strongly_Agree = c(25.4, 25.5, 21.6, 23.4),
Somewhat_Agree = c(44.1, 42.6, 43.8, 44.9),
Somewhat_Disagree = c(21.9, 22.1, 23.9, 22.7),
Strongly_Disagree = c(8.6, 9.8, 10.8, 9.0)
)
# Transform data to long format
data_long %
tidyr::pivot_longer(
cols = starts_with("Strongly_") | starts_with("Somewhat_"),
names_to = "Response",
values_to = "Percentage"
)
# Set response order
data_long$Response <- factor(
data_long$Response,
levels = c("Strongly_Agree", "Somewhat_Agree", "Somewhat_Disagree", "Strongly_Disagree")
)
# Create stacked bar graph
ggplot(data_long, aes(x = factor(Year), y = Percentage, fill = Response)) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE), color = "black") +
geom_text(aes(label = round(Percentage, 1)),
position = position_stack(vjust = 0.5, reverse = TRUE), size = 3.5, color = "white") +
scale_fill_manual(
values = c(
"Strongly_Agree" = "#74C476",
"Somewhat_Agree" = "#31A354",
"Somewhat_Disagree" = "#FD8D3C",
"Strongly_Disagree" = "#E31A1C"
),
labels = c("Strongly Agree", "Somewhat Agree", "Somewhat Disagree", "Strongly Disagree")
) +
labs(
title = "Changes in Opinions About Having Children After Marriage",
x = "Year",
y = "Percentage (%)",
fill = "Response"
) + Β
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
) +
coord_flip()

Wrapping up
Data can help us understand change and make better decisions. More than just numbers, the shifts in attitudes toward marriage and childbearing are a clear indication of how our society is changing, and it's important for policymakers and individuals alike to understand how we view marriage and childbearing differently than in the past.
Stacked bar graphs are a great way to visually unpack complex data, and with the R code in this article, you can visualize and analyze your own data. You'll be able to understand the nature of your problems and find new solutions.
While marital reproduction is a personal choice, it's clear that it plays an important role in moving toward a sustainable society. Whatever insights you take away from this data, we hope it's a small seed for building a better future.
As a side note, how did the change in attitudes toward childbearing after marriage play out in the total fertility rate? Total Fertility Rate Definition and Calculation: Trendy Visualizations with R Check out the post to learn more.
#Code breakdown
1. load the library
library(ggplot2) library(dplyr)
- ggplot2: A powerful graphics library for data visualization
- DPLYR: Providing efficient tools for data manipulation
2. Create a dataset
data <- data.frame( Year = c(2018, 2020, 2022, 2024), Strongly_Agree = c(25.4, 25.5, 21.6, 23.4), Somewhat_Agree = c(44.1, 42.6, 43.8, 44.9), Somewhat_Disagree = c(21.9, 22.1, 23.9, 22.7), Strongly_Disagree = c(8.6, 9.8, 10.8, 9.0) )
- Create a dataframe with response rates by year
- Each column represents a percentage by year and response type
3. transform your data
data_long % tidyr::pivot_longer( cols = starts_with("Strongly_") | starts_with("Somewhat_"), names_to = "Response", values_to = "Percentage" )
- Convert data to long format using the tidyr::pivot_longer() function
- Convert columns that start with 'Strongly_' or 'Somewhat_' to 'Response' columns
- Move those values to the 'Percentage' column
4. Set the response order
data_long$Response <- factor( data_long$Response, levels = c("Strongly_Agree", "Somewhat_Agree", "Somewhat_Disagree", "Strongly_Disagree") )
- Convert Response column to factor to sort in a specific order
- This determines the order of the bars in the graph
5. Create a graph
ggplot(data_long, aes(x = factor(Year), y = Percentage, fill = Response)) + geom_bar(stat = "identity", position = position_stack(reverse = TRUE), color = "black") + geom_text(aes(label = round(Percentage, 1)), position = position_stack(vjust = 0.5, reverse = TRUE), size = 3.5, color = "white") +
- Create a basic graph object with ggplot()
- Create a bar graph with geom_bar(), stack bars with position_stack()
- Show percentage values inside each bar with geom_text()
6. set colors and labels
scale_fill_manual( values = c( "Strongly_Agree" = "#74C476", "Somewhat_Agree" = "#31A354", "Somewhat_Disagree" = "#FD8D3C", "Strongly_Disagree" = "#E31A1C" ), labels = c("Strongly Agree", "Somewhat Agree", "Somewhat Disagree", "Strongly Disagree") )
- Specifying colors for each response type with scale_fill_manual()
- Setting the label text for the legend with the labels parameter
7. set the graph title and axis labels
labs( title = "Changes in Opinions About Having Children After Marriage", x = "Year", y = "Percentage (%)", fill = "Response" ) +
- Set graph title, x-axis label, y-axis label, and legend title with labs() function
8. Theme and style settings
theme_minimal() + theme( plot.title = element_text(hjust = 0.5, size = 16, face = "bold"), axis.title = element_text(size = 12), axis.text = element_text(size = 10), legend.title = element_text(size = 12), legend.text = element_text(size = 10) )
- Apply the default minimal theme with theme_minimal()
- Use the theme() function to fine-style titles, axis labels, legends, etc.
9. flip the axis
coord_flip()
- Flip the x and y axes with coord_flip() to convert to a horizontal bar graph






