1

I am trying to plot two different variables on the Y-axis vs one variable on the X-axis. I am using ggplot geom_bar for the same. However, the results are not coming in the way what I wanted. My data frame looks as below:

      DAY_OF_WEEK CATEGORY_TOTAL OVERALL_TOTAL CAT_PERCENT OVERALL_PERCENT
1          FRIDAY           4893     30542         16              20
2          MONDAY           5198     31197         17              20
3        SATURDAY            133      1139         12               1
4        THURSDAY           4806     29641         16              19
5         TUESDAY           5184     31757         16              21
6       WEDNESDAY           4569     28090         16              18

ggplot(my_data_frame, aes(x=DAY_OF_WEEK,y=CATEGORY_TOTAL,fill=OVERALL_TOTAL)) + 
    geom_bar(stat="identity",position = "dodge")

I need DAY_OF_WEEK on the X-axis, and two bars next to each other for each day. One bar corresponding to CATEGORY_TOTAL and the other one for OVERALL_TOTAL. Similarly I want another plot for the percentages as well. However, with the above ggplot statement, I am only getting one bar i.e. CATEGORY_TOTAL.

Please suggest on how to achieve what I needed.

Thanks

Heroka
  • 12,889
  • 1
  • 28
  • 38
greenhorntechie
  • 386
  • 2
  • 6
  • 13
  • Can you provide us with a reproducible example? – Roman Luštrik Nov 18 '15 at 11:38
  • Generally, ggplot2 works best with data in long format, so one line for each 'point' (or other thing) you want plotted. – Heroka Nov 18 '15 at 11:38
  • Roman, my data frame is as given above and I was two bars next to each other. Can you please let me know what other information is needed. I am a novice in R. So not sure on what is needed here! – greenhorntechie Nov 18 '15 at 11:44
  • [Here are a few tips](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to produce a great example that will attract more (many?) answerers. – Roman Luštrik Nov 18 '15 at 11:49

1 Answers1

0

Here is a start for you; first we melt the data and then we plot.

library(ggplot2)
library(reshape2)

#relevel days of the week as more logical
dat$DAY_OF_WEEK <- factor(dat$DAY_OF_WEEK, levels=c("MONDAY","TUESDAY","WEDNESDAY","THURSDAY",
                                                    "FRIDAY","SATURDAY","SUNDAY"))

#turn to long
m_dat <- melt(dat,id="DAY_OF_WEEK")

#create grouping variable
m_dat$group <- gsub(".+_","",m_dat$variable)

#plot based on selection
p1 <- ggplot(m_dat[m_dat$group=="TOTAL",],
             aes(x=DAY_OF_WEEK,y=value,group=variable,fill=variable)) +
  geom_bar(stat="identity",position="dodge")
p1

enter image description here

Heroka
  • 12,889
  • 1
  • 28
  • 38
  • Heroka, a further query. If I want to plot both CATEGORY_TOTAL and CAT_PERCENT on the same plot, essentially with two different y-axes, what modifications need to be done? Would that be possible in R? – greenhorntechie Nov 18 '15 at 14:12
  • You can use facet_grid. Plots with different y-axis are philosophically hard to make in ggplot, as they are often confusing. – Heroka Nov 18 '15 at 14:13
  • Or you can greate two plots, and plot them together with grid.arrange fromt he gridExtra package. There are a lot of posts on SO about that. – Heroka Nov 18 '15 at 14:21
  • saw some posts about gridExtra and understood that it would put two different plots next to each other. But the problem is the scale on both Y-axes. Scales are completely different i.e one is between 0-100 (percent) and the other anywhere 0-30000 (Freq). I would want to plot these two variables against the same x-axis in single plot to show two different dimensions as two barplots. If ggplot doesnt have a good way of achieving this, is there any other alternative? – greenhorntechie Nov 18 '15 at 14:43
  • I believe its the same with facet_grid as well – greenhorntechie Nov 18 '15 at 14:44
  • What I was trying to say is, don't do it. There are very little advantages to plots with two y-axes. Don't put two y-axes in a single plot. if you're adamant about it, there's also enough SO-posts about that. – Heroka Nov 18 '15 at 14:44
  • Ok Thanks Heroka for your inputs and suggestions. I shall try to put them on seperate plots. – greenhorntechie Nov 18 '15 at 15:17