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: enter image description here


2 Answers 2


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.


This should create a plot like:

On the left pane shows the counts of how different software is being used represented through bar plots and on the right pane shows the distribution of salaries grouped by software used represented through box plots.


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()
  • $\begingroup$ Good but How can I make software column? $\endgroup$
    – Hamideh
    Jun 11, 2015 at 17:11
  • $\begingroup$ 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. $\endgroup$ Jun 11, 2015 at 17:27
  • $\begingroup$ @LaurenGoodwin My answer to this question uses gridExtra to plot a barplot and boxplot side by side to produce the visualization requested. $\endgroup$
    – Nitesh
    Jun 11, 2015 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.