Site icon GIS Crack

Create a stacked bar graph using ggplot2 in R

R is a programming language and software environment commonly used for statistical analysis and data visualization. One of the most popular packages for creating visualizations in R is ggplot2. This package provides a wide range of tools for creating high-quality and customizable graphics, making it a valuable tool for anyone working with data.

In this blog post, we will discuss how to create a stacked bar graph using ggplot2 in R. Stacked bar graphs are a common way to display data where one variable is broken down into groups defined by another variable. We will go through the steps involved in generating random data for gender, income, and educational level, creating a data.frame, and using ggplot() to create the graph. We will also demonstrate how to customize the graph’s appearance using various functions such as ggtitle() and theme(), and how to save the graph using ggsave(). By the end of this post, you should have a good understanding of how to create and customize stacked bar graphs in R.

  1. Load the ggplot2 package with the library() function.

library(ggplot2)

2. Set the location of the data on the computer with the setwd() function.

setwd("D:/Documents/RUTA")

3. Generate three random variables for gender, income, and educational level. The sample() function is used to generate random samples from the possible values.

4. The factor() function is used to create a categorical variable from the gender variable.

set.seed(1)

Género <- factor(sample(c("Hombre", "Mujer"),
size = 50, replace = TRUE),
levels = c("Hombre", "Mujer"))
Ingresos <- sample(1:4, size = 50, replace = TRUE)
Nivel_educativo <- sample(c("Alto", "Medio", "Bajo"),
size = 50, replace = TRUE)

5. Create a data.frame from the generated variables.

df <- data.frame(x = Género, y = Ingresos, grupo = Nivel_educativo)

6.Create a stacked bar graph using ggplot(), passing the data.frame and specifying the variables to use on the x-axis (Gender), y-axis (Income), and to fill the bars (Group). The geom_bar() function is used to create the bars, and the stat = “identity” specifies that the identity of the data should be shown.

7.Add a title to the graph with ggtitle().

8.Apply the scale_fill_brewer() function to change the color palette used in the legend.

9.Customize the appearance of the graph with the theme() function. In this case, adjust the position of the title and give it an appropriate font size and format.

10.Add a legend with guides(), specifying the title of the legend with guide_legend().

GApi <- ggplot(df, aes(x = Género, y = Ingresos, fill = grupo)) +
geom_bar(stat = "identity") +
ggtitle("Ingresos por género")+
scale_fill_brewer() +
theme(plot.title = element_text(hjust = 0.5, size = 18, face = "bold")) +
guides(fill = guide_legend(title = "Nivel educativo"))

11.Plot the graph.

GApi

12.Finally, save the graph with ggsave(), specifying the file name, the size of the graph, and the units of measurement.

ggsave("grafico_barras_apliladas.png", plot = GApi, width = 8, height = 6, units = "in")

Made with ChatGPT

Exit mobile version