Search code examples
rlinegraph

How can I make a line graph from a prop.table in R?


I would like to create a line graph with three lines, one for each of three groups in column one below (i.e., 1s, 2&p, 3s). I would like my x-axis to be the second column below (i.e., np, loc, adj, V-ing, gonna) and my y-axis to be the relative frequency shown in the third column. But I'm at a loss for how to do this. Any help would be much appreciated.

1s  np     0.83333333 0.16666667
    loc    0.90909091 0.09090909
    adj    0.91666667 0.08333333
    V-ing  0.50000000 0.50000000
    gonna  1.00000000 0.00000000
2&p np     0.78571429 0.21428571
    loc    0.87500000 0.12500000
    adj    0.84615385 0.15384615
    V-ing  0.83333333 0.16666667
    gonna  1.00000000 0.00000000
3s  np     0.76315789 0.23684211
    loc    0.87500000 0.12500000
    adj    0.68888889 0.31111111
    V-ing  0.78571429 0.21428571
    gonna  1.00000000 0.00000000

Solution

  • Are you looking for something like that ? If so, you can achieve it using ggplot2 package and the geom_line function by passing the following aesthetics aes:

    library(ggplot2)
    ggplot(data = df, aes(x = XVar, y = Freq1, group = group))+
      geom_line(aes(color = group))
    

    enter image description here

    Structure of your data (called here df)

       group  XVar     Freq1      freq2
    1:    1s    np 0.8333333 0.16666667
    2:    1s   loc 0.9090909 0.09090909
    3:    1s   adj 0.9166667 0.08333333
    4:    1s V-ing 0.5000000 0.50000000
    5:    1s gonna 1.0000000 0.00000000
    6:   2&p    np 0.7857143 0.21428571
    

    Reproducible data

    structure(list(group = c("1s", "1s", "1s", "1s", "1s", "2&p", 
    "2&p", "2&p", "2&p", "2&p", "3s", "3s", "3s", "3s", "3s"), XVar = c("np", 
    "loc", "adj", "V-ing", "gonna", "np", "loc", "adj", "V-ing", 
    "gonna", "np", "loc", "adj", "V-ing", "gonna"), Freq1 = c(0.83333333, 
    0.90909091, 0.91666667, 0.5, 1, 0.78571429, 0.875, 0.84615385, 
    0.83333333, 1, 0.76315789, 0.875, 0.68888889, 0.78571429, 1), 
        freq2 = c(0.16666667, 0.09090909, 0.08333333, 0.5, 0, 0.21428571, 
        0.125, 0.15384615, 0.16666667, 0, 0.23684211, 0.125, 0.31111111, 
        0.21428571, 0)), row.names = c(NA, -15L), class = c("data.table", 
    "data.frame"), .internal.selfref = <pointer: 0x55f7ff640350>)