4

I am new to R/ggplot2 and am trying to create a line graph of counts (or percentages, it doesn't matter) for responses to 6 stimuli in ggplot in R. The stimuli should go across the x-axis, and the count on the y-axis. One line will represent the number of participants who responded with a preposition, and the other line will represent the number of participants who responded with a number.

I believe that ggplot with geom_line() requires an x and y (where y is the count or percentage).

Should I create a new data frame with count so that I can use ggplot? And then, a subquestion would be how do I count responses based on the stimulus data (so, how do I count response based on another column in the data frame, or how many preposition responses for stimulus 1, how many number responses for stimulus 1, how many preposition responses for stimulus 2, etc. Maybe with some kind of if statement?)?

or

Is there a way to automatically produce these counts in ggplot?

Of course, it's entirely possible that I'm going about this the wrong way entirely.

I've tried to search this, but cannot find anything. Thank you so much.

Lisa
  • 909
  • 2
  • 13
  • 31
  • Check out the ggplot2's `..count..` variable. – CephBirk Sep 14 '15 at 20:53
  • With a categorical x variable, I would recommend using grouped bars instead of lines, but maybe that's just me. In `ggplot`, this will also have the advantage of automating the counting, as the default behavior for `geom_bar()` is to use `stat="bin"` (which counts the number of cases in each group) for the y-axis. – ulfelder Sep 14 '15 at 21:14
  • You can use `stat = "bin"` in `geom_line` if you want *ggplot2* to count up the number per group for you. Lots of info about aggregating data by group on SO - see [here](http://stackoverflow.com/questions/17421776/how-to-add-count-of-unique-values-by-group-to-r-data-frame?lq=1) and [here](http://stackoverflow.com/questions/25293045/count-number-of-rows-in-a-data-frame-in-r-based-on-group) for a start. – aosmith Sep 14 '15 at 21:49
  • Thank you for the help! This was very useful. I ended up making a frequency table and turning it into a data frame and plotting that with ggplot. – Lisa Sep 15 '15 at 14:24
  • Good job figuring out an approach that worked for you. You can go ahead and write up an answer for how you approached the problem so the question can be marked as solved. – aosmith Sep 15 '15 at 15:11

1 Answers1

1

As I said in my comment, I ended up creating a frequency table and using ggplot to plot the resulting data frame. Here's the code below!

# creates data frame
resp <- c("number", "number", "preposition", "number")
sound <- c(1, 1, 2, 2)
df <- data.frame(resp, sound) 

# creates frequency table
freq.table <- prop.table((xtabs(~resp+sound, data=df)), 2)
freq.table.df <- as.data.frame(freq.table)

# plots lines based on frequency
ggplot(freq.table.df, aes(sound, Freq, group=resp, color=resp)) + 
  geom_line()
Lisa
  • 909
  • 2
  • 13
  • 31