b'Aaron Hamer'http://aaronhamer.com/blog/2019-01-09T07:29:09+00:00b''Human-like generalization in a machine through predicate learning2019-01-09T07:29:09+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/human-like-generalization-in-a-machine-through-predicate-learning/<p>Arxiv: https://arxiv.org/abs/1806.01709</p>
<p>Paper lays out a simulation which performs generalization from one Atari game to another. Atari frames are parsed by an image preprocessor, and the output of that preprocessor is fed to DORA. DORA is hooked into the game via OpenAI Gym, and given access to the score as a value function. DORA surpasses human-like performance in a relatively small number of trials with 1 game, then generalizes to another game. Competing models (e.g., DQNs as in Mnih et al 2015, DNNs as in LeCun et al 2015) need more trials, and then fail to generalize to game 2. Once performance in game 2 is reached, competing models are unable to return to game 1 without a similar retraining phase, whereas DORA can move fluently from game 1 to game 2 or back, showing human-like performance in first shot and exceeding it in later trials.</p>
<p>There are two novel results in this paper:</p>
<p>1) The way that similarity & magnitude computation is handled has only appeared in conference proceedings to date. In short, you rely on the direct mapping of number of active neurons coding for larger magnitudes, activate two laterally-inhibiting units which conjunctively code for the pattern of activation against two things which share at least one magnitude dimension, and learn the settling pattern. This extends the current DORA work by learning connections to patterns of <em>activity</em> rather than patterns of <i>activation</i> - so you code for, like, fast settling with one unit active, fast settling with the other unit active, coactivation. It's a subtle (but possibly confusing) extension. This replaces the clumsier metric array module from LISA, and builds on earlier work in the DORA lab to provide a principled source for magnitude and similarity representations.</p>
<p>2) Using DORA to play video games is novel, and showing human-like performance on a novel (albeit similar) videogame is unprecendented. While other models might not need the image preprocessing DORA depends on, DORA generalizes to entire schemas which were outside the training space.</p>
<p>The paper is dense - the learning algorithm isn't fully specified until supplementary materials, the invariance learned for magitude/similarity has an underlying different process (settling vs. activation for LISAese object/role representations), acronyms and terms are introduced before being defined. It assumes a lot of the reader - even as a DORA lab member, I had to carefully reread several sections to ensure I could follow the argument and explication. I also feel that the labeling of processes for the purposes of explication can confuse people - anything from labeling stuff in figures (e.g., more-x, more-height) to explaining stuff in terms of what DORA does. It seems like making the computational framework more explicit and attributing actions to processes rather than to the framework might make that easier to parse.</p>Back on the wagon2019-01-09T06:51:11+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/back-on-the-wagon/<p>So there's been a content and research gap round these parts because a bunch of stuff happened.</p>
<p>And moving right along...</p>
<p>I decided to put some structure in place to ensure that I make progress and actually finish.</p>
<p>1) Every other week meeting with adviser w/ co-chair as optional</p>
<p>Treat these like standup, albeit with a much lower cadence. What did, blockers, what do next.</p>
<p>2) Keep records of these meetings</p>
<p>Jump through school hoops of activity + easy reference for other stuff</p>
<p>3) Keep a research log</p>
<p>Read a paper? Write a log. Run a sim? Write a log. Crazy idea that just might work? Write a log.</p>
<p>3a) Write recaps of research log, send to standup attendees day before</p>
<p>So we can skip context building in the meeting.</p>
<p>4) Build a project plan with some capacity for failure.</p>
<p>Get Hurewitz et al 2006 performance from images. See if that ports to word problems as input. Try to get cardinals from there.</p>
<p>5) Identify uncertainty in the plan - mitigate it or eliminate it ASAP</p>
<p>Timelines are fuzzy, so focusing on making sure I can get a sim of Hurewitz et al 2006 working with hand-constructed inputs and then work back to image input. Use this to feel out how much I need to give the model - how much heavy lifting does the image preprocessor need to do? Can I rely on the Gallistel pulses stuff as a fallback if the image preprocessor is too hard?</p>
<p>6) Make sure we're jumping through the right process hoops</p>
<p>Write annual progress report, review in ~2 weeks (8w before deadline). This doc should just be a project plan for the rest.</p>
<p>7) Set up a lit summary</p>
<p>Write 1 page reviews of everything I read that's relevant. This goes in research log, but gets tagged for lit review. Paper, basic results.</p>
<p>8) Start operationalizing the plan.</p>
<p>Lay out the hand-labeled stuff above as sim 1. Write it up as such. Work out the sim/behavioral task mapping for the rest of the papers.</p>
<p>9) Figure out how to pay for it.</p>
<p>You gotta deal with the stuff that knocked you off the wagon or it's gonna be a long, burnt out journey.</p>Focus2016-12-20T00:01:51+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/focus/<p>It's been a hard time for focus, lately.</p>
<p>There is so much happening and it's easy to keep looking at the latest, to retweet the stuff that bothers you or that you agree with. It's easy to look at all the terrible things happeneing and question whether what you're doing even matters or to try to find something, anything you can do to help.</p>
<p>Even on a personal level, my life has been tumultuous. I've been in employment limbo for months and just relocated to Scotland to finish my studies. Not surprisingly, I haven't been particularly productive.</p>
<p>I've always struggled with motivation. I get a lot done, but it's mostly the stuff that I want to get done or the things I need to get done. One of the issues with this approach to task management is that the longer I let an unwanted task sit, the harder it is to start. Part of this is just me psyching myself out - the longer you put something off, the more unpleasant it must be, right? Some of this is guilt, because timelines slip and you feel bad for holding up others. Some of this is a really poor motivation technique (When things get bad enough, I tell myself that I don't get to do the things I want to until I do the Ugly Thing and I end up hitting refresh on social media until it's time for bed). Perhaps the bigger problem is that I do almost everything unfocused and distracted. I sort of assume this is true for everyone, but I don't actually know. Many of us assume that our internal states are a model for those of others.</p>
<p>But - I can feel my mind settling. I haven't felt this way in months. And that got me thinking. I've always done my best work when my mind settles. If I'm lucky, this takes a few hours of uninterrupted, unfocused exploration - think web trawling. When I think something is important enough, this happens as a natural consequence of focused, intense effort. I know running gets me there, but I hadn't thought about the problem in these terms until now. This is a huge blind spot for me, likely because I've gotten away with it until now.</p>
<p>Well, not entirely. I'm in Scotland to finish my studies in part because I let ugly necessary tasks sit for too long. And it's really easy to rationalize that truth away - the tasks were pointless hoop jumping with no real outputs to prove that I could do things that I was already doing. I was busy doing other things that did matter which ended up super beneficial for my career, but I couldn't have known that going in.</p>
<p>None of that matters. I should have done the thing.</p>
<p><br><br>Think carefully about the work you avoid. Why are you avoiding it? Should you be doing it?</p>
<p>Be honest with yourself about the answers to those questions, and be ready to make changes if you don't like them.</p>
<p>And next time, start with the thing you don't want to do before it gets bigger.</p>Nintendo didn't attack Zoe Quinn - gamergate did2016-06-24T07:25:47+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/nintendo-didnt-attack-zoe-quinn-gamergate-did/<p>Okay, so - a couple of Paper Mario screenshots from a recent Nintendo event have been floating around. They were carefully juxtaposed in a way that strongly suggests a reference to the vile abuse Zoe Quinn suffered at the hands of gamergate (a controversy where a lying bitter ex managed to channel all the impotent white male rage at a lady and the gaming industry before Donald Trump began gorging himself on their ignorance).</p>
<blockquote class="twitter-tweet">
<p dir="ltr" lang="en">what the fuck did I ever do to you, Nintendo, that y'all had to make my suffering into a fucking joke </p>
<p dir="ltr" lang="en"><img src="https://pbs.twimg.com/media/CllO-B5UgAAOcWx.jpg" height="215" width="375"><img src="https://pbs.twimg.com/media/CllO-BuUoAAtZhm.jpg" height="215" width="375"></p>
</blockquote>
<blockquote class="twitter-tweet">— teënage shirtbag (@UnburntWitch) <a href="https://twitter.com/UnburntWitch/status/745714046511394816">June 22, 2016</a></blockquote>
<p>Did Nintendo attack Zoe Quinn? Probably not. I don't even know that I think Nintendo is in the wrong, here. They could have been more in the right, though. Anyone in the gaming industry should know that the first -gate people around gaming will think of is gamergate, and making light of that is a terrible idea. This is a place where they should have been more vigilant, although I tend to believe that this was unintentional.</p>
<p>The important thing to note here is that Zoe Quinn did not put those screenshots together herself. Gamergaters did <a href="https://www.reddit.com/r/KotakuInAction/comments/4ob3ox/humor_gamergate5_guys_reference_in_the_new_paper/">several days</a> before she saw it, and put it in front of enough people that it got back to her.<br><br>That is - gamergaters took stuff out of context to make it look like Nintendo was making light of Zoe Quinn's experience at the center of the most intense, sustained harassment anyone's ever had to face.</p>
<p>Gamergaters twisted Nintendo's media to convince someone else that Nintendo was attacking them.</p>
<p>If you're upset that anyone would believe Nintendo would attack someone like that, well, you should be more upset that someone would try to use Nintendo to hurt others, <em>at Nintendo's expense</em>. If you can't be bothered to be empathetic towards the person who has dealt with the most sustained harassment anyone's ever faced, at least get enough of the facts so that you defend Nintendo from the right people. That would be the gamergaters that are trying to drag them into the mud they live in, not their targeted victims.</p>Conferencing, candor, and care2014-07-28T03:44:54+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/conferencing-candor-and-care/<p>I'm sitting in my AirBnB in Quebec after a fruitful Cog Sci. Conferences just kill me - even a four day with a single talk leaves me feeling super drained; I slept in until noon and didn't leave for my poutine quota until 8ish. Funnily enough, the more a conference kills me, the more fruitful it was.<br><br>Let's quantify fruitful:</p>
<p>Two collaborators have postdocs lined up after networking</p>
<p>Four new collaborators with 2-4 paper ideas lined up</p>
<p>I'm writing a chapter!</p>
<p>Reading list grew by ~30 papers</p>
<p>I saw a fantastic example of how not to behave at conferences, which I'll get to</p>
<p> </p>
<p>I'm really excited about my friends getting postdocs - in one case, I made an introduction that went REALLY well, and in the other, we spent a few evenings networking and creating space for interesting conversations. Networking isn't just about talking to people, plugging your work, or even making introductions. Sometimes effective networking is about figuring out who should be in the room when, or how to peel off from a group to create space for interesting chats. You have to pay attention to the dynamics of the situation and figure out ways to create opportunities to really connect.</p>
<p>I have a few thoughts on this, but I'd start with paying attention to and exploiting nonverbal cues (e.g., nonverbal cues to invite, exclude, or draw attention).</p>
<p>Note that being excellent at networking doesn't amount to much if you don't do good work. I'm happy I was able to help my friends meet the right people and have the right conversations, but none of that would have meant a damn thing if they weren't already doing great work. The foundation of a successful academic career is good work, and they got these jobs on the merits of their work.</p>
<p>I saw some great examples of people discussing sensitive issues with care this weekend, managing what they said and left unsaid with obvious effort and thought. I also saw people use candor to connect with people, both to gush (I thought your talk was spectacular, it was the best thing I've seen at cog sci, I've never disagreed with David Marr before but after your talk I kind of do.) and to discuss difficulties with a mutual collaborator. That said, there's a delicate walk between care and candor; I saw some miss the mark.</p>
<p>Candor's a powerful tool - it helps us connect over passion, truth, and a little vulnerability. When candid, we strip away a lot of the conventions that protect us from being rude or exercising poor judgment. This builds rapport and trust, but taken too far, candor creates distance and discomfort.<br><br>Two students gave posters that they didn't believe in, and chose to share their discomfort with their work in different ways. One student didn't care for the statistics used to sell the poster, and chose to exercise care in sharing her discomfort. She explained the post-hoc tests (one-tailed T tests in both directions) with candor, explained that they were doing further runs with some tweaks to see if they could find larger effects. She told people that they'd expected the effect to be larger and that she was no longer sure of the research hypothesis. In short, she made it clear that she didn't believe in the work, but she did so tactfully, and illustrated the problems and their attempted solutions.</p>
<p>The other student went on a long screed as soon as I stopped at her poster about how she didn't believe in the core paradigm she was working in. She didn't believe that it could be tied to the topic she was ACTUALLY interested in, and while she could explain it to you she didn't want to. After she ran out of steam, she asked me if I wanted her to run me through it. It seemed clear to me that she didn't want to, so I left.</p>
<p>One of these students shared her skepticism with her current work in a way that drew people in. The other pushed people away.<br><br>Don't present work you're not proud of. If you have to, find and focus on the parts you can be proud of, and be honest about those strengths as well as the parts that aren't quite right. Don't rant about how your entire paradigm is wrong before you even get around to explaining it - people remember. But if you just do good work, you'll never have to worry about this.</p>Rejection and Superpowers2014-05-29T16:07:14+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/rejection-and-superpowers/<p>I was rejected for the NSF GRFP. When I got the notification, I opened the <a target="_blank" href="http://bairdbeer.com/en/beer/tsunen_dtl07.html">nice beer</a> and <a target="_blank" href="http://patric-chocolate.com/">great chocolate</a> I had set aside.</p>
<p>My reaction surprised my supervisors. They expected me to be brittle, incosolable, dead to the world for a few days. I'd pinned a lot of hopes on the funding from the NSF GRFP but it didn't pan out. Thing is, we're going to face rejection all our lives as academics, or any other endeavour where we strive for our growth edges. Let's leave aside the other stuff for a second and focus in on that academic stuff since presumably that's what we're mostly about.</p>
<p>What does a working academic do? We teach. We write. We ask others to judge our writing on its merits, either for publication or funding. We're going to spend our careers submitting our work for others to judge, and not one of us is going to get every manuscript accepted, grant awarded, or position applied for*. Logically, this means that we will be rejected, likely over and over again.</p>
<p>If we focus on outcomes here (e.g., getting accepted to the journal or getting the grant) we're in for a career of disappointment. I know that many of us buy in to the whole sacrifice for the pursuit of knowledge academic ascetic thing (just look at PhD stipends!) but I am not ready to accept a career of disappointment piled upon setbacks.</p>
<p>I prefer to focus on the effort I put into the task rather than the outcome in terms of how I feel about my work. I set aside rewards based on how much effort I put into the task when I complete it. Regardless of outcome, when I find out the decision I enjoy whatever reward I set aside, commensurate with the effort and not the result. In some cases, this means I have <a target="_blank" href="http://freephotooftheday.clientk.com/wp02/wp-content/uploads/2008/01/snickers-fun-size.jpg">a Snickers bar</a> when I get accepted to a graduate program. In other cases, this means I crack <a target="_blank" href="http://www.masterofmalt.com/whiskies/nikka-taketsuru-17-year-old-whiskey/">a really nice bottle of whiskey</a> when I whiff on a grant application.</p>
<p>It turns out that this is about more than preserving my sanity. According to Mueller & Dweck (1998), focusing on ability and outcome rather than effort can undermine children's confidence and future motivation. Admittedly, I am not a child (although I love <a target="_blank" href="http://shop.lego.com/en-US/Teenage-Mutant-Ninja-Turtles-ByTheme">Lego</a> and <a target="_blank" href="http://video.adultswim.com/rick-and-morty/">cartoons</a> and do not come equipped with a driver's license), nor am I hypothesis-blind, but the result is intuitively satisfying. It makes sense that focusing on effort (something which can be increased) rather than intelligence (a relatively fixed quantity) would lead to more resilient responses to failure. I align the content of the reward with effort and the timing of the reward with outcome in an effort to drive home that effort matters more than outcomes.<br><br>This is not to say that one should ignore outcomes entirely and focus entirely on effort. That's a silly stance to take - if I put a lot of effort into doing something poorly, I should evaluate the outcome and change my strategy or focus as a consequence of the outcome. However, I should emphasize doing work I'm proud of rather than doing work that gets me good results because over the long run, this leads to the best outcomes for me. Bribing myself to do good work is just a way of reinforcing those habits and that outlook. Dissociating how you feel about the work you've done from how attached you are to the outcomes of that work is a superpower, because it frees you to do good work and stay motivated based on things within your control (e.g., your effort) rather than things outside your control (e.g., the funding environment, the job market, the acceptance rate or whims of the action editor at your target journal, etc).</p>
<p>This superpower allows you to work hard and stay positive regardless of outcome. It frees you to try and care when outcomes are uncertain. It puts you in control of your destiny because it blunts the power of circumstances to affect your motivation and happiness.</p>
<p>Of course, this focus on effort rather than outcome can lead to unforeseen difficulties (e.g., comparing positive feedback on work you're proud of with work you're not), but the best way to avoid those difficulties is to only show others work that fills you with pride based on the effort that went into it.</p>
<p>* If you are an exception to this, please let the rest of us know your secret!</p>
<p><strong>References:</strong></p>
<p>Mueller, C. M., & Dweck, C. S. (1998). Praise for intelligence can undermine children's motivation and performance. <i>Journal of Personality and Social Psychology</i>, <i>75</i>(1), 33.</p>Sanity check yourself before you nervous wreck yourself2014-04-30T10:58:02+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/sanity-checking-yourself/<p>A ton of academics talk about imposter syndrome - the belief that you're tricking everyone around you into thinking you're way better than you are, as good as they are, and the fear that they'll discover this ruse and cast you out.</p>
<p>I do rather interdisciplinary work and rather technical work in non-technical departments. This often means that I'm bringing expertise to bear that no one else at the table has, references that they haven't seen, or novel experiment designs. I love this aspect of my work - I really think that magical stuff happens when you make analogies that cross functional categories.</p>
<p>Like most good things, the upside of bringing outside expertise has a downside; for me, this manifests as occasional bouts of extra-intense imposter syndrome (or maybe what I feel is the normal take on imposter syndrome; I can't speak for the subjective experiences of others). See, when you're in a room full of linguists and you're talking about relational reasoning or in a room full of cognitive psychologists and you're talking about linguistic developmental trajectories no one there can sanity check you. While it's fun to be the expert, it can make it difficult for those around you to evaluate your competence (backing up your statements with data and references helps!). More problematically, a tiny amount of expertise (e.g., enough to know you're dangerous) can be interpreted as deep expertise by those outside of your discipline.</p>
<p>This tendency for a little knowledge to go a long way seems especially pervasive among graduate students; a lot of us come from a culture where we use knowledge as a proxy for ability or effort. This "outside expertise" effect is compounded if you appear confident and assertive. This sometimes leads us into absurd situations - one of my colleagues identified a really strong effect for a filler item in a survey and the audience assumed that she chose it deliberately and had a theory to explain the effect. To her credit, she announced that she had no idea why the association was as strong as it was. I've had similar experiences where a side interest or seminar syllabus turned me into the "outside expert" in ways that struck me as absurd; if reading a handful of papers an expert makes, what does that mean for the areas where I actually feel like an expert?</p>
<p>I've been struggling with this for weeks, and tonight I found a tool that was particularly effective at cutting through the doubt. I've got a revision due for a pub so I was forced to look at something I've written that I'm proud of. I was expecting a hack job that glossed over stuff I didn't know well and spent too many words on things within my core research interests. What I found was a paper that was much better than I remembered or expected.</p>
<p>Try this: dig up work that you've done a few months ago that you're proud of. Take a look at it. It's better than you think.</p>
<p>So are you!</p>Habits are expectations we set for ourselves2014-04-20T13:24:34+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/habits-are-expectations-we-set-for-ourselves/<p>One of the worst things you can do to your online presence is start a blog and never update it. It'd be better to have a static webpage (although those become increasingly embarrassing as your CV ages) than to have a blog and never update it.</p>
<p>That said, we're all busy and it's hard to see the value that a blog creates. It's easy to understand the value of blogging (in a vague, hand-wavy way) if you're the face of a company, if you're an artist, or an academic who cares about outreach. Here's some perspective that make it clearer for the rest of us:</p>
<p><strong>We are all creators.</strong></p>
<p>Think about your day. What did you make? Did you write something? Did you sort through interesting things online and share some of them? Did you make a sandwich? I bet you're making things all the time without realizing it. What interesting story can you tell about what you created today?</p>
<p><strong>Blogging is the habit of writing.</strong></p>
<p>Many of us wish to be better writers. The best way to improve is to do. Blogging is a great way to toe those waters without a huge commitment. This is important - we all struggle with big commitments (it's hard to forget how important or impactful larger works are). Habits (especially creative ones!) are nothing more than expectations we set for ourselves. Expectations are powerful things. How we set them, how we meet them, and how we handle things when we can't hit them tell people important things about us. Setting small ones for yourself (like blogging) and meeting them (by posting regularly) reinforces a story: I am the sort of person who does the things I say I will.</p>
<p><strong>Be the sort of person who does the things you say you will, when you say you'll do them.</strong></p>
<p>That's all it takes to stand out from the crowd (<a target="_blank" href="http://boss.blogs.nytimes.com/2014/03/11/the-surprisingly-large-cost-of-telling-small-lies/?_php=true&_type=blogs&_r=0">never lie</a>, <a target="_blank" href="http://zenhabits.net/snore/">be concise</a>). If you start a blog, keep it up. If you can't do that, get rid of your blog.</p>Recipe Inspiration Dump2014-04-16T21:43:49+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/recipe-inspiration-dump/<p>Here are a bunch of recipes I love or would love to try!</p>
<p><a target="_blank" href="http://nookandpantry.blogspot.com/2008/04/cream-scones.html">Cream Scones - Nook and Pantry</a></p>
<p><a target="_blank" href="http://smittenkitchen.com/blog/2009/05/graham-crackers/">Graham Crackers - Smitten Kitchen</a></p>
<p><a target="_blank" href="http://www.foodnetwork.com/recipes/alton-brown/cheese-souffle-recipe.html">Cheese Souffle - Alton Brown</a></p>
<p><a target="_blank" href="http://gstratton.blogspot.com/2006/08/flatbread-or-is-it-pita.html">Frybread - Leaves from the Inn of the Last Home</a></p>
<p><a target="_blank" href="http://www.chefsteps.com/activities/english-muffin/?socid=70">English Muffin - ChefSteps</a></p>
<p><a target="_blank" href="http://apt2bbakingco.blogspot.com/2011/12/gingerbread-cookie-clouds.html">Gingerbread Cookie Clouds - Apt. 2B Baking Co.</a></p>
<p><a target="_blank" href="http://apt2bbakingco.blogspot.com/2012/03/chocolate-irish-cream-candies.html">Irish Cream Chocolate Candies - Apt. 2B Baking Co.</a></p>
<p><a target="_blank" href="http://www.dessertsforbreakfast.com/2012/10/salted-dark-chocolate-espresso-cookies.html">Salted Dark Chocolate Espresso Cookies - Desserts for Breakfast</a></p>
<p><a target="_blank" href="http://buttermeupbrooklyn.com/2012/08/tiny-salted-chocolate-sparkle-cookies/">Salted Chocolate Sparkle Cookies - Butter Me Up, Brooklyn!</a></p>
<p><a target="_blank" href="http://www.mcsweeneys.net/articles/thanksgiving-reminiscences-and-recipes-from-the-authors-of-toro-bravo">Aunt Ingrid's Rum Cake - Toro Bravo</a></p>
<p><a target="_blank" href="http://locallemons.com/local_lemons/2009/12/stuffed-crust-pizza.html">Stuffed Crust Pizza - Local Lemons</a></p>
<p><a target="_blank" href="http://bsinthekitchen.com/French-Onion-Soup-Grilled-Cheese/">French Onion Soup Grilled Cheese - BS' in the Kitchen</a></p>
<p><a target="_blank" href="http://www.thekitchykitchen.com/?videos=/cheddar-beer-toast-with-chives-aka-welsh-rarebit/">Welsh Rarebit - Kitchy Kitchen</a></p>
<p><a target="_blank" href="http://iamafoodblog.com/mini-french-onion-mac-and-cheese-recipe/">French Onion Mac & Cheese - I am a food blog</a></p>
<p><a target="_blank" href="http://iamafoodblog.com/momofuku-milk-bar-liquid-cheesecake-recipe/">Momofuku Milk Bar Liquid Cheesecake - I am a food blog</a></p>
<p><a target="_blank" href="http://peegaw.tumblr.com/post/14664424798/making-coconut-tres-leches-doughnuts">Coconut Tres Leches Donuts - Notions and Notations of a Novice Cook</a></p>
<p><a target="_blank" href="http://www.thekitchykitchen.com/?recipes=/chocolate-brown-butter-bread-pudding/">Chocolate Brown Butter Bread Pudding - Kitchy Kitchen</a></p>
<p><a target="_blank" href="http://www.kitchenkonfidence.com/2013/02/cereal-milk-punch">Cereal Milk Punch - Kitchen Konfidence</a></p>
<p><a target="_blank" href="http://www.flourishingfoodie.com/2013/03/how-to-make-perfect-margarita-on-rocks.html">Perfect Margarita - Flourishing Foodie</a></p>
<p><a target="_blank" href="http://thefloursack.blogspot.com/2011/05/garlic-potato-soup.html">Garlic Potato Soup - The Flour Sack</a></p>
<p><a target="_blank" href="http://www.loveandlemons.com/2012/03/21/baked-tomato-cheese-bread-soup">Baked Tomato and Cheese Bread Soup - Love and Lemons</a></p>
<p><a target="_blank" href="http://www.pastryaffair.com/blog/boozy-margarita-lime-cake.html">Boozy Margarita Lime Cake - Pastry Affair</a></p>
<p><a target="_blank" href="http://www.pastryaffair.com/blog/2011/6/10/garlic-parmesan-pull-apart-bread.html">Garlic Parmesan Pull-Apart Bread - Pastry Affair</a></p>
<p><a target="_blank" href="http://www.instructables.com/id/How-to-Make-Your-Own-Cookie-Shot-Glass/?ALLSTEPS">Dominque Ansel's Cookie Shot Glasses - Instructables</a></p>
<p><a target="_blank" href="http://drizzleanddip.com/2013/06/05/roasted-smashed-new-potatoes-with-parmesan">Roasted Smashed New Potatoes with Parmesan - Drizzle and Dip</a></p>
<p><a target="_blank" href="http://www.spoonforkbacon.com/2013/04/beer-battered-fried-olives/">Beer Battered Fried Olives - Spoon Fork Bacon</a></p>
<p><a target="_blank" href="http://www.heathersfrenchpress.com/2013/06/homemade-cocoa-puffs.html">Homemade Cocoa Puffs - French Press</a></p>
<p><a target="_blank" href="http://kalofagas.blogspot.com/2008/02/i-did-it.html">Spanakopita - Kalofagas</a></p>
<p><a target="_blank" href="http://visualrecipes.com/recipe-details/recipe_id/270/Chicken-Paprikash-(Paprikas-Csirke)-with-Spaetzle/">Paprikas Csirke - Visual Recipes</a></p>
<p><a target="_blank" href="http://food52.com/recipes/6441-fresh-sriracha-aka-home-made-rooster">Fresh Sriracha - Food 52</a></p>
<p><a target="_blank" href="http://whatshouldieatforbreakfasttoday.com/post/70076301506/eggs-baked-with-tomatoes-and-champignons">Eggs baked with tomatoes and champignons - What Should I Eat For Breakfast Today?</a></p>Customizing graphs with ggplot22014-04-01T16:10:27+00:00ahamer/blog/author/ahamer/http://aaronhamer.com/blog/customizing-graphs-with-ggplot2/<p>R & ggplot2 go great together, but the resulting graphs often feel like they could use a makeover. Don't worry - we're here to help!</p>
<p>For the purposes of this tutorial, I'm going to assume you've already got R & ggplot2 installed; if that's not the case, you can <a target="_blank" title="R Project Homepage" href="http://cran.r-project.org/">get R here</a> and you can install ggplot2 by running the following command once R is installed and open:</p>
<pre>install.packages('ggplot2')</pre>
<p>Great, once that's all sorted, let's load up ggplot2:</p>
<pre>library(ggplot2)</pre>
<p>Put this <a title="Andy Field's Exam Anxiety data set" href="http://www.sagepub.com/dsur/study/DSUR%20Data%20Files/Chapter%204/Exam%20Anxiety.dat">Exam Anxiety data set</a> (Field, 2013) in your working directory, and run the following command to bring the data in as a dataframe:</p>
<pre>examData = read.delim('Exam Anxiety.dat', header = TRUE)</pre>
<p>Great! Let's put together a simple scatterplot in ggplot2, comparing self-reported anxiety scores to exam scores:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam)) + geom_point()</pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph01.jpg" alt="Graph 1" height="480" width="480"></p>
<p>Let's talk about what's going on in this command before we go further.</p>
<p>The portion before the + tells ggplot what dataframe to use, as well as which columns to use for the axes. You could stop here and have a perfectly valid command, but nothing would get plotted - ggplot2 needs to be told exactly what to do with this data. You can add instructions to ggplot telling it what to do with this data via the + operator. Here, we're telling ggplot that we want it to use points.</p>
<p>This graph isn't super helpful. Let's add a regression line:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam)) + geom_point() + <strong>geom_smooth()</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph02.jpg" alt="Graph 2" height="480" width="480"></p>
<p>See? We add new elements by using the + operator. This simple plotting stuff should be old hat to you by now, so we're going to use gender to group the results, and add some contrasting color & point shapes:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, <strong>group = Gender, color = Gender, shape = Gender</strong>)) + geom_point() + geom_smooth()</pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph03.jpg" alt="Graph 3" height="480" width="480"></p>
<p>We added the group instructions to the base ggplot command for a few reasons; we can add it to individual commands after that point but we'd have to retype it. Since both geom_point and geom_smooth use the data, it makes sense to centralize it!</p>
<p>I find those confidence regions visually distracting; let's clean up the graph by getting rid of them:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(<strong>se=FALSE</strong>)</pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph04.jpg" alt="Graph 4" height="480" width="480"></p>
<p>Here, we told the geom_smooth function (which draws that regression line) to ditch the confidence region with the se=false argument.</p>
<p>Let's make those labels more informative:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + <strong>ylab('Self-reported anxiety rating') + xlab('Exam score')</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph05.jpg" alt="Graph 5" height="480" width="480"></p>
<p>The ylab & xlab let you control the labels for their respective axes. I'm not happy with the scale on the x axis; while I'm fine with quartiles on anxiety ratings I think that exam scores will be easier to analyze if we look at 10 point intervals. Let's make that happen!</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + <strong>scale_x_continuous('Exam score', breaks=seq(0, 100, 10))</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph06.jpg" alt="Graph 6" height="480" width="480"></p>
<p>We had to get rid of the xlab command - it's a shortcut for the labeling done in the scale_x_continuous command (and there's a corresponding discrete version). Let's change those x tick labels to something wordier:</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), <strong>labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')</strong>)</pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph07.jpg" alt="Graph 7"></p>
<p>Huh. Those labels are really cool, but I'd like to make them slanted so they don't overlap and I can read them!</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + <strong>theme(axis.text.x = element_text(angle = 50, hjust = 1))</strong></pre>
<div><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph08.jpg" alt="Graph 8" height="480" width="480"></div>
<div>I forgot a title for this graph! Silly me!</div>
<div>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + theme(axis.text.x = element_text(angle = 50, hjust = 1)) + <strong>labs(title='A whimsical take on the relationship between anxiety and exam score')</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph09.jpg" alt="Graph 9" height="480" width="480"></p>
<p>My, that's ugly. Let's give that title a little breathing room.</p>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + theme(axis.text.x = element_text(angle = 50, hjust = 1)) + labs(title='A whimsical take on the relationship between anxiety and exam score') + <strong>theme(plot.title = element_text(vjust = 1))</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph10.jpg" alt="Graph 10" height="480" width="480"></p>
<p>I want serifs!</p>
</div>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + theme(axis.text.x = element_text(angle = 50, hjust = 1)) + labs(title='A whimsical take on the relationship between anxiety and exam score') + theme(plot.title = element_text(vjust = 1)) + <strong>theme(text = element_text(family = 'serif'))</strong></pre>
<div></div>
<div><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph11.jpg" alt="Graph 11" height="480" width="480"></div>
<div>That's much more whimsical. Let's get rid of the background grey stuff in one fell swoop:</div>
<div></div>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + theme(axis.text.x = element_text(angle = 50, hjust = 1)) + labs(title='A whimsical take on the relationship between anxiety and exam score') + theme(plot.title = element_text(vjust = 1)) + theme(text = element_text(family = 'serif')) + <strong>theme_bw()</strong></pre>
<div></div>
<div><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph12.jpg" alt="Graph 12" height="480" width="480"></div>
<div>Hey! What happened to our label shenanigans? I want my serifs back! I bet that ggplot tries to do everything in order, so later stuff can override earlier stuff:</div>
<pre>ggplot(examData, aes(y = Anxiety, x = Exam, group = Gender, color = Gender, shape = Gender)) + geom_point() + geom_smooth(se=FALSE) + ylab('Self-reported anxiety rating') + scale_x_continuous('Exam score', breaks=seq(0, 100, 10), labels=c('Zero', 'Ten', 'Two tens', 'Ten and two tens', 'Two score', 'L', 'Ten sixes', 'Five fourteens', 'Twenty fours', 'Possession', 'A century')) + labs(title='A whimsical take on the relationship between anxiety and exam score') + theme_bw() + <strong>theme(axis.text.x = element_text(angle = 50, hjust = 1)) + theme(text = element_text(family = 'serif')) + theme(plot.title = element_text(vjust = 1))</strong></pre>
<p><img src="http://aaronhamer.com/static/media/uploads/images/20140401/graph13.jpg" alt="Graph 13" height="480" width="480"></p>
<p>We've messed with a lot of different elements, learned that you can apply formatting anywhere from one object to the entire plot, that there are often a bunch of ways to do the same thing, and also that theming choices are applied in order. Next time, we'll look at box plots, error bars, and pie charts!</p>
<p><strong>References</strong></p>
<p>Field, A. (2013). Exam anxiety [Tab-delimited file]. Retrieved from <a href="http://www.sagepub.com/dsur/study/DSUR%20Data%20Files/Chapter%204/Exam%20Anxiety.dat">http://www.sagepub.com/dsur/study/DSUR%20Data%20Files/Chapter%204/Exam%20Anxiety.dat</a>.</p>