0

I have data structured like this:

F1           F2         2012   2011 
Dinner      Monday        5      10
Lunch       Tuesday      20      15
Breakfast   Wednesday    4       20

I want to create a cross tab visual graph just like is described in this post:

visualizing crosstab tables with a plot in R

However, I want two columns on the graph so that I can see the year over year difference.

I successfully replicated the code at the above link to create the chart with one year. Is there a way for me to add another year?

Community
  • 1
  • 1
user1609391
  • 445
  • 1
  • 9
  • 24
  • 1
    make your data reproducible and also mention what you have done (the script you did) , you will get more help –  May 21 '16 at 07:37

1 Answers1

0

First, we rearrange your data to long format:

dflong <- df %>% tidyr::gather(key, value, -(F1:F2))

Then, using the ggplot method in the post your mentionned, simply add facet_grid():

ggplot(dflong, aes(F1, F2)) + 
  geom_point(aes(size = value), colour = "green") + 
  theme_bw() + xlab("") + ylab("") +
  scale_size_continuous(range=c(10,30)) + 
  geom_text(aes(label = value)) +
  facet_grid(. ~ key)

Which gives:

enter image description here

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
  • Thank you so much this worked great. Is there an easy way to not have the charts separate. Basically I was looking for one big chart where the two year columns appear right next to each bottom category (breakfast, dinner, lunch). Basically Breakfast would have two lines right next to each other one for 2011 and one for 2012. Then the same happens for dinner and lunch. Thanks! – user1609391 May 23 '16 at 05:23
  • @user1609391 You mean `F1` in `facet_grid()` instead of `key` and `aes(key, F2)` ? – Steven Beaupré May 23 '16 at 10:27