--- title: " Graphical summary of categorical response" author: "Statistics III: Multivariate Data and Regression" date: "2025-10-03" output: "beamer_presentation" --- # Bar plot - Bar plots visualize how a single variable is distributed by providing a count of how many times each value (discrete) of the variable is attained. # Bar plot of counts ```{r bar plot} #install.packages("CatDataAnalysis") #library("CatDataAnalysis") library(ggplot2) bar1<-ggplot(diamonds, aes(x=cut, fill=cut )) + geom_bar( ) + scale_fill_hue(c = 40) +theme(legend.position="none") ``` # ```{r bar1, echo=FALSE} plot(bar1) ``` # Bar graph to show the proportions ```{r bar plot with proportion} bar2<-ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = after_stat(prop), group = 1)) ``` # ```{r echo=FALSE} plot(bar2) ``` # Other alternatives are pie chart, donut chart and treemap. ```{r pie chart} dt<-table(diamonds$cut) data<-as.data.frame(dt) pie<-ggplot(data, aes(x="", y=Freq, fill=Var1)) + geom_bar(stat="identity", width=1, color="white") + coord_polar("y", start=0) + theme_void() ``` # ```{r echo=FALSE} plot(pie) ``` # Grouped bar plot - Plot frequencies for two categorical variables. - You should use grouped bar chart when making comparisons across different levels of a factor. - Use it when you want to look at how the second category variable changes within each level of the first and vice versa. - So this is a situation where we have one categorical predictor and one categorical response. - In the following we have used the type of device as predictor and gender as response. Which gender is using more laptops? - One can look at the other way around depending on the scientific questions. Which device do men prefer? # ```{r grouped bar plot} # https://github.com/rsquaredacademy-education/ # online-courses/tree/master/categorical-data-in-r data <- readRDS('analytics.rds') data[1:3,1:4] bar3<-ggplot(data) + geom_bar(aes(x = device, fill = gender), position = "dodge") + xlab("Device") + ylab("Count") ``` # ```{r echo=FALSE} plot(bar3) ``` # Stacked bar plots - The bars are stacked on top of each other instead of placing them next to each other. Use stacked bar plots while looking at cumulative value. ```{r stacked bar plot} bar4<- ggplot(data) + geom_bar(aes(x = device, fill = gender)) + xlab("Device") + ylab("Count") ``` # ```{r echo=FALSE} plot(bar4) ``` # Stacked proportional bar plot - The height of all bars in this plot are the same. - The distribution of the second categorical variable is scaled to 1. - The length of each bar is determined by its share in the category. - In the example, we see higher proportation of gender data is available for desktop users than for mobile and tablet users. - We lose the information that overall there are very few tablet users. # ```{r stacked proportional bar plot} bar5<- ggplot(data, aes(x = device, fill = gender)) + geom_bar(position = "fill") + labs(y = "Proportion") ``` # ```{r echo=FALSE} plot(bar5) ``` # Tile/Mosaic chart - Useful for multiple predictors. - In the Titanic example there are three predictors(gender, child/adult and class) and one response (saved yes/no). All categorical. This is a regression type data. - In the HairEyeColor example, the role of predictor/response is unclear. This is more of a multivariate data setting (not regression). There are 3 categorical variables: gender, Hair Color and Eye Color. # ```{r tile, fig.show='hide'} if(!require('vcd')) { install.packages('vcd') library('vcd') } data("Titanic") mosaicplot(Titanic,color=TRUE) mosaicplot(HairEyeColor) ``` # ```{r tile plot, echo=FALSE} mosaicplot(Titanic,color=TRUE) ``` # ```{r tile plot2, echo=FALSE} mosaicplot(HairEyeColor) ``` # Parallel coordinates plot. - Multiple quantitative responses. Categorical predictor. - Fisher's Iris data. Predictor is species. Response are Sepal/Petal Length/Width. ```{r parallel coor, fig.show='hide'} library(MASS) ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3]) parc<-parcoord(ir, col = 1 + (0:149)%/%50) ``` # ```{r plot parallel, echo=FALSE} parcoord(ir, col = 1 + (0:149)%/%50) ```