# how to compare distributions of variables?

I'm a French girl studying R for the first time. I wanted to learn how to compare distributions of two variables.

I have a dataset with results of a survey from a country that has two parties: a social democratic party and a fiscal conservative party. This survey measures party ID, attitudes on social policies and a few other things. List of variables (A to G) and their coding, in the order they appear in the Excel file:

• A. Support for Social Democratic - Fiscal Conservative Party (coded 1 Strong Soc-Dem, 2 Soc-Dem, 3 In the middle, 4 Fisc-Con, 5 Strong Fisc-Con) Attitudes towards social policies are recorded in variables B to E (all coded 1 Strongly Agree, 2 Agree, 3 Neither, 4 Disagree, 5 Strongly Disagree):

• B. Government should tax the rich and help the poor

• C. Government should increase pensions

• D. Government should take care of the homeless

• E. Government should take care of the unemployed

• F. Age (in years)

• G. Number of hours spent listening to music each week (in hours)

Here is a sample data:

    A B C D E  F  G
1   1 2 1 1 1 59 17
2   3 3 3 3 3 40 16
3   2 3 2 2 2 45  7
4   4 3 4 4 4 83  6
5   1 1 1 1 1 46 11


I need to compare the distributions of variable B and D using appropriate measures. Compared to taking care of the homeless, what do respondents believe regarding the way government should tax the rich ?

I tried the following function to compare both B : "Government should tax the rich and help the poor" and D "Government should take care of the homeless" :

boxplot(data$B ~ data$D)


Which gives me, with the true data, something like :

But I don't know neither how to to read it nor what respondents believe regarding the way government should tax the rich. I'm not even sure it was the right plot function to use.

• Welcome to the site. It appears that your question is not so much about R as about statistics. If that is so, I would edit the title because questions about programming are off topic here. – Peter Flom Oct 8 '17 at 12:43

I think the plots proposed by @Bernhard are useful for looking at the association between two ordinal variables. My interpretation of the question was that the poster was more interested in comparing the two distributions.

For this I would use bar plots of counts, which are similar in concept to a histogram for non-continuous variables.

This examples uses the same data as Bernhard. There are a couple of steps of converting the variables to factor variables, and converting the data frame to a long format.

### Adapted from: http://rcompanion.org/handbook/C_04.html

if(!require(lattice)){install.packages("lattice")}

B <- c(sample(1:5, 400, replace = TRUE), rep(2, 80), rep(4, 100))
D <- c(sample(1:5, 400, replace = TRUE), rep(2, 80), rep(5, 100))

Measurement = factor(c(B, D))
Group       = c(rep("B", length(B)), rep("D", length(D)))

Data = data.frame(Group, Measurement)

str(Data)
tail(Data)

library(lattice)

histogram(~ Measurement | Group,
data=Data,
layout=c(1,2)      #  columns and rows of individual plots
)


You have got discrete data with only 5 possible values. Boxplots are not really a good way to express these. Best choice in my opinion would be a mosaic plot, which can be obtained via the mosaic function in package vcd, a sunflowerplot could be handy, if your data is not too big (function sunflowerplot does not need an extra package) or a balloon plot like geom_count from package ggplot2. All of these take the discrete ordinal scale into account.

I'd say: Go for a mosaic plot.

library(vcd)
A B C D E  F  G
1 2 1 1 1 59 17
3 3 3 3 3 40 16
2 3 2 2 2 45  7
4 3 4 4 4 83  6
1 1 1 1 1 46 11")

mosaic(B ~ D, data = data)

sunflowerplot(B~ D, data=data)


This doesn't look like much with the small sample data, but try it with larger data.

EDIT:

An extended and better sample code for 3 alternatives

B <- c(sample(1:5, 400, replace = TRUE), rep(2, 80), rep(4, 100))
D <- c(sample(1:5, 400, replace = TRUE), rep(2, 80), rep(5, 100))
data <- data.frame(B=B, D=D)

library(vcd)
mosaicplot(B ~ D, data=data)

library(ggplot2)
ggplot(aes(x = B, y = D), data=data)+geom_count()

sunflowerplot(B ~ D, data=data)