Plotting binary variable in R
<p>I have a dataframe where I created a variable (<code>data$adtype</code>) for if the tone of an ad is positive or negative. So, if it is positive, the ad gets a zero, negative then the ad gets a 1. So, I'm doing bar plots of the adtype but I'm subsetting the data by state. So, (<code>data$adtype[data$state=="NY"]</code>) for instance. For states that have both positive and negative ads, it will plot the graph correctly. The issue is, for states that only aired positive ads, it will graph a single bar and it won't even include the other variable on the graph. </p>

<p>When I do the <code>table(data$adtype[data$sate=="NY"])</code>, for instance
it might say:</p>

<pre><code>0       1 
2500    3000
</code></pre>

<p>which means there are 2500 positive ads and 3000 negative ads, and it will graph both bars.</p>

<p>Now, if New Jersey only has positive ads,and no negative ads</p>

<pre><code>table(data$adtype[data$sate=="NJ"])
</code></pre>

<p>I would expect this code to return the following</p>

<pre><code>0     1
500   0
</code></pre>

<p>where there are 500 positive ads and 0 negative ads.</p>

<p>instead, it returns this</p>

<pre><code>0
500
</code></pre>

<p>and it won't even place the other value on the table, nor will it graph it. </p>

<p>I'm trying to produce a graph where it has both binary values, 0 and 1, on the x-axis, and even if one of the values has zero ads in it, it will still include it in the graph.</p>

<p>Hope this makes sense</p>