In the year 2005, Wilkinson created or rather originated the concept of grammar of graphics to describe the deep features which is included between all statistical graphics. The grammar includes simple set of core rules and principles. “Grammar of graphics” is the only sole reason which makes ggplot2 very powerful because the R developer is not limited to set of pre-specified graphics which is used in other packages. This package works under deep grammar called as “Grammar of graphics” which is made up of a set of independent components that can be created in many ways. This library is a phenomenal tool for creating graphics in R but even after many years of near-daily use we still need to refer to our Cheat Sheet. This package is designed to work in a layered fashion, starting with a layer showing the raw data collected during exploratory data analysis with R then adding layers of annotations and statistical summaries.Įven the most experienced R users need help for creating elegant graphics. The plots can be created iteratively and edited later. It provides beautiful, hassle-free plots that take care of minute details like drawing legends and representing them. Ggsave("grafico_torta.Ggplot2 is an R package which is designed especially for data visualization and providing best exploratory data analysis. Finally, use the ggsave function to save the graph. Theme(plot.title = element_text(hjust = 0.5, face = "bold", family = "Times New Roman"))ĥ. Geom_text(aes(label=paste(round(100 * Porcentaje, 2), "%")), position = position_stack (vjust = 0.5)) + Ggtitle("Distribución del PIB por sector") + P <- ggplot(datos, aes(x="", y=Porcentaje, fill=Sector)) +
geom_text(aes(label=paste(round(100 * Porcentaje, 2), “%”)), position = position_stack(vjust = 0.5)): adds a layer of text to the graph with the corresponding percentages for each section of the pie chart, with a vertical justification of 0.5.theme_void(): sets an empty theme for the graph.ggtitle(“Distribución del PIB por sector”), xlab(“”) and ylab(“Porcentaje”): set the title, x-axis label, and y-axis label for the graph, respectively.scale_fill_brewer(palette = “Paired”): sets the fill scale to use the Paired palette from RColorBrewer.coord_polar(“y”, start=0): sets the polar coordinates for the y-axis with a starting point at 0.geom_bar(width = 1, stat = “identity”): adds a layer of bars to the graph with a width of 1 and an “identity” state.The graph is based on the data frame “data” and aesthetic mappings (aes) are set for x, y, and fill. p data datos <- ame(Sector = c(.), Porcentaje = c(.)) creates a data frame called datos with two columns: Sector and Porcentaje.ĭatos <- ame(Sector = c("Suministro de electricidad y agua", "Acuicultura y pesca de camarón", "Alojamiento y servicios de comida", "Pesca", "Transporte", "Comercio"), library(ggplot2) loads the ggplot2 package into the current R workspace.ģ. Installation and loading of the ggplot2 package.Ģ.
The following are the steps to create a pie chart in R: RStudio is a popular software for data visualization and offers several tools to easily create pie charts. Pie charts, also known as circle charts, are a commonly used tool in data analysis to show the distribution of different variables in a data set.