2

I want to create a clustered Bar chart in R using 2 numeric variables, e.g:

Movie Genre (X-axis) and Gross$ + Budget$ should be Y-axis

It's a very straightforward chart to create in Excel. However, in R, I have put Genre in my X-axis and Gross$ in Y-axis.

My question is: Where do I need to put another Numeric variable ie Budget$ in my code so that the new Budget$ will be visible beside Gross$ in the chart?

Here is my Code:

ggplot(data=HW, aes(reorder(x=HW$Genre,-HW$Gross...US, sum), 
y=HW$Gross...US))+
  geom_col()

P.S. In aes I have just put reorder to sort the categories.

Appreciate help!

Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36
Ashutosh D
  • 21
  • 1

2 Answers2

0

Could you give us some data so we can recreate it?

I think you are looking for geom_bar() and one of its options, position="dodge", which tells ggplot to put the bars side by side. But without knowing your data and its structure I can't further help you.

Carles Borredá
  • 358
  • 2
  • 8
0

Melting the dataset should help in this case. A dummy-data based example below:

Data

HW <- data.frame(Genre = letters[sample(1:6, 100, replace = T)],
                 Gross...US = rnorm(100, 1e6, sd=1e5),
                 Budget...US = rnorm(100, 1e5, sd=1e4))

Code

library(tidyverse)
library(reshape2)
HW %>% 
  melt %>% 
  ggplot(aes(Genre, value, fill=variable)) +  geom_col(position = 'dodge')

enter image description here

Vlad C.
  • 944
  • 7
  • 12
  • Appreciate your comments! unfortunately I am getting this message for Tidyverse and Reshape2 packages. "package is not available (for R version 3.5.0)" Is there any other work around? – Ashutosh D Sep 14 '18 at 17:51
  • Further to this, can change target numbers into a Line (like a combo chart in excel) on secondary axis? – Ashutosh D Oct 10 '18 at 07:38