Home > front end >  Creating a frequency polygon overlay in R
Creating a frequency polygon overlay in R

Time:03-25

I'm trying to create a frequency polygon overlay plot in R. I have been able to generate relative frequency tables for responses to a 3-question survey, with responses given as discrete values for 1-5.

How can I make a frequency polygon plot that represents the information below (the percent of respondents per event) for multiple events on the same plot?

Here's the dput for the table:

dput(t1)
structure(c(1.33196721311475, 0.867678958785249, 0.995024875621891, 
0.544069640914037, 1.0498687664042, 0, 0.759219088937093, 0.66334991708126, 
0.217627856365615, 0, 1.63934426229508, 6.83297180043384, 0.995024875621891, 
0.870511425462459, 1.0498687664042, 19.5696721311475, 37.0932754880694, 
26.6998341625207, 20.2393906420022, 20.4724409448819, 77.4590163934426, 
54.4468546637744, 70.6467661691542, 78.1284004352557, 77.4278215223097
), .Dim = c(5L, 5L), .Dimnames = structure(list(c("A", "B", "C", 
"D", "E"), c("1", "2", "3", "4", "5")), .Names = c("", "")))

I am also trying to color code the events, so they are differentiated on the resultant plot.

January Events Relative Frequency Table

This is what the desired frequency polygon plot would look like (in Excel), however Excel is super inefficient for a task like this, so I thought R could do a better job!

CodePudding user response:

This is straightforward using a little data wrangling and ggplot:

library(tidyverse)

 pivot_longer(as.data.frame(cbind(t(t1), n = 1:5)), 1:5) %>%
  ggplot(aes(n, value, colour = name))  
   geom_line()  
   theme_bw() 

Created on 2022-03-23 by the reprex package (v2.0.1)

  • Related