Read line graphs in ggplot2

I am new to R / ggplot2 and trying to create a linear graph of counters (or percent, it doesn't matter) to answer 6 stimuli in ggplot in R. The stimuli must go through the x axis, and the count on the y axis. One line will represent the number of participants who answered with a preposition, and the other line will represent the number of participants who answered with a number.

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

Do I have to create a new data frame with a counter in order to use ggplot? And then podcasting will consist in how I can calculate the answers based on the stimulus data (such as how I count the answer on the basis of another column in the data frame or how many answers in the sentence for stimulus 1, how many number answers for stimulus 1, how many preposition answers for stimulus 2, etc. Maybe with some kind of statement if?)?

or

Is there a way to automatically make these calculations in ggplot?

Of course, it is entirely possible that I will completely change that.

I tried to find it, but I can not find anything. Thank you very much.

+4
source share
1 answer

, ggplot . !

# 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()
+1

Source: https://habr.com/ru/post/1607344/


All Articles