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:
