# visualize a horizontal box plot in R

I have a dataset like this. The data has been collected through a questionnaire and I am going to do some exploratory data analysis.

windows <- c("yes", "no","yes","yes","no")
sql     <- c("no","yes","no","no","no")
excel  <- c("yes","yes","yes","no","yes")
salary <- c(100,200,300,400,500 )

test<- as.data.frame (cbind(windows,sql,excel,salary),stringsAsFactors=TRUE)
test[,"salary"] <- as.numeric(as.character(test[,"salary"] ))


I have an outcome variable (salary) in my dataset and a couple of input variables (tools). How can I visualize a horizontal box plot like this:

Let's start by creating some fake dataset.

software = sample(c("Windows","Linux","Mac"), n=100, replace=T)
salary = runif(n=100,min=1,max=100)
test = data.frame(software, salary)


This should create a dataframe test that will look like somewhat like:

    software    salary
1    Windows 96.697217
2      Linux 29.770905
3    Windows 94.249612
4        Mac 71.188701
5      Linux 94.028326
6      Linux  7.482632
7        Mac 98.841689
8        Mac 81.152623
9    Windows 54.073761
10   Windows  1.707829


EDIT based on comment Note that if the data does not already exist in the above format, it can be changed to this format. Let's take a data frame provided in the original question and lets assume the dataframe is called raw_test.

    windows sql excel salary
1     yes  no   yes    100
2      no  yes  yes    200
3     yes  no   yes    300
4     yes  no    no    400
5      no  no   yes    500


Now, using the melt function/ method from the reshape package in R, first create the dataframe test (that will be used for final plotting) as follows:

# use melt to convert from wide to long format
test = melt(raw_test,id.vars=c("salary"))
# subset to only select where value is "yes"
test = subset(test, value == 'yes')
# replace column name from "variable" to "software"
names(test)[2] = "software"


Now, you will get a datframe test that looks like:

  salary software value
1     100  windows   yes
3     300  windows   yes
4     400  windows   yes
7     200      sql   yes
11    100    excel   yes
12    200    excel   yes
13    300    excel   yes
15    500    excel   yes


Having created the dataset. We will now generate the plot.

First, create the bar plot on the left based on the counts of software that represents usage rate.

p1 <- ggplot(test, aes(factor(software))) + geom_bar() + coord_flip()


Next, create the boxplot on the right.

p2 <- ggplot(test, aes(factor(software), salary)) + geom_boxplot() + coord_flip()


Finally, place both these plots next to each other.

require('gridExtra')
grid.arrange(p1,p2,nrow=1)


This should create a plot like:

You are going to need to make a column that contains software info-- for example name it software and the salary column has the corresponding salary so something like

 Software   Salary
Microsoft  100
Microsoft  300
Microsoft  400
SQL        200


and so on...then you can plot with the code below

p <- ggplot(test, aes(factor(software), salary))
p + geom_boxplot() + coord_flip()

• Good but How can I make software column? – Hamideh Jun 11 '15 at 17:11
• That is something you are going to have to research yourself. I am not sure what your data set looks like-- the melt function may be of use to you. – Lauren Goodwin Jun 11 '15 at 17:27
• @LaurenGoodwin My answer to this question uses gridExtra to plot a barplot and boxplot side by side to produce the visualization requested. – Nitesh Jun 11 '15 at 23:52