Here is an image of my plot so far. At the end of the post I provide the code to reproduce it.
For the time being i use horizontal lines to separate the four groups of lines (defined by variable de in the dataframe). But I would like to use colored rectangles in the background of each group. See the following image to get an idea.
I tried geom_rect and geom_tile with no success. Could anybody help me?
mdfr<-structure(list(name = structure(c(13L, 13L, 13L, 14L, 14L, 14L,
1L, 1L, 1L, 10L, 10L, 10L, 7L, 7L, 7L, 2L, 2L, 2L, 15L, 15L,
15L, 8L, 8L, 8L, 11L, 11L, 11L, 16L, 16L, 16L, 4L, 4L, 4L, 12L,
12L, 12L, 9L, 9L, 9L, 17L, 17L, 17L, 5L, 5L, 5L, 6L, 6L, 6L,
3L, 3L, 3L, 13L, 13L, 13L, 14L, 14L, 14L, 1L, 1L, 1L, 10L, 10L,
10L, 7L, 7L, 7L, 2L, 2L, 2L, 15L, 15L, 15L, 8L, 8L, 8L, 11L,
11L, 11L, 16L, 16L, 16L, 4L, 4L, 4L, 12L, 12L, 12L, 9L, 9L, 9L,
17L, 17L, 17L, 5L, 5L, 5L, 6L, 6L, 6L, 3L, 3L, 3L, 13L, 13L,
14L, 14L, 1L, 1L, 10L, 10L, 7L, 7L, 2L, 2L, 15L, 15L, 8L, 8L,
11L, 11L, 16L, 16L, 4L, 4L, 12L, 12L, 9L, 9L, 17L, 17L, 5L, 5L,
6L, 6L, 3L, 3L), .Label = c("10012/06", "541/13", "700-1/15",
"700/13", "737/13", "751/15", "512/12", "579/13", "715/14", "458/07",
"635/13", "705/13, \n705-1/15", "10004/07", "10005/07", "563/09",
"698/16", "717/14"), class = "factor"), Contr.finish = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Initial", "Current",
"Forecast", "Cost"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("start_date", "end_date"
), class = "factor"), value = c("2007-05-30", "2009-03-30", "2016-06-29",
"2007-09-05", "2010-03-05", "2017-09-30", "2006-09-26", "2008-09-26",
"2015-08-31", "2007-11-20", "2011-11-20", "2014-03-20", "2012-01-31",
"2014-07-31", "2016-03-20", "2013-06-21", "2016-06-21", "2016-06-21",
"2009-04-15", "2011-04-15", "2017-12-31", "2013-06-21", "2016-06-21",
"2016-06-21", "2013-12-18", "2016-08-18", "2017-08-18", "2016-04-14",
"2018-02-14", "2018-02-14", "2013-06-03", "2014-10-03", "2016-05-10",
"2013-08-07", "2015-02-07", "2016-06-30", "2014-09-11", "2016-09-11",
"2016-09-11", "2014-09-26", "2016-09-26", "2016-09-26", "2013-03-20",
"2016-03-20", "2016-03-20", "2015-10-09", "2016-08-09", "2016-08-09",
"2015-11-10", "2016-05-10", "2016-05-10", "2009-03-30", "2016-06-29",
"2016-06-29", "2010-03-05", "2017-09-30", "2017-09-30", "2008-09-26",
"2015-08-31", "2016-08-31", "2011-11-20", "2014-03-20", "2015-12-31",
"2014-07-31", "2016-03-20", "2016-12-20", "2016-06-21", "2016-06-21",
"2016-12-30", "2011-04-15", "2017-12-31", "2017-12-31", "2016-06-21",
"2016-06-21", "2018-03-31", "2016-08-18", "2017-08-18", "2018-02-28",
"2018-02-14", "2018-02-14", "2018-02-14", "2014-10-03", "2016-05-10",
"2016-05-10", "2015-02-07", "2016-06-30", "2016-06-30", "2016-09-11",
"2016-09-11", "2017-07-28", "2016-09-26", "2016-09-26", "2016-09-26",
"2016-03-20", "2016-03-20", "2018-10-19", "2016-08-09", "2016-08-09",
"2016-08-09", "2016-05-10", "2016-05-10", "2016-05-10", "2007-05-30",
"2013-09-24", "2007-09-05", "2010-10-21", "2006-09-26", "2016-08-02",
"2007-11-20", "2015-10-19", "2012-01-31", "2015-11-23", "2013-06-21",
"2015-06-09", "2009-04-15", "2014-05-06", "2013-06-21", "2015-03-28",
"2013-12-18", "2015-05-24", "2016-04-14", "2016-04-14", "2013-06-03",
"2016-01-07", "2013-08-07", "2015-12-08", "2014-09-11", "2015-07-24",
"2014-09-26", "2015-06-18", "2013-03-20", "2017-02-22", "2015-10-09",
"2015-10-09", "2015-11-10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 1L, 1L,
1L, 1L), .Label = c("de1", "de2", "de3", "de4"), class = "factor")), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "110", "410", "710", "103", "131", "161", "191",
"221", "251", "281", "311", "341", "371", "401", "431", "461",
"491", "521", "551", "581", "611", "641", "671", "701", "731",
"761", "791", "821", "851", "881", "911", "941", "971", "1001"
), .Names = c("name", "Contr.finish", "variable", "value", "bar",
"de"), class = "data.frame")
dfr<-structure(list(name = structure(c(2L, 4L, 3L, 1L), .Label = c("10004/07",
"10012/06", "458/07", "512/12"), class = "factor"), text = c("Region 1",
"Region 2", "Region 3", "Region 4"), name0 = c(0, 6.5, 9.5, 12.5
)), .Names = c("name", "text", "name0"), row.names = c(NA, -4L
), class = "data.frame")
library(ggplot2)
library(scales)
library(ggthemes)
ggplot(mdfr, aes(as.POSIXct(as.Date(value, "%Y-%m-%d")), name, colour = Contr.finish)) +
geom_line(aes(size=bar)) +
guides(colour = guide_legend(override.aes = list(size=5)), size="none", fill="none") +
geom_line(size=2.0) +
xlab("") + ylab("") +
theme_stata() +
geom_hline(data=dfr, aes(yintercept = name0), color = "#4d4d4d", size=0.8) + #
scale_fill_brewer(palette="Dark2") +
scale_x_datetime(breaks = date_breaks("1 year"),labels = abbreviate) +
scale_colour_manual(values=c("Initial" = "#67bf5c", "Current" = "#1f77b4",
"Forecast" = "#ff9e4a", "Cost" = "#c10534")) +
theme(legend.position = "bottom",
axis.text.y=element_text(angle=0)
)
You can use geom_rect() and there set xmin= and xmax= to minimal and maximal values of your dates or some other values outside the limits. For the ymin= and ymax= used name values converted to numeric (they have to factors in your dataframe) and then -0.5 and +0.5 (as for each discrete value there is place of 1 around it). Added expand=c(0,0) to scale_x_datetime() to remove white areas.
+ geom_rect(aes(xmin=min(as.POSIXct(as.Date(value, "%Y-%m-%d"))),
xmax=max(as.POSIXct(as.Date(value, "%Y-%m-%d"))),
ymin=as.numeric(name)-0.5,ymax=as.numeric(name)+0.5,
fill=de),alpha=0.05,linetype=0)
Related
I am using the rms library and the lrm function to do a penalized logistic regression.
Just look to my data:
> dput(cs_data_train[1:50,])
structure(list(DataCRMSanoflore.Year_Sales = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
2L), .Label = c("2015", "2016", "2017"), class = "factor"), DataCRMSanoflore.HOURS_INSCR = c(14L,
18L, 17L, 16L, 11L, 22L, 23L, 17L, 9L, 21L, 18L, 19L, 12L, 11L,
17L, 16L, 21L, 20L, 14L, 19L, 22L, 17L, 22L, 13L, 19L, 13L, 21L,
16L, 23L, 19L, 11L, 21L, 11L, 22L, 20L, 13L, 11L, 17L, 15L, 12L,
15L, 21L, 17L, 14L, 10L, 17L, 10L, 12L, 18L, 13L), DataCRMSanoflore.Month_Sales = structure(c(9L,
2L, 5L, 9L, 4L, 7L, 3L, 9L, 7L, 12L, 3L, 3L, 12L, 3L, 3L, 6L,
3L, 4L, 5L, 8L, 8L, 1L, 4L, 10L, 9L, 5L, 4L, 9L, 2L, 12L, 9L,
4L, 4L, 3L, 6L, 8L, 6L, 4L, 12L, 5L, 6L, 9L, 7L, 9L, 1L, 9L,
7L, 11L, 11L, 4L), .Label = c("01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12"), class = "factor"), DataCRMSanoflore.Date_Sales = structure(c(3L,
10L, 22L, 23L, 26L, 13L, 12L, 2L, 25L, 11L, 10L, 9L, 4L, 10L,
18L, 9L, 9L, 1L, 14L, 24L, 4L, 2L, 2L, 22L, 17L, 4L, 14L, 22L,
2L, 5L, 29L, 13L, 2L, 10L, 25L, 5L, 10L, 1L, 6L, 20L, 7L, 9L,
1L, 3L, 17L, 22L, 3L, 9L, 20L, 13L), .Label = c("01", "02", "03",
"04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31"), class = "factor"), DataCRMSanoflore.HOURS_INSCR.1 = c(14L,
18L, 17L, 16L, 11L, 22L, 23L, 17L, 9L, 21L, 18L, 19L, 12L, 11L,
17L, 16L, 21L, 20L, 14L, 19L, 22L, 17L, 22L, 13L, 19L, 13L, 21L,
16L, 23L, 19L, 11L, 21L, 11L, 22L, 20L, 13L, 11L, 17L, 15L, 12L,
15L, 21L, 17L, 14L, 10L, 17L, 10L, 12L, 18L, 13L), DataCRMSanoflore.Year_Creation_Sales = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
2L), .Label = c("2015", "2016", "2017"), class = "factor"), DataCRMSanoflore.Month_Creation_Sales = structure(c(9L,
2L, 10L, 10L, 9L, 7L, 12L, 9L, 7L, 12L, 3L, 4L, 2L, 6L, 3L, 6L,
10L, 4L, 5L, 8L, 3L, 1L, 4L, 11L, 9L, 5L, 4L, 9L, 2L, 12L, 10L,
4L, 4L, 3L, 10L, 8L, 6L, 4L, 12L, 8L, 6L, 2L, 10L, 5L, 1L, 9L,
8L, 11L, 11L, 4L), .Label = c("01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12"), class = "factor"), DataCRMSanoflore.Day_Creation_Sales = structure(c(11L,
15L, 2L, 31L, 26L, 23L, 5L, 2L, 25L, 16L, 10L, 13L, 7L, 3L, 18L,
9L, 8L, 27L, 18L, 24L, 6L, 2L, 4L, 16L, 17L, 12L, 15L, 22L, 10L,
5L, 1L, 14L, 2L, 10L, 5L, 5L, 10L, 25L, 6L, 5L, 28L, 8L, 10L,
18L, 17L, 22L, 31L, 9L, 21L, 22L), .Label = c("01", "02", "03",
"04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31"), class = "factor"), DataCRMSanoflore.Year_Validation_Sales = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
2L), .Label = c("2015", "2016", "2017"), class = "factor"), DataCRMSanoflore.Month_Validation_Sales = structure(c(9L,
2L, 10L, 11L, 10L, 7L, 12L, 9L, 7L, 12L, 3L, 4L, 2L, 6L, 3L,
6L, 10L, 4L, 5L, 8L, 3L, 1L, 4L, 11L, 9L, 5L, 4L, 9L, 2L, 12L,
10L, 4L, 4L, 3L, 10L, 8L, 6L, 4L, 12L, 8L, 6L, 2L, 10L, 5L, 1L,
9L, 9L, 11L, 11L, 4L), .Label = c("01", "02", "03", "04", "05",
"06", "07", "08", "09", "10", "11", "12"), class = "factor"),
DataCRMSanoflore.Day_Validation_Sales = structure(c(14L,
16L, 3L, 3L, 1L, 27L, 6L, 5L, 27L, 21L, 19L, 27L, 8L, 5L,
21L, 10L, 9L, 30L, 26L, 27L, 7L, 4L, 15L, 17L, 18L, 13L,
20L, 29L, 11L, 7L, 2L, 16L, 3L, 20L, 6L, 6L, 13L, 29L, 8L,
6L, 30L, 9L, 12L, 20L, 18L, 29L, 1L, 10L, 23L, 25L), .Label = c("01",
"02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
), class = "factor"), DataCRMSanoflore.AGE_CUSTUMER = c(37L,
23L, 34L, 32L, 45L, 52L, 44L, 55L, 37L, 29L, 33L, 29L, 30L,
37L, 56L, 48L, 44L, 42L, 45L, 33L, 37L, 53L, 55L, 60L, 57L,
33L, 51L, 32L, 35L, 54L, 41L, 47L, 59L, 33L, 45L, 35L, 36L,
28L, 42L, 24L, 32L, 39L, 33L, 36L, 49L, 56L, 45L, 39L, 54L,
55L), DataCRMSanoflore.MEAN_PURCHASE = c(71.75, 50.7142857142857,
18.6666666666667, 0, 0, 54.7, 0.666666666666667, 38, 6.5,
0, 83.3333333333333, 44.3333333333333, 25.7777777777778,
24.1818181818182, 23.3846153846154, 35.5294117647059, 21.6363636363636,
1.125, 6, 8.66666666666667, 18.4, 16.9285714285714, 0, 0,
36.5, 21.5, 18.5714285714286, 28.125, 101.333333333333, 0,
2, 0, 20.9166666666667, 69.1428571428571, 16.6666666666667,
1.5, 87.1666666666667, 48.25, 13.3333333333333, 20.5833333333333,
12, 0, 23, 15.1428571428571, 0, 30.4375, 30.3076923076923,
24.625, 23.4285714285714, 20.0833333333333), DataCRMSanoflore.NUMBER_GIFTS = c(1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 4L, 3L,
4L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 1L,
1L, 2L, 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
2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("0", "1"), class = "factor")), .Names = c("DataCRMSanoflore.Year_Sales",
"DataCRMSanoflore.HOURS_INSCR", "DataCRMSanoflore.Month_Sales",
"DataCRMSanoflore.Date_Sales", "DataCRMSanoflore.HOURS_INSCR.1",
"DataCRMSanoflore.Year_Creation_Sales", "DataCRMSanoflore.Month_Creation_Sales",
"DataCRMSanoflore.Day_Creation_Sales", "DataCRMSanoflore.Year_Validation_Sales",
"DataCRMSanoflore.Month_Validation_Sales", "DataCRMSanoflore.Day_Validation_Sales",
"DataCRMSanoflore.AGE_CUSTUMER", "DataCRMSanoflore.MEAN_PURCHASE",
"DataCRMSanoflore.NUMBER_GIFTS", "SENSIBILITE", "IMPERFECTIONS",
"BRILLANCE", "GRAIN_PEAU", "RIDES_VISAGE", "ALLERGIES", "MAINS",
"PEAU_CORPS", "INTERET_ALIM_NATURELLE", "INTERET_ORIGINE_GEO",
"INTERET_VACANCES", "INTERET_ENVIRONNEMENT", "INTERET_COMPOSITION",
"DataCRMSanoflore.Nb_achats", "OUTCOME"), row.names = c(22L,
33L, 40L, 48L, 54L, 59L, 74L, 78L, 87L, 89L, 104L, 115L, 121L,
141L, 159L, 161L, 163L, 165L, 196L, 202L, 211L, 222L, 272L, 300L,
318L, 325L, 327L, 349L, 374L, 380L, 392L, 393L, 394L, 398L, 427L,
440L, 449L, 456L, 470L, 477L, 479L, 490L, 505L, 508L, 514L, 520L,
528L, 531L, 534L, 543L), class = "data.frame")
Then when I want to fit the model using this code:
fit = lrm(OUTCOME ~ .-1,data = cs_data_train,x=T, y=T)
It gives an error:
singular information matrix in lrm.fit (rank= 148 ). Offending
variable(s): DataCRMSanoflore.HOURS_INSCR.1 Error in lrm(OUTCOME ~ .
- 1, data = cs_data_train, x = T, y = T) : Unable to fit model using “lrm.fit”
I searched but I could not resolve this issue. Thank you for your help!
EDIT:
As Said in the comment below. I need to remove one of each both correlated variables. So I write this code :
> highlyCorrelated <- findCorrelation(correlationMatrix, cutoff=(0.7),verbose = FALSE)
> print(highlyCorrelated)
[1] 21 20 26 15 18 17 22 16 25 19 23 24 6 9 7 10 28 2
> important_var=colnames(DATA_BASE[,-highlyCorrelated])
> important_var
[1] "DataCRMSanoflore.Year_Sales" "DataCRMSanoflore.Date_Sales" "DataCRMSanoflore.HOURS_INSCR.1"
[4] "DataCRMSanoflore.Day_Creation_Sales" "DataCRMSanoflore.MEAN_PURCHASE" "OUTCOME"
> DATA_BASE<-DATA_BASE[,-highlyCorrelated]
> str(DATA_BASE)
'data.frame': 5775 obs. of 6 variables:
$ DataCRMSanoflore.Year_Sales : num 2 1 2 1 2 1 1 1 1 2 ...
$ DataCRMSanoflore.Date_Sales : num 13 3 10 22 23 26 13 1 12 2 ...
$ DataCRMSanoflore.HOURS_INSCR.1 : num 17 14 18 17 16 11 22 14 23 17 ...
$ DataCRMSanoflore.Day_Creation_Sales: num 13 11 15 2 31 26 23 1 5 2 ...
$ DataCRMSanoflore.MEAN_PURCHASE : num 0 71.8 50.7 18.7 0 ...
$ OUTCOME : Factor w/ 2 levels "0","1": 1 1 2 1 1 1 2 2 1 1 ...
But I get then the same error
Error in lrm(OUTCOME ~ . - 1, data = train, x = T, y = T) : Unable
to fit model using “lrm.fit”
This really weird!
How can I resolve this please ?
I am trying to figure out a way of introducing negative values of factors in a stacked barplot in ggplot2. The data is level of support for basic income among Finnish MPs. It is at the bottom of the post.
I can get a plot that is like the one I want (minus the negatively valued factors) with the following code:
library(forcats)
library(ggplot2)
support.plot <- ggplot(mpsupport.df, aes(fct_infreq(Party))) +
geom_bar (aes(fill=Support)) +
coord_flip() +
theme(legend.position = "bottom")+
ylab("Party") +
xlab("Number of MPs")
This gives the following:
What I would like is for the graph to be centred on the green-turquoise border, so that support for basic income was to the right, while opposition was to the left. Does this make sense?
Data:
> dput(mpsupport.df)
structure(list(Party = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 5L, 6L, 7L, 7L, 7L), .Label = c("National Coalition",
"Centre Party", "Social Democratic Party", "Left Alliance", "Christian Democrats",
"True Finns", "Swedish People's Party", "Greens"), class = "factor"),
Support = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L), .Label = c("fully.agree", "partially.agree",
"partially.disagree", "fully.disagree"), class = "factor")), .Names = c("Party",
"Support"), row.names = c("1", "2", "2.1", "2.2", "2.3", "2.4",
"4", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "4.7", "6", "8",
"8.1", "8.2", "8.3", "8.4", "8.5", "8.6", "8.7", "8.8", "8.9",
"8.10", "8.11", "8.12", "8.13", "8.14", "9", "9.1", "9.2", "9.3",
"9.4", "9.5", "9.6", "9.7", "10", "10.1", "10.2", "10.3", "10.4",
"10.5", "10.6", "10.7", "10.8", "10.9", "10.10", "10.11", "10.12",
"10.13", "10.14", "10.15", "10.16", "10.17", "10.18", "10.19",
"10.20", "10.21", "10.22", "10.23", "10.24", "10.25", "10.26",
"10.27", "10.28", "10.29", "10.30", "10.31", "10.32", "10.33",
"11", "11.1", "11.2", "11.3", "12", "12.1", "12.2", "12.3", "13",
"14", "14.1", "14.2", "14.3", "14.4", "14.5", "14.6", "14.7",
"14.8", "14.9", "14.10", "14.11", "14.12", "14.13", "14.14",
"14.15", "14.16", "14.17", "14.18", "14.19", "14.20", "15", "15.1",
"17", "17.1", "17.2", "17.3", "17.4", "17.5", "17.6", "17.7",
"17.8", "17.9", "17.10", "17.11", "17.12", "17.13", "17.14",
"17.15", "17.16", "17.17", "17.18", "17.19", "18", "18.1", "18.2",
"18.3", "18.4", "18.5", "18.6", "18.7", "19", "19.1", "19.2",
"19.3", "19.4", "19.5", "19.6", "19.7", "19.8", "19.9", "19.10",
"19.11", "19.12", "19.13", "19.14", "19.15", "19.16", "19.17",
"19.18", "19.19", "19.20", "19.21", "19.22", "19.23", "21", "21.1",
"22", "22.1", "22.2", "22.3", "22.4", "22.5", "22.6", "22.7",
"22.8", "22.9", "22.10", "22.11", "22.12", "23", "23.1", "23.2",
"23.3", "25", "25.1", "25.2", "25.3", "25.4", "25.5", "25.6",
"27", "27.1", "27.2", "27.3", "27.4", "27.5", "29", "30", "31",
"31.1", "31.2"), class = "data.frame")
Try something along these lines:
library(ggplot)
library(forcats)
mpsupport.df$dummy = ifelse(mpsupport.df$Support %in% c("fully.agree", "partially.agree"), 1, -1)
agg = aggregate(dummy ~ Support + Party, data = mpsupport.df, FUN = sum)
ggplot(data = agg)+
geom_bar (aes(y = dummy, x= fct_infreq(Party), fill = factor(Support, levels = c("fully.agree", "partially.agree", "fully.disagree" ,"partially.disagree"))), stat= "identity") +
coord_flip()+
theme(legend.position = "bottom", legend.title = element_blank())
I'm trying to create a facet wrapped ggplot boxplot with dataframe dataw and I'm trying to modify the labels of each subplot.
dataw <- structure(list(base = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 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 6L, 6L, 6L, 6L, 6L, 6L), values = c(13, 22, 16, 21, 52, 1,
1.709, 2.121, 2.061, 2.233, 3.388, 1, 5, 6, 6, 2, 1, 0.856, 1.116,
1.207, 1.175, 0.95, 76, 45, 5, 1, 1, 15, 8.558, 5.44, 1.147,
0.857, 0.831, 10, 7, 40, 4, 10, 5, 1.547, 1.174, 4.777, 1.071,
1.356, 7, 0, 1, 6, 1, 8, 1.322, 0.728, 0.83, 1.178, 0.831, 4,
2, 0, 1, 3, 0, 1.098, 0.96, 0.63, 0.888, 1.013, 13, 22, 16, 21,
52, 1, 1.709, 2.121, 2.061, 2.233, 3.388, 3, 6, 7, 2, 9, 11,
0.952, 1.474, 1.45, 0.967, 1.306, 13, 22, 16, 21, 52, 1, 1.709,
2.121, 2.061, 2.233, 3.388, 3, 8, 15, 0, 5, 2, 1.014, 1.583,
2.289, 0.773, 1.135, 10, 3, 8, 1, 4, 2, 1.504, 1.03, 1.244, 0.884,
1.047, 4, 1, 0, 2, 5, 1, 1.066, 0.862, 0.689, 0.963, 1.125, 2,
0, 0, 2, 0, 1, 0.919, 0.723, 0.479, 0.922, 0.721, 7, 8, 0, 8,
7, 0, 1.299, 1.236, 0.779, 1.298, 1.224, 13, 22, 16, 21, 52,
1, 1.709, 2.121, 2.061, 2.233, 3.388, 45, 38, 41, 13, 34, 1,
2.817, 2.264, 2.398, 1.374, 3.848, 3, 0, 1, 1, 2, 14, 0.973,
0.641, 0.846, 0.866, 0.909, 13, 22, 16, 21, 52, 1, 1.709, 2.121,
2.061, 2.233, 3.388, 7, 0, 0, 1, 2, 1, 1.37, 0.436, 0.706, 0.685,
0.902, 0, 5, 5, 0, 7, 1, 0.597, 1.113, 1.079, 0.71, 1.222, 3,
1, 4, 0, 23, 8, 0.992, 0.84, 1.07, 0.762, 2.399, 17, 7, 18, 6,
10, 1, 2.4, 1.315, 1.948, 1.135, 1.306, 21, 8, 50, 4, 6, 12,
2.412, 1.254, 3.857, 1.075, 1.168, 13, 22, 16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, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), .Label = c("D<U+2192>", "G<U+2192>", "A<U+2192>", "K<U+2192>",
"C<U+2192>", "T<U+2192>"), class = "factor")), .Names = c("base",
"pos", "values", "type", "labels"), row.names = c("1", "2", "3",
"4", "5", "3942", "3943", "3944", "3945", "3946", "3947", "11",
"21", "31", "41", "51", "63", "64", "65", "66", "67", "68", "12",
"22", "32", "42", "52", "2953", "2954", "2955", "2956", "2957",
"2958", "13", "23", "33", "43", "53", "2461", "2462", "2463",
"2464", "2465", "2466", "14", "24", "34", "44", "54", "7493",
"7494", "7495", "7496", "7497", "7498", "111", "214", "311",
"411", "511", "4874", "4875", "4876", "4877", "4878", "4879",
"121", "221", "321", "421", "521", "9356", "9357", "9358", "9359",
"9360", "9361", "131", "231", "331", "431", "531", "9221", "9222",
"9223", "9224", "9225", "9226", "15", "25", "35", "45", "55",
"93561", "93571", "93581", "93591", "93601", "93611", "112",
"215", "312", "412", "512", "1579", "1580", "1581", "1582", "1583",
"1584", "122", "222", "322", "422", "522", "1782", "1783", "1784",
"1785", "1786", "1787", "132", "232", "332", "432", "532", "3398",
"3399", "3400", "3401", "3402", "3403", "16", "26", "36", "46",
"56", "2257", "2258", "2259", "2260", "2261", "2262", "113",
"216", "313", "413", "513", "1027", "1028", "1029", "1030", "1031",
"1032", "123", "223", "323", "423", "523", "8654", "8655", "8656",
"8657", "8658", "8659", "133", "233", "333", "433", "539", "702",
"703", "704", "705", "706", "707", "17", "27", "37", "47", "57",
"8123", "8124", "8125", "8126", "8127", "8128", "114", "217",
"314", "414", "514", "93562", "93572", "93582", "93592", "93602",
"93612", "124", "224", "324", "424", "524", "3700", "3701", "3702",
"3703", "3704", "3705", "134", "234", "334", "434", "5310", "8233",
"8234", "8235", "8236", "8237", "8238", "18", "28", "38", "48",
"58", "1542", "1543", "1544", "1545", "1546", "1547", "115",
"218", "315", "415", "515", "533", "534", "535", "536", "537",
"538", "125", "225", "325", "425", "525", "208", "209", "210",
"211", "212", "213", "135", "235", "335", "435", "5311", "93563",
"93573", "93583", "93593", "93603", "93613"), class = "data.frame")
These are the first few rows of dataw
head(dataw)
base pos values type labels
1 A 1 13 score D<U+2192>
2 A 1 22 score D<U+2192>
3 A 1 16 score D<U+2192>
4 A 1 21 score D<U+2192>
5 A 1 52 score D<U+2192>
3942 A 1 1 score D<U+2192>
I'm plotting it like so.
prettify <- theme(panel.background = element_rect(fill = NA,color="gray"),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(size=.1, color="black",linetype="dotted"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_line(size=.1, color="black"),
legend.position="bottom")
ggplot(dataw,aes(x = base, y = values, color = type, group = base)) +
geom_boxplot() +
facet_wrap(type ~ pos, scales="free_y", nrow = 2) +
theme_gray() %+replace% prettify
Currently the sublabels are the type value followed by a comma and the pos value. However I would like to get rid of the type value, and label it so that the labels of each subplot are in the format: "Position [pos value], [labels value]"
What would be the best way to go about this? Thank you.
Try replacing the entire ggplot statement with
ggplot(data=transform(dataw, plt_labels = paste("Position ", pos, ", ", labels, sep="")),aes(x = base, y = values, color = type, group = base)) +
geom_boxplot() +
facet_grid(type ~ plt_labels, scales="free_y") +
theme_gray() %+replace% prettify
which should give
I have a dataframe called dataw that I'm trying to plot into dual facet wrapped boxplots.
dataw <- structure(list(base = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 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 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L), values = c(13, 22, 16, 1, 1.709,
2.121, 2.061, 1, 5, 6, 1, 0.856, 1.116, 1.207, 76, 45, 5, 15,
8.558, 5.44, 1.147, 10, 7, 40, 5, 1.547, 1.174, 4.777, 7, 0,
1, 8, 1.322, 0.728, 0.83, 4, 2, 0, 0, 1.098, 0.96, 0.63, 13,
22, 16, 1, 1.709, 2.121, 2.061, 3, 6, 7, 11, 0.952, 1.474, 1.45,
13, 22, 16, 1, 1.709, 2.121, 2.061, 3, 8, 15, 2, 1.014, 1.583,
2.289, 10, 3, 8, 2, 1.504, 1.03, 1.244, 4, 1, 0, 1, 1.066, 0.862,
0.689, 2, 0, 0, 1, 0.919, 0.723, 0.479, 7, 8, 0, 0, 1.299, 1.236,
0.779, 13, 22, 16, 1, 1.709, 2.121, 2.061, 45, 38, 41, 1, 2.817,
2.264, 2.398, 3, 0, 1, 14, 0.973, 0.641, 0.846, 13, 22, 16, 1,
1.709, 2.121, 2.061, 7, 0, 0, 1, 1.37, 0.436, 0.706, 0, 5, 5,
1, 0.597, 1.113, 1.079, 3, 1, 4, 8, 0.992, 0.84, 1.07, 17, 7,
18, 1, 2.4, 1.315, 1.948, 21, 8, 50, 12, 2.412, 1.254, 3.857,
13, 22, 16, 1, 1.709, 2.121, 2.061), type = structure(c(2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L), .Label = c("ipdRatio", "score"), class = "factor")), .Names = c("base",
"pos", "values", "type"), row.names = c("1", "2", "3", "3942",
"3943", "3944", "3945", "11", "21", "31", "63", "64", "65", "66",
"12", "22", "32", "2953", "2954", "2955", "2956", "13", "23",
"33", "2461", "2462", "2463", "2464", "14", "24", "34", "7493",
"7494", "7495", "7496", "111", "212", "311", "4874", "4875",
"4876", "4877", "121", "221", "321", "9356", "9357", "9358",
"9359", "131", "231", "331", "9221", "9222", "9223", "9224",
"15", "25", "35", "93561", "93571", "93581", "93591", "112",
"213", "312", "1579", "1580", "1581", "1582", "122", "222", "322",
"1782", "1783", "1784", "1785", "132", "232", "332", "3398",
"3399", "3400", "3401", "16", "26", "36", "2257", "2258", "2259",
"2260", "113", "214", "313", "1027", "1028", "1029", "1030",
"123", "223", "323", "8654", "8655", "8656", "8657", "133", "233",
"333", "702", "703", "704", "705", "17", "27", "37", "8123",
"8124", "8125", "8126", "114", "215", "314", "93562", "93572",
"93582", "93592", "124", "224", "324", "3700", "3701", "3702",
"3703", "134", "234", "334", "8233", "8234", "8235", "8236",
"18", "28", "38", "1542", "1543", "1544", "1545", "115", "216",
"315", "533", "534", "535", "536", "125", "225", "325", "208",
"209", "210", "211", "135", "235", "335", "93563", "93573", "93583",
"93593"), class = "data.frame")
I'm plotting it like this:
prettify <- theme(panel.background = element_rect(fill = NA,color="gray"),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(size=.1, color="black",linetype="dotted"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_line(size=.1, color="black"),
legend.position="bottom")
ggplot(dataw,aes(x = base, y = values, color = type, group = type)) +
geom_boxplot() +
facet_wrap(type ~ pos, scales="free", nrow = 2) +
theme_gray() %+replace% prettify
But I keep getting only one boxplot in each plot square like so, when in fact I want 4 boxplots for each square:
Does anyone see what I am doing wrong here? Thanks!
So, I have created a list (and a single column matrix) that contains 256 nested lists. What I would like to do, is to convert each of the 256 lists into a single dataframe of 16 columns and then write.table it. Although each list contains the same number of columns (16), the number of rows for each list varies. I have tried to use unlist unsuccessfully because the changing row counts. I can subset each list individually, so I know there's an easier way to do the whole list.
I'm pretty new to R, so I apologize for asking what may be a naive novice question. I searched through a lot of topics the last couple days and didn't see anything that seemed to match my problem. for loop seems like it might be unnecessary and I wasn't sure if lapply was the correct route, either.
UPDATE: dput of first list:
list(structure(list(structure(c(2L, 11L, 15L, 8L, 7L, 3L, 6L, 10L,
1L, 1L, 18L, 13L, 14L, 19L, 16L, 17L, 4L, 5L, 9L, 12L), .Label = c("",
"Aaron Rodgers", "Andrew Quarless", "Derrick Coleman", "Doug Baldwin",
"DuJuan Harris", "Eddie Lacy", "James Starks", "Jermaine Kearse",
"John Kuhn", "Jordy Nelson", "Luke Willson", "Marshawn Lynch", "Percy
Harvin", "Randall Cobb", "Ricardo Lockette", "Robert Turbin",
"Russell Wilson", "Zach Miller"), class = "factor"), Tm =
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("GNB", "Passing", "SEA", "Tm"),
class = "factor"), Cmp = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "19",
"23", "Cmp", "Rushing"), class = "factor"), Att = structure(c(3L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("", "28", "33", "Att", "Receiving"
), class = "factor"), Yds = structure(c(2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, NA, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("", "189", "191", "Yds"), class = "factor"),
TD = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1",
"2", "TD"), class = "factor"), Int = structure(c(3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("", "0", "1", "Int"), class = "factor"),
Lng = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "23",
"33", "Lng"), class = "factor"), Att = structure(c(1L, 1L,
1L, 7L, 3L, 1L, 2L, 2L, NA, 8L, 7L, 4L, 5L, 1L, 1L, 6L, 1L,
1L, 1L, 1L), .Label = c("", "1", "12", "20", "4", "6", "7",
"Att"), class = "factor"), Yds = structure(c(1L, 1L, 1L,
7L, 6L, 1L, 9L, 3L, NA, 10L, 5L, 2L, 8L, 1L, 1L, 4L, 1L,
1L, 1L, 1L), .Label = c("", "110", "2", "27", "29", "34",
"37", "41", "7", "Yds"), class = "factor"), TD = structure(c(1L,
1L, 1L, 2L, 2L, 1L, 2L, 3L, NA, 5L, 2L, 4L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L), .Label = c("", "0", "1", "2", "TD"), class = "factor"),
Lng = structure(c(1L, 1L, 1L, 2L, 4L, 1L, 8L, 6L, NA, 9L,
3L, 7L, 5L, 1L, 1L, 8L, 1L, 1L, 1L, 1L), .Label = c("", "12",
"13", "15", "16", "2", "21", "7", "Lng"), class = "factor"),
Rec = structure(c(1L, 7L, 5L, 3L, 4L, 4L, 1L, 1L, NA, 8L,
1L, 2L, 6L, 4L, 3L, 1L, 2L, 4L, 2L, 2L), .Label = c("", "1",
"2", "3", "6", "7", "9", "Rec"), class = "factor"), Yds = structure(c(1L,
12L, 9L, 3L, 3L, 6L, 1L, 1L, NA, 13L, 1L, 4L, 10L, 8L, 7L,
1L, 5L, 4L, 11L, 2L), .Label = c("", "1", "11", "14", "15",
"26", "38", "42", "58", "59", "8", "83", "Yds"), class = "factor"),
TD = structure(c(1L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, NA, 4L,
1L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 2L), .Label = c("", "0",
"1", "TD"), class = "factor"), Lng = structure(c(1L, 7L,
9L, 3L, 4L, 8L, 1L, 1L, NA, 14L, 1L, 5L, 11L, 10L, 11L, 1L,
6L, 12L, 13L, 2L), .Label = c("", "1", "11", "12", "14",
"15", "16", "18", "23", "24", "33", "6", "8", "Lng"), class = "factor")), .Names = c("", "Tm", "Cmp", "Att", "Yds", "TD", "Int",
"Lng", "Att", "Yds", "TD", "Lng", "Rec", "Yds", "TD", "Lng"),
row.names = c(NA, -20L ), class = "data.frame"))
So, each observation in my list is like this above and I want to convert all of the lists into their 16 column(Now that I think about it, it's 17 columns, one is just unnamed) dataframe layout and stack all the rows together in one place that I can then write.table
Let's call your list l where l[[1]] is what you have dput above.
Two easy ways from base R and from data.table
do.call("rbind", l)
data.table::rbindlist(l)
This assumes that the columns match in each list element. Your example doesn't confirm this, although you state it.