0
$\begingroup$

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 :

enter image description here

the image in my mind:

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

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!

$\endgroup$
4
  • 1
    $\begingroup$ This is great, thanks a lot. $\endgroup$
    – Ehsno
    Commented Aug 29, 2021 at 19:18
  • $\begingroup$ Not necessarily this column chart, but what if I have another column {Month}, then what chart is the best to combine all of these dimensions? $\endgroup$
    – Ehsno
    Commented Sep 6, 2021 at 12:04
  • $\begingroup$ @Ehsno so another month? Like, say September? You can do the same! $\endgroup$ Commented Sep 7, 2021 at 5:47
  • $\begingroup$ Sorry, I meant what if there is another feature which is categorical. let's say in our example something like age group.. can we combine all these features in 1 single chart, and not necessarily a column chart. $\endgroup$
    – Ehsno
    Commented Sep 7, 2021 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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