Note that there are some explanatory texts on larger screens.

plurals
  1. POR plot function - axes for a line chart
    primarykey
    data
    text
    <p>assume the following frequency table in R, which comes out of a survey:</p> <pre><code> 1 2 3 4 5 8 m 5 16 3 16 5 0 f 12 25 3 10 3 1 NA 1 0 0 0 0 0 </code></pre> <p>The rows stand for the gender of the survey respondent (male/female/no answer). The colums represent the answers to a question on a 5 point scale (let's say: 1= agree fully, 2 = agree somewhat, 3 = neither agree nor disagree, 4= disagree somewhat, 5 = disagree fully, 8 = no answer).</p> <p>The data is stored in a dataframe called "slm", the gender variable is called "sex", the other variable is called "tv_serien".</p> <p>My problem is, that I don't find a (in my opinion) proper way to create a line chart, where the x-axis represents the 5-point scale (plus the don't know answers) and the y-axis represents the frequencies for every point on the scale. Furthemore I want to create two lines (one for males, one for females).</p> <p>My solution so far is the following:</p> <ol> <li><p>I create a plot without plotting the "content" and the x-axis:</p> <pre><code>plot(slm$tv_serien, xlim = c(1,6), ylim = c(0,100), type = "n", xaxt = "n") </code></pre></li> </ol> <p>The problem here is that it feels like cheating to specify the <code>xlim=c(1,6)</code>, because the raw scores of <code>slm$tv_serien</code>are 100 values. I tried also to to plot the variable via <code>plot(factor(slm$tv_serien)...)</code>, but then it would still create a metric scale from 1 to 8 (because the dont know answer is 8).</p> <p>So my first question is how to tell R that it should take the six distinct values (1 to 5 and 8) and take that as the x-axis?</p> <ol start="2"> <li><p>I create the new x axis with proper labels:</p> <pre><code>axis(1, 1:6, labels = c("1", "2", "3", "4", "5", "DK")) </code></pre></li> </ol> <p>At least that works pretty well. ;-)</p> <ol start="3"> <li><p>Next I create the line for the males:</p> <pre><code>lines(1:5, table(slm$tv_serien[slm$sex == 1]), col = "blue") </code></pre></li> </ol> <p>The problem here is that there is no DK (=8) answer, so I manually have to specify <code>x = 1:5</code> instead of 1:6 in the "normal" case. My question here is, how to tell R to also draw the line for nonexisting values? For example, what would have happened, if no male had answered with 3, but I want a continuous line?</p> <ol start="4"> <li><p>At last I create the line for females, which works well:</p> <pre><code>lines(1:6, table(slm$tv_serien[slm$sex == 2], col = "red") </code></pre></li> </ol> <p>To summarize:</p> <ul> <li>How can I tell R to take the 6 distinct values of <code>slm$tv_serien</code> as the x axis?</li> <li>How can i draw continuous lines even if the line contains "0"?</li> </ul> <p>Thanks for your help!</p> <p>PS: Attached you find the current plot for the abovementiond functions.</p> <p>PPS: I tried to make a list from "1." to "4." but it seems that every new list element started again with "1.". Sorry.</p> <p><img src="https://i.stack.imgur.com/feHPI.png" alt="line chart"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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