Plot two categorical variables against two numeric variable in ggplot

In my dataset, I have two numeric revenue features, one for each month, and two categorical features one for region and other for value segment. what I want to do is compare these two revenues col by col for each region and facet wrap by value segment. Is there any way to do that in ggplot2?

sample data :

the image in my mind:

Topic ggplot2 r

Category Data Science


Two things before the answer:

  1. Please always produce a sample dataframe with dput()
  2. As this is totally coding based, this might be more suitable for StackOverflow

Proceeding with the answer

#Load the libraries
library(tidyverse)
library(reshape2)

#Create the data frame

JUL_REV <- c(100,2000,3000,340)
AUG_REV <- c(2032,2103,3002,300)
REGION <- c('REGION 1','REGION 1','REGION 2','REGION 2')
VALUE_SEGMENT <- c('VALUE SEGMENT 1','VALUE SEGMENT 2','VALUE SEGMENT 1','VALUE SEGMENT 2' )

df <- data.frame(JUL_REV, AUG_REV, REGION, VALUE_SEGMENT)

#Melt the dataframe

melt(df, id=c('REGION','VALUE_SEGMENT')) -> melt_df

#Plot with ggplot

ggplot()+
  geom_bar(data=melt_df,aes(x=REGION, y=value, fill=variable), stat='identity')+
  facet_grid(~VALUE_SEGMENT)

Output

I put the legends at the top. Let me know if you need any help!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.