Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do it with <code>ggplot2</code>, using <code>facets</code>. When I read <em>"I want to create a plot where the data points are grouped as defined by the factor"</em>, the first thing that came to my mind was <code>facets</code>.</p> <p>But in this particular case, faster alternative should be:</p> <pre><code>plot(as.numeric(trans.factor), casp6) </code></pre> <p>And you can play with plot options afterwards (<code>type</code>, <code>fg</code>, <code>bg</code>...), but I recommend sticking with <code>ggplot2</code>, since it has much cleaner code, great functionality, you can avoid overplotting... etc. etc.</p> <p>Learn how to deal with factors. You got barplot when evaluating <code>plot(trans.factor, casp6)</code> 'cause <code>trans.factor</code> was class of <code>factor</code> (ironically, you even named it in such manor)... and <code>trans.factor</code>, as such, was declared <strong>before</strong> a continuous (numeric) variable within <code>plot()</code> function... hence <code>plot()</code> "feels" the need to subset data and draw boxplot based on each part (if you declare continuous variable first, you'll get an ordinary graph, right?). <code>ggplot2</code>, on the other hand, interprets factor in a different way... as <em>"an ordinary"</em>, numeric variable (this stands for syntax provided by <em>Jonathan Chang</em>, you must specify <code>geom</code> when doing something more complex in <code>ggplot2</code>).</p> <p>But, let's presuppose that you have one continuous variable and a factor, and you want to apply histogram on each part of continuous variable, defined by factor levels. This is where the things become complicated with base graph capabilities.</p> <pre><code># create dummy data &gt; set.seed(23) &gt; x &lt;- rnorm(200, 23, 2.3) &gt; g &lt;- factor(round(runif(200, 1, 4))) </code></pre> <p>By using base graphs (<code>package:graphics</code>):</p> <pre><code>par(mfrow = c(1, 4)) tapply(x, g, hist) </code></pre> <p>ggplot2 way:</p> <pre><code>qplot(x, facets = . ~ g) </code></pre> <p>Try to do this with <code>graphics</code> in one line of code (semicolons and custom functions are considered cheating!):</p> <pre><code>qplot(x, log(x), facets = . ~ g) </code></pre> <p>Let's hope that I haven't bored you to death, but helped you!</p> <p>Kind regards,<br> <em>aL3xa</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload