Related
Question: why is geom_line() not connecting through geom_point()?
I have:
Written with
ggplot(a,
aes(x = month, color = year, fill = year)) +
scale_color_manual(values = colsze) +
scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_discrete(labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I cannot figure out why this does not work? E.g. following tutorials like this
geom_line() seems to appear in the legend but not in plot.
a <- structure(list(month = structure(c(4L, 1L, 4L, 7L, 1L, 9L, 2L,
8L, 8L, 10L, 7L, 10L, 9L, 9L, 9L, 2L, 10L, 7L, 4L, 2L, 2L, 3L,
11L, 11L, 12L, 9L, 12L, 10L, 10L, 10L, 11L, 5L, 10L, 10L, 10L,
10L, 10L, 12L, 11L, 7L, 12L, 6L, 9L, 9L, 9L, 7L, 9L, 4L, 12L,
12L, 11L, 3L, 3L, 11L, 11L, 11L, 7L, 11L, 12L, 12L, 12L, 2L,
4L, 1L, 11L, 11L, 1L, 4L, 8L, 2L, 10L, 5L, 5L, 6L, 7L, 11L, 11L,
11L, 11L, 11L, 12L, 11L, 10L, 7L, 12L, 9L, 9L, 7L, 10L, 8L, 8L,
5L, 9L, 10L, 9L, 3L, 8L, 10L, 10L, 8L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "factor"),
year = structure(c(3L, 3L, 2L, 1L, 4L, 4L, 4L, 1L, 1L, 1L,
3L, 1L, 2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 2L,
4L, 4L, 2L, 2L, 2L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 4L, 1L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 1L, 2L, 2L, 1L, 4L,
2L, 1L, 1L, 4L, 4L, 2L, 2L, 4L, 4L, 2L, 3L, 3L, 3L, 4L, 4L,
1L, 1L, 3L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L
), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
n = c(92L, 95L, 83L, 95L, 70L, 88L, 94L, 103L, 103L, 98L,
95L, 98L, 90L, 89L, 89L, 76L, 98L, 97L, 79L, 103L, 103L,
111L, 104L, 104L, 73L, 89L, 73L, 107L, 107L, 107L, 88L, 111L,
107L, 107L, 107L, 107L, 107L, 73L, 104L, 78L, 87L, 92L, 90L,
90L, 90L, 78L, 89L, 92L, 98L, 98L, 85L, 111L, 111L, 85L,
85L, 85L, 97L, 104L, 73L, 73L, 73L, 71L, 92L, 99L, 85L, 104L,
99L, 83L, 103L, 94L, 90L, 90L, 90L, 92L, 97L, 85L, 85L, 88L,
88L, 85L, 73L, 89L, 107L, 97L, 87L, 89L, 89L, 95L, 96L, 103L,
103L, 75L, 90L, 90L, 90L, 88L, 87L, 98L, 98L, 103L)), row.names = c(NA,
-100L), groups = structure(list(month = structure(c(1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L,
7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
12L, 12L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12"), class = "factor"), year = structure(c(2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 3L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 3L, 4L), .Label = c("2017", "2018", "2019", "2020"), class = "factor"),
.rows = structure(list(c(64L, 67L), 2L, 5L, 20:21, 62L, 16L,
c(7L, 70L), c(22L, 52L, 53L), 96L, 19L, c(3L, 68L), c(1L,
48L, 63L), 72:73, 92L, 32L, c(42L, 74L), 4L, c(40L, 46L
), c(11L, 88L), c(18L, 57L, 75L, 84L), c(8L, 9L, 69L,
90L, 91L, 100L), 97L, c(14L, 15L, 26L, 47L, 86L, 87L),
c(13L, 43L, 44L, 45L, 93L, 95L), 6L, c(10L, 12L, 17L,
98L, 99L), c(71L, 94L), c(28L, 29L, 30L, 33L, 34L, 35L,
36L, 37L, 83L), 89L, c(23L, 24L, 39L, 58L, 66L), c(51L,
54L, 55L, 56L, 65L, 76L, 77L, 80L), 82L, c(31L, 78L,
79L), 49:50, c(25L, 27L, 38L, 59L, 60L, 61L, 81L), c(41L,
85L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 36L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Try this:
ggplot(a,
aes(x = as.numeric(month), color = year, fill = year)) +
# scale_color_manual(values = colsze) +
# scale_fill_manual(values = alpha(colsze, .2)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12),
labels = c("January", "February", "March", "April", "May",
"June", "July", "August", "Septemer",
"October", "November", "December")) +
geom_point(aes(y = n), size = 4, shape=19) +
geom_line(aes(y = n)) +
scale_y_continuous(breaks = seq(0, 120, 10), limits = c(0, 120)) +
facet_wrap(.~year)
I commented on those two lines because in your reproducible example there is no variable colsze.
The problem is that month is a factor and must first be converted to numeric. For a better visualization, evaluate whether to rotate the labels on the x axis by 45 °
I have a dataframe from Alzheimer disease patients. I would like to create a table with counts for the number of patients (indicated by patient ID: iid) with age_at_onset <75, <70 for each status (1,2,3) per Ethnicity. How can I do this in R?
df <- structure(list(iid = structure(c(`5068` = 80L, `15562` = 58L,
`8939` = 52L, `17602` = 34L, `3173` = 40L, `12591` = 30L, `17391` = 97L,
`8241` = 93L, `9746` = 10L, `9673` = 7L, `16594` = 29L, `16911` = 60L,
`4796` = 18L, `6598` = 12L, `11462` = 26L, `16425` = 17L, `12698` = 37L,
`17118` = 81L, `1501` = 76L, `13294` = 92L, `8072` = 84L, `11642` = 46L,
`4164` = 85L, `9035` = 62L, `16691` = 35L, `16002` = 86L, `3915` = 21L,
`7409` = 54L, `9759` = 11L, `6130` = 6L, `15153` = 23L, `13539` = 100L,
`13262` = 87L, `742` = 28L, `17592` = 33L, `16812` = 53L, `213` = 66L,
`11963` = 77L, `12093` = 89L, `11910` = 68L, `15813` = 73L, `1104` = 51L,
`1966` = 95L, `5589` = 61L, `8860` = 41L, `482` = 16L, `3967` = 55L,
`5869` = 1L, `12435` = 20L, `11675` = 50L, `16701` = 36L, `5893` = 2L,
`16880` = 57L, `13290` = 90L, `1097` = 49L, `1476` = 71L, `9100` = 67L,
`6220` = 8L, `15393` = 42L, `16631` = 31L, `9641` = 4L, `13485` = 99L,
`1028` = 44L, `8200` = 91L, `12190` = 94L, `5581` = 19L, `7266` = 43L,
`12254` = 98L, `15763` = 69L, `17764` = 79L, `16239` = 96L, `7548` = 59L,
`12037` = 83L, `7813` = 70L, `12943` = 63L, `17748` = 75L, `12703` = 38L,
`11964` = 78L, `14018` = 45L, `1769` = 88L, `13713` = 22L, `13100` = 74L,
`13866` = 32L, `2527` = 25L, `2281` = 15L, `4463` = 39L, `5815` = 14L,
`14040` = 47L, `16560` = 24L, `12887` = 56L, `11167` = 13L, `6123` = 5L,
`5668` = 48L, `3036` = 82L, `7622` = 65L, `11470` = 27L, `4770` = 64L,
`17050` = 72L, `6295` = 9L, `9575` = 3L), .Label = c("08AD09051_NACC295883",
"08AD10766_NACC977458", "08AD9133", "09AD14006", "09AD14313_NACC904765",
"09AD14360_NACC785663", "09AD14874", "09AD14943_NACC009736",
"09AD15417_NACC169039", "09AD15778", "09AD15810", "09AD17022_NACC426380",
"25795", "NACC026302", "NACC026743", "NACC044624", "NACC062886",
"NACC083669", "NACC088187", "NACC094571", "NACC107551", "NACC134929",
"NACC178119", "NACC178349", "NACC183751", "NACC186606", "NACC192719",
"NACC193548", "NACC209758", "NACC224665", "NACC243923", "NACC246256",
"NACC261383", "NACC283729", "NACC298544", "NACC305567", "NACC310219",
"NACC310896", "NACC312856", "NACC336802", "NACC342957", "NACC350799",
"NACC351234_09AD13080", "NACC355338", "NACC355951", "NACC361682",
"NACC369873", "NACC397276", "NACC402765", "NACC403144", "NACC407162",
"NACC412031", "NACC413408", "NACC422516_08AD10849", "NACC436908",
"NACC465387", "NACC472288", "NACC479723", "NACC485644_08AD8204",
"NACC504120", "NACC508353", "NACC509594", "NACC510498", "NACC519864",
"NACC521718_08AD9198", "NACC559675", "NACC585997", "NACC605438",
"NACC612578", "NACC619036_09AD14621", "NACC621261", "NACC634809",
"NACC635885", "NACC639654", "NACC640099", "NACC642393", "NACC660918",
"NACC660981", "NACC684037", "NACC690933", "NACC695603", "NACC703758",
"NACC740374", "NACC744168_08AD7716", "NACC766835", "NACC769330",
"NACC775129", "NACC792439", "NACC796641", "NACC805995", "NACC806269_09AD13056",
"NACC809589", "NACC824113_08AD9038", "NACC884140", "NACC916661",
"NACC921664", "NACC926195", "NACC929277", "NACC959601", "NACC992086"
), class = "factor"), omit = structure(c(`5068` = 1L, `15562` = 1L,
`8939` = 1L, `17602` = 1L, `3173` = 1L, `12591` = 2L, `17391` = 1L,
`8241` = 1L, `9746` = 1L, `9673` = 2L, `16594` = 2L, `16911` = 2L,
`4796` = 1L, `6598` = 2L, `11462` = 1L, `16425` = 1L, `12698` = 1L,
`17118` = 1L, `1501` = 1L, `13294` = 1L, `8072` = 1L, `11642` = 2L,
`4164` = 1L, `9035` = 1L, `16691` = 1L, `16002` = 1L, `3915` = 1L,
`7409` = 1L, `9759` = 1L, `6130` = 1L, `15153` = 1L, `13539` = 2L,
`13262` = 1L, `742` = 2L, `17592` = 1L, `16812` = 1L, `213` = 2L,
`11963` = 2L, `12093` = 2L, `11910` = 2L, `15813` = 1L, `1104` = 1L,
`1966` = 1L, `5589` = 1L, `8860` = 1L, `482` = 1L, `3967` = 1L,
`5869` = 2L, `12435` = 1L, `11675` = 2L, `16701` = 1L, `5893` = 1L,
`16880` = 2L, `13290` = 2L, `1097` = 1L, `1476` = 1L, `9100` = 1L,
`6220` = 1L, `15393` = 1L, `16631` = 1L, `9641` = 1L, `13485` = 2L,
`1028` = 1L, `8200` = 2L, `12190` = 1L, `5581` = 2L, `7266` = 1L,
`12254` = 1L, `15763` = 1L, `17764` = 1L, `16239` = 1L, `7548` = 1L,
`12037` = 1L, `7813` = 1L, `12943` = 2L, `17748` = 1L, `12703` = 1L,
`11964` = 1L, `14018` = 1L, `1769` = 1L, `13713` = 1L, `13100` = 1L,
`13866` = 2L, `2527` = 1L, `2281` = 1L, `4463` = 1L, `5815` = 1L,
`14040` = 1L, `16560` = 2L, `12887` = 1L, `11167` = 2L, `6123` = 2L,
`5668` = 1L, `3036` = 1L, `7622` = 1L, `11470` = 1L, `4770` = 1L,
`17050` = 2L, `6295` = 2L, `9575` = 1L), .Label = c("0", "1"), class = "factor"),
sex = structure(c(1L, 1L, 1L, 2L, 1L, NA, 2L, 2L, 1L, NA,
2L, 2L, 1L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 2L, NA, 2L, 2L, 1L, 2L, 2L, NA, NA, NA,
2L, 1L, 2L, 1L, 1L, 2L, 2L, NA, 2L, NA, 1L, 2L, 1L, NA, 2L,
1L, 2L, 2L, 2L, 2L, 1L, NA, 2L, NA, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, NA, 2L, 2L, 1L, 2L, 1L, 1L, 2L, NA, 1L, 2L,
1L, 2L, 2L, 1L, 1L, NA, NA, 1L, 1L, 2L, 2L, 2L, 2L, NA, 2L
), .Label = c(" 1", " 2", "-9"), class = "factor"), status = structure(c(2L,
2L, 2L, 1L, 3L, NA, 2L, 2L, 2L, NA, 2L, NA, 1L, NA, 2L, 1L,
1L, 1L, 2L, 1L, 2L, NA, 3L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
NA, 1L, 2L, 2L, 2L, NA, NA, NA, NA, 2L, 1L, 1L, 3L, 2L, 1L,
3L, NA, 1L, NA, 2L, 1L, NA, NA, 1L, 2L, 2L, 1L, 1L, 1L, 2L,
NA, 1L, NA, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, NA, 1L,
1L, 2L, 2L, 2L, 1L, 1L, NA, 3L, 1L, 3L, 3L, 1L, NA, 2L, NA,
NA, 3L, 2L, 2L, 1L, 2L, 2L, NA, 2L), .Label = c(" 1", " 2",
" 3", "-9"), class = "factor"), age_at_onset = structure(c(`5068` = 4L,
`15562` = 16L, `8939` = 24L, `17602` = NA, `3173` = 24L,
`12591` = NA, `17391` = 15L, `8241` = 13L, `9746` = 18L,
`9673` = NA, `16594` = 20L, `16911` = NA, `4796` = NA, `6598` = NA,
`11462` = 20L, `16425` = NA, `12698` = NA, `17118` = NA,
`1501` = 5L, `13294` = NA, `8072` = 11L, `11642` = NA, `4164` = 25L,
`9035` = NA, `16691` = NA, `16002` = NA, `3915` = NA, `7409` = 21L,
`9759` = 14L, `6130` = NA, `15153` = NA, `13539` = NA, `13262` = NA,
`742` = 26L, `17592` = 28L, `16812` = 9L, `213` = 14L, `11963` = NA,
`12093` = NA, `11910` = NA, `15813` = 10L, `1104` = NA, `1966` = NA,
`5589` = 16L, `8860` = 8L, `482` = NA, `3967` = 7L, `5869` = NA,
`12435` = NA, `11675` = NA, `16701` = 19L, `5893` = NA, `16880` = 22L,
`13290` = NA, `1097` = NA, `1476` = 7L, `9100` = 22L, `6220` = NA,
`15393` = NA, `16631` = NA, `9641` = NA, `13485` = NA, `1028` = NA,
`8200` = NA, `12190` = NA, `5581` = NA, `7266` = 17L, `12254` = 17L,
`15763` = NA, `17764` = 6L, `16239` = NA, `7548` = 14L, `12037` = 27L,
`7813` = 26L, `12943` = NA, `17748` = NA, `12703` = NA, `11964` = 20L,
`14018` = 23L, `1769` = 25L, `13713` = NA, `13100` = NA,
`13866` = NA, `2527` = 12L, `2281` = NA, `4463` = 1L, `5815` = 3L,
`14040` = NA, `16560` = NA, `12887` = 14L, `11167` = NA,
`6123` = NA, `5668` = 5L, `3036` = 2L, `7622` = 7L, `11470` = NA,
`4770` = 17L, `17050` = 15L, `6295` = NA, `9575` = 19L), .Label = c("44",
"52", "56", "58", "60", "61", "62", "64", "65", "66", "67",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "88", "90"), class = "factor"),
age_last_visit = structure(c(`5068` = 8L, `15562` = 18L,
`8939` = 24L, `17602` = 16L, `3173` = 21L, `12591` = NA,
`17391` = 17L, `8241` = NA, `9746` = NA, `9673` = NA, `16594` = 25L,
`16911` = 4L, `4796` = 5L, `6598` = NA, `11462` = 21L, `16425` = 10L,
`12698` = 25L, `17118` = 12L, `1501` = 7L, `13294` = 9L,
`8072` = NA, `11642` = NA, `4164` = 21L, `9035` = 21L, `16691` = 3L,
`16002` = 14L, `3915` = 13L, `7409` = NA, `9759` = NA, `6130` = 25L,
`15153` = 22L, `13539` = NA, `13262` = 24L, `742` = 26L,
`17592` = 30L, `16812` = 9L, `213` = 11L, `11963` = NA, `12093` = NA,
`11910` = NA, `15813` = 10L, `1104` = 24L, `1966` = 14L,
`5589` = 18L, `8860` = 23L, `482` = 15L, `3967` = 7L, `5869` = NA,
`12435` = 6L, `11675` = NA, `16701` = 25L, `5893` = NA, `16880` = 20L,
`13290` = NA, `1097` = 8L, `1476` = 5L, `9100` = 28L, `6220` = 21L,
`15393` = 17L, `16631` = 9L, `9641` = 24L, `13485` = NA,
`1028` = 7L, `8200` = NA, `12190` = 8L, `5581` = 15L, `7266` = NA,
`12254` = 19L, `15763` = 7L, `17764` = 6L, `16239` = 11L,
`7548` = NA, `12037` = 29L, `7813` = NA, `12943` = NA, `17748` = 23L,
`12703` = 27L, `11964` = 23L, `14018` = 26L, `1769` = 24L,
`13713` = 13L, `13100` = 20L, `13866` = NA, `2527` = 13L,
`2281` = 21L, `4463` = 4L, `5815` = 3L, `14040` = 2L, `16560` = 14L,
`12887` = 24L, `11167` = NA, `6123` = NA, `5668` = 12L, `3036` = 1L,
`7622` = NA, `11470` = 18L, `4770` = 18L, `17050` = 18L,
`6295` = NA, `9575` = NA), .Label = c("59", "60", "61", "62",
"64", "65", "67", "68", "69", "70", "71", "72", "73", "74",
"75", "76", "77", "79", "80", "81", "82", "83", "84", "85",
"86", "89", "91", "92", "93", "94"), class = "factor"), age_at_death = structure(c(`5068` = 2L,
`15562` = NA, `8939` = NA, `17602` = NA, `3173` = NA, `12591` = NA,
`17391` = NA, `8241` = 10L, `9746` = 9L, `9673` = NA, `16594` = NA,
`16911` = NA, `4796` = NA, `6598` = NA, `11462` = NA, `16425` = NA,
`12698` = NA, `17118` = NA, `1501` = NA, `13294` = NA, `8072` = 6L,
`11642` = NA, `4164` = NA, `9035` = NA, `16691` = NA, `16002` = NA,
`3915` = NA, `7409` = 16L, `9759` = 8L, `6130` = NA, `15153` = NA,
`13539` = NA, `13262` = NA, `742` = 14L, `17592` = NA, `16812` = NA,
`213` = NA, `11963` = NA, `12093` = NA, `11910` = NA, `15813` = NA,
`1104` = NA, `1966` = NA, `5589` = NA, `8860` = NA, `482` = NA,
`3967` = NA, `5869` = NA, `12435` = NA, `11675` = NA, `16701` = NA,
`5893` = 16L, `16880` = NA, `13290` = NA, `1097` = NA, `1476` = 1L,
`9100` = NA, `6220` = NA, `15393` = NA, `16631` = NA, `9641` = NA,
`13485` = NA, `1028` = NA, `8200` = NA, `12190` = NA, `5581` = NA,
`7266` = 11L, `12254` = NA, `15763` = NA, `17764` = 3L, `16239` = NA,
`7548` = 6L, `12037` = 15L, `7813` = 13L, `12943` = NA, `17748` = NA,
`12703` = NA, `11964` = NA, `14018` = NA, `1769` = 12L, `13713` = NA,
`13100` = NA, `13866` = NA, `2527` = 5L, `2281` = NA, `4463` = NA,
`5815` = NA, `14040` = NA, `16560` = NA, `12887` = NA, `11167` = NA,
`6123` = NA, `5668` = NA, `3036` = NA, `7622` = 4L, `11470` = NA,
`4770` = NA, `17050` = NA, `6295` = NA, `9575` = 7L), .Label = c("66",
"70", "71", "73", "74", "75", "77", "79", "82", "83", "85",
"86", "88", "90", "93", "94"), class = "factor"), aaoaae = structure(c(3L,
16L, 24L, 19L, 25L, NA, 15L, 13L, 18L, NA, 20L, 6L, 7L, NA,
20L, 13L, 29L, 15L, 4L, 12L, 10L, NA, 25L, NA, 5L, 17L, 16L,
21L, 14L, 29L, 26L, NA, 28L, 26L, 31L, 8L, 14L, NA, NA, NA,
9L, 28L, 17L, 22L, 7L, 18L, 10L, NA, 8L, NA, 19L, 33L, 24L,
NA, 11L, 6L, 22L, 25L, 20L, 12L, NA, NA, 10L, NA, 11L, 18L,
17L, 17L, 10L, 5L, 14L, 14L, 30L, 26L, NA, 27L, 32L, 20L,
23L, 25L, 16L, 24L, NA, 16L, 25L, 6L, 5L, 4L, 17L, 14L, NA,
NA, 15L, 2L, 6L, 22L, 17L, 15L, NA, 19L), .Label = c("-9",
"52", "58", "60", "61", "62", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "88", "90",
"91", "94"), class = "factor"), aaoaae2 = structure(c(3L,
16L, 24L, 19L, 25L, NA, 15L, 13L, 18L, NA, 20L, 6L, 7L, NA,
20L, 13L, 29L, 15L, 4L, 12L, 10L, NA, 25L, NA, 5L, 17L, 16L,
21L, 14L, 29L, 26L, NA, 28L, 26L, 31L, 8L, 14L, NA, NA, NA,
9L, 28L, 17L, 22L, 7L, 18L, 10L, NA, 8L, NA, 19L, 33L, 24L,
NA, 11L, 6L, 22L, 25L, 20L, 12L, NA, NA, 10L, NA, 11L, 18L,
17L, 17L, 10L, 5L, 14L, 14L, 30L, 26L, NA, 27L, 32L, 20L,
23L, 25L, 16L, 24L, NA, 16L, 25L, 6L, 5L, 4L, 17L, 14L, NA,
NA, 15L, 2L, 6L, 22L, 17L, 15L, NA, 19L), .Label = c("-9",
"52", "58", "60", "61", "62", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "88", "90",
"91", "94"), class = "factor"), apoe_1 = structure(c(3L,
3L, 3L, 3L, 3L, NA, 3L, 3L, 3L, NA, 3L, 2L, 3L, NA, 3L, 2L,
3L, 4L, 4L, 3L, 3L, NA, 4L, 3L, 3L, 3L, 3L, 3L, 3L, NA, 2L,
NA, 3L, 3L, 2L, 4L, 4L, NA, NA, NA, 4L, 3L, 4L, 2L, NA, 3L,
4L, NA, 3L, NA, 4L, 3L, 2L, NA, 4L, 3L, 3L, 3L, 3L, 3L, 3L,
NA, 3L, NA, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, NA, 3L,
3L, 4L, 3L, 3L, 3L, 3L, NA, 3L, 3L, 3L, 3L, 3L, 3L, 3L, NA,
NA, 3L, 3L, 3L, 3L, 3L, 4L, NA, 4L), .Label = c("-9", "2",
"3", "4"), class = "factor"), apoe_2 = structure(c(4L, 4L,
3L, 3L, 3L, NA, 4L, 4L, 4L, NA, 3L, 3L, 3L, NA, 4L, 3L, 3L,
4L, 4L, 3L, 4L, NA, 2L, 4L, 3L, 4L, 3L, 4L, 3L, NA, 3L, NA,
3L, 3L, 3L, 4L, 2L, NA, NA, NA, 2L, 3L, 3L, 3L, NA, 3L, 3L,
NA, 3L, NA, 3L, 3L, 2L, NA, 3L, 4L, 4L, 4L, 3L, 4L, 4L, NA,
4L, NA, 2L, 3L, 3L, 3L, 4L, 3L, 2L, 4L, 4L, 3L, NA, 3L, 3L,
4L, 3L, 3L, 4L, 3L, NA, 3L, 3L, 2L, 3L, 2L, 3L, 4L, NA, NA,
2L, 4L, 3L, 4L, 2L, 3L, NA, 4L), .Label = c("-9", "2", "3",
"4"), class = "factor"), apoe4any = structure(c(3L, 3L, 2L,
2L, 2L, NA, 3L, 3L, 3L, NA, 2L, 2L, 2L, NA, 3L, 2L, 2L, 3L,
3L, 2L, 3L, NA, 3L, 3L, 2L, 3L, 2L, 3L, 2L, NA, 2L, NA, 2L,
2L, 2L, 3L, 3L, NA, NA, NA, 3L, 2L, 3L, 2L, NA, 2L, 3L, NA,
2L, NA, 3L, 2L, 2L, NA, 3L, 3L, 3L, 3L, 2L, 3L, 3L, NA, 3L,
NA, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, NA, 2L, 2L, 3L,
2L, 2L, 3L, 2L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 3L, NA, NA, 2L,
3L, 2L, 3L, 2L, 3L, NA, 3L), .Label = c("-9", "0", "1"), class = "factor"),
apoe4dose = structure(c(3L, 3L, 2L, 2L, 2L, NA, 3L, 3L, 3L,
NA, 2L, 2L, 2L, NA, 3L, 2L, 2L, 4L, 4L, 2L, 3L, NA, 3L, 3L,
2L, 3L, 2L, 3L, 2L, NA, 2L, NA, 2L, 2L, 2L, 4L, 3L, NA, NA,
NA, 3L, 2L, 3L, 2L, NA, 2L, 3L, NA, 2L, NA, 3L, 2L, 2L, NA,
3L, 3L, 3L, 3L, 2L, 3L, 3L, NA, 3L, NA, 3L, 2L, 2L, 2L, 3L,
3L, 2L, 3L, 3L, 2L, NA, 2L, 2L, 4L, 2L, 2L, 3L, 2L, NA, 2L,
2L, 2L, 2L, 2L, 2L, 3L, NA, NA, 2L, 3L, 2L, 3L, 2L, 3L, NA,
4L), .Label = c("-9", "0", "1", "2"), class = "factor"),
Ethnicity = structure(c(`5068` = 4L, `15562` = 4L, `8939` = 4L,
`17602` = 3L, `3173` = 4L, `12591` = 4L, `17391` = 4L, `8241` = 4L,
`9746` = 4L, `9673` = 4L, `16594` = 4L, `16911` = 4L, `4796` = 4L,
`6598` = 4L, `11462` = 4L, `16425` = 4L, `12698` = 4L, `17118` = 4L,
`1501` = 4L, `13294` = 4L, `8072` = 4L, `11642` = 4L, `4164` = 1L,
`9035` = 4L, `16691` = 4L, `16002` = 4L, `3915` = 2L, `7409` = 4L,
`9759` = 4L, `6130` = 4L, `15153` = 4L, `13539` = 4L, `13262` = 4L,
`742` = 4L, `17592` = 3L, `16812` = 4L, `213` = 1L, `11963` = 4L,
`12093` = 4L, `11910` = 4L, `15813` = 4L, `1104` = 4L, `1966` = 4L,
`5589` = 1L, `8860` = 4L, `482` = 4L, `3967` = 4L, `5869` = 4L,
`12435` = 4L, `11675` = 4L, `16701` = 4L, `5893` = 4L, `16880` = 4L,
`13290` = 4L, `1097` = 4L, `1476` = 4L, `9100` = 4L, `6220` = 4L,
`15393` = 4L, `16631` = 4L, `9641` = 4L, `13485` = 4L, `1028` = 4L,
`8200` = 4L, `12190` = 4L, `5581` = 4L, `7266` = 4L, `12254` = 4L,
`15763` = 4L, `17764` = 3L, `16239` = 4L, `7548` = 4L, `12037` = 4L,
`7813` = 4L, `12943` = 4L, `17748` = 3L, `12703` = 4L, `11964` = 4L,
`14018` = 4L, `1769` = 4L, `13713` = 4L, `13100` = 4L, `13866` = 4L,
`2527` = 4L, `2281` = 2L, `4463` = 4L, `5815` = 4L, `14040` = 4L,
`16560` = 4L, `12887` = 4L, `11167` = 4L, `6123` = 4L, `5668` = 4L,
`3036` = 4L, `7622` = 4L, `11470` = 4L, `4770` = 2L, `17050` = 4L,
`6295` = 4L, `9575` = 4L), .Label = c("AA", "Asian", "Hispanic",
"NHW"), class = "factor")), row.names = c(NA, -100L), class = "data.frame")
We can do a group by 'status', 'Ethnicity' and get the sum of logical vector
library(dplyr)
df %>%
group_by(status, Ethnicity) %>%
summarise(n_75 = sum(as.numeric(as.character(age_at_onset)) < 75,
na.rm = TRUE),
n_70= sum(as.numeric(as.character(age_at_onset)) < 70,
na.rm = TRUE) )
-output
# A tibble: 10 x 4
# Groups: status [4]
# status Ethnicity n_75 n_70
# <fct> <fct> <int> <int>
# 1 " 1" Asian 0 0
# 2 " 1" Hispanic 0 0
# 3 " 1" NHW 0 0
# 4 " 2" Asian 1 0
# 5 " 2" Hispanic 1 1
# 6 " 2" NHW 18 9
# 7 " 3" AA 1 0
# 8 " 3" NHW 5 5
# 9 <NA> AA 1 0
#10 <NA> NHW 0 0
This is work:
> df %>% select(iid, age_at_onset, status, Ethnicity) %>%
+ mutate(LT75 = ifelse(as.numeric(as.character(df$age_at_onset)) < 75, 1,0), LT70 = ifelse(as.numeric(as.character(df$age_at_onset)) < 70, 1,0)) %>% group_by(status, Ethnicity) %>%
+ summarise(Lessthan75 = sum(LT75, na.rm = 1), Lessthan70 = sum(LT70, na.rm = 1))
`summarise()` regrouping output by 'status' (override with `.groups` argument)
# A tibble: 10 x 4
# Groups: status [4]
status Ethnicity Lessthan75 Lessthan70
<fct> <fct> <dbl> <dbl>
1 " 1" Asian 0 0
2 " 1" Hispanic 0 0
3 " 1" NHW 0 0
4 " 2" Asian 1 0
5 " 2" Hispanic 1 1
6 " 2" NHW 18 9
7 " 3" AA 1 0
8 " 3" NHW 5 5
9 NA AA 1 0
10 NA NHW 0 0
>
You have age_at_onset as factor, convert it to numeric, then use cut to divide data into different buckets and count to count how many iid fall into each bucket.
library(dplyr)
df %>%
mutate(age_at_onset = as.numeric(as.character(age_at_onset)),
age_group = cut(age_at_onset, c(-Inf, 70, 75, Inf))) %>%
count(Ethnicity, status, age_group)
# Ethnicity status age_group n
#1 AA 3 (70,75] 1
#2 AA 3 (75, Inf] 1
#3 AA <NA> (70,75] 1
#4 Asian 1 <NA> 2
#5 Asian 2 (70,75] 1
#6 Hispanic 1 <NA> 2
#7 Hispanic 2 (-Inf,70] 1
#8 Hispanic 2 (75, Inf] 1
#9 NHW 1 <NA> 29
#10 NHW 2 (-Inf,70] 10
#11 NHW 2 (70,75] 9
#12 NHW 2 (75, Inf] 13
#13 NHW 2 <NA> 2
#14 NHW 3 (-Inf,70] 5
#15 NHW 3 (75, Inf] 1
#16 NHW <NA> (75, Inf] 1
#17 NHW <NA> <NA> 20
If needed you can drop the NA values in age_group column with filter(!is.na(age_group)).
I'm attempting to plot coefficient estimates of insect Orders using small_multiple which integrates with ggplo however when I use scale_colour_hue to color code each Order I get Error: Continuous value supplied to discrete scale.
Any help would be appreciated.
Data:
m123456_df:
term estimate std.error statistic group by_2sd model Order
insecticidearea -1.87 1.84 -1.01 fixed TRUE
AcariInsecticideFor Acari
insecticidearea 3.02 1.66 1.80 fixed TRUE
AraneaeInsecticideFor Araneae
insecticidearea 28.18 5.76 4.89 fixed TRUE
ColeopteraInsecticideFor Coleoptera
insecticidearea -2.60 3.52 -0.73 fixed TRUE
DipteraInsecticideFor Diptera
insecticidearea -6.97 7.85 -0.88 fixed TRUE
HemipteraInsecticideFor Hemiptera
insecticidearea 5.47 2.96 1.84 fixed TRUE
HomopteraInsecticideFor Homoptera
insecticidearea -3.98 4.13 -0.96 fixed TRUE
HymenopteraInsecticideFor Hymenoptera
insecticidearea -0.07 0.68 -0.11 fixed TRUE
LepidopteraInsecticideFor Lepidoptera
insecticidearea -9.98 3.28 -3.03 fixed TRUE
OdonataInsecticideFor Odonata
insecticidearea -0.60 0.83 -0.72 fixed TRUE
OrthopteraInsecticideFor Orthoptera
insecticidearea -1.97 1.70 -1.15 fixed TRUE
ThysanopInsecticideFor Thysanoptera
To better see the structure of the data,
dput(m123456_df):
"structure(list(X = c(49L, 50L, 51L, 52L, 53L, 169L, 170L, 171L,
172L, 173L, 1L, 2L, 3L, 4L, 109L, 110L, 111L, 112L, 113L, 54L,
55L, 56L, 57L, 58L, 174L, 175L, 176L, 177L, 178L, 5L, 6L, 7L,
8L, 114L, 115L, 116L, 117L, 118L, 59L, 60L, 61L, 62L, 63L, 179L,
180L, 181L, 182L, 183L, 9L, 10L, 11L, 12L, 119L, 120L, 121L,
122L, 123L, 69L, 70L, 71L, 72L, 73L, 189L, 190L, 191L, 192L,
193L, 17L, 18L, 19L, 20L, 129L, 130L, 131L, 132L, 133L, 74L,
75L, 76L, 77L, 78L, 194L, 195L, 196L, 197L, 198L, 21L, 22L, 23L,
24L, 134L, 135L, 136L, 137L, 138L, 79L, 80L, 81L, 82L, 83L, 199L,
200L, 201L, 202L, 203L, 25L, 26L, 27L, 28L, 139L, 140L, 141L,
142L, 143L, 84L, 85L, 86L, 87L, 88L, 204L, 205L, 206L, 207L,
208L, 29L, 30L, 31L, 32L, 144L, 145L, 146L, 147L, 148L, 89L,
90L, 91L, 92L, 93L, 209L, 210L, 211L, 212L, 213L, 33L, 34L, 35L,
36L, 149L, 150L, 151L, 152L, 153L, 94L, 95L, 96L, 97L, 98L, 214L,
215L, 216L, 217L, 218L, 37L, 38L, 39L, 40L, 154L, 155L, 156L,
157L, 158L, 99L, 100L, 101L, 102L, 103L, 219L, 220L, 221L, 222L,
223L, 41L, 42L, 43L, 44L, 159L, 160L, 161L, 162L, 163L, 104L,
105L, 106L, 107L, 108L, 224L, 225L, 226L, 227L, 228L, 45L, 46L,
47L, 48L, 164L, 165L, 166L, 167L, 168L), term = structure(c(1L,
3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L,
6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L,
1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L,
2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L,
6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L,
1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L,
2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L,
7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L,
5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L,
6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L, 4L, 2L, 6L,
7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L, 6L, 7L, 1L,
4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L, 1L, 3L, 2L,
6L, 7L, 1L, 4L, 2L, 6L, 7L, 1L, 2L, 6L, 7L, 1L, 5L, 2L, 6L, 7L
), .Label = c("(Intercept)", "doy", "insecticidearea", "neonicarea",
"pesticidearea", "sd_(Intercept).SiteID.x", "sd_Observation.Residual"
), class = "factor"), estimate = c(5.833565955, -1.872580966,
0.227436188, 9.992583603, 6.852396625, 5.142632969, -0.678674828,
0.254534918, 9.864923466, 6.97100003, 4.477798595, 0.039781365,
9.850994785, 6.987034948, 4.3283009, 0.123013649, 0.022392237,
9.838260007, 6.994754179, 17.88900765, 3.029762821, 10.65695216,
2.226434694, 16.74910855, 23.73870445, -6.329202795, 11.20683527,
3.142802325, 16.38385835, 19.72583775, 10.76829079, 1.846216149,
16.86087792, 20.66463849, -0.908216649, 10.86819106, 2.09135404,
16.82721692, 23.13229672, 28.18575111, 7.732269676, 18.27238254,
56.95696509, 46.09461569, -8.045705619, 10.81339723, 19.1102249,
58.22254846, 40.85473609, 10.14359574, 18.64465552, 58.48404924,
36.83632929, 4.404336962, 9.931425672, 18.16739664, 58.57271159,
39.6406962, -2.605713718, 9.307524378, 15.82145387, 34.13815459,
36.98723437, 1.498641219, 8.947675916, 15.38990904, 34.28639819,
37.99163279, 9.083495066, 15.55045251, 34.24464848, 38.25136135,
-0.276226574, 9.096730123, 15.56422095, 34.24013774, 26.01030204,
-6.971380003, 9.152834977, 0, 76.54684844, 18.81324754, 4.584413346,
8.566929469, 0, 76.59232376, 21.71528903, 8.750358994, 0, 76.62612401,
21.52279232, 0.204300155, 8.743035747, 0, 76.62605563, 23.1671264,
5.474675587, 10.61361745, 7.81542411, 27.03810868, 34.70000476,
-11.75757077, 11.81009181, 9.412199453, 26.11205322, 27.01646276,
11.12642064, 7.617789448, 27.21673834, 30.33676716, -3.179909206,
11.50125637, 8.155250611, 27.04250033, 49.27043746, -3.98632448,
5.038285369, 19.08368393, 36.3099002, 44.65665992, 3.305098051,
4.660102066, 19.15206617, 36.29619993, 46.86770473, 4.875937397,
19.07213112, 36.36348295, 43.97229941, 3.034326999, 4.717080506,
19.16011396, 36.30805912, 5.398084837, -0.079315577, 2.151502251,
0, 5.373302675, 5.618080493, -0.492859394, 2.150411138, 0, 5.367888491,
5.34786557, 2.145072919, 0, 5.373448857, 6.696394307, -1.214487209,
2.183364895, 0, 5.338975281, 7.142053223, -9.981494663, -6.62191384,
6.870891062, 0.081012541, 7.347328261, -0.955827518, 4.837078368,
6.183147924, 0.245901403, 6.703342056, 4.82069418, 6.322801182,
0.729648159, 12.34478985, -4.615010887, 4.343005964, 4.884578148,
0.209518655, 5.332438336, -0.604879396, 0.877454525, 8.290188235,
4.675812692, 5.70857201, -1.066255113, 1.034587249, 8.238516889,
4.671352748, 4.962001457, 0.863341599, 8.327281966, 4.669175273,
5.976663938, -1.049824638, 0.915523839, 8.275547511, 4.677431349,
5.945020749, -1.977904212, -1.231110798, 2.537802029, 10.77392762,
5.579913084, -1.248700513, -1.384542346, 2.633756398, 10.77936693,
4.732988096, -1.412934304, 2.432763407, 10.84148556, 5.62838417,
-1.108552993, -1.46904816, 2.412972399, 10.83202785), std.error = c(2.345020833,
1.840768317, 1.750080908, NA, NA, 2.554842927, 1.701707856, 1.833883917,
NA, NA, 1.937814035, 1.755048913, NA, NA, 3.153724651, 2.050476456,
1.779085063, NA, NA, 1.814657198, 1.663420324, 1.66101237, NA,
NA, 1.837869979, 1.636172422, 1.643826419, NA, NA, 1.517843621,
1.665313905, NA, NA, 2.32233174, 1.679925176, 1.67454475, NA,
NA, 6.545154384, 5.761076413, 5.699062437, NA, NA, 6.737162579,
5.737241184, 5.834839768, NA, NA, 5.593994125, 5.82677481, NA,
NA, 7.906973554, 6.089892019, 5.829353741, NA, NA, 3.99576339,
3.52936854, 3.444993938, NA, NA, 3.983759621, 3.318339418, 3.446513164,
NA, NA, 3.310686622, 3.436527267, NA, NA, 4.845766085, 3.764235629,
3.440400053, NA, NA, 8.628004354, 7.855856109, 7.876962746, NA,
NA, 8.728305686, 7.915196489, 7.874937593, NA, NA, 7.150052148,
7.872039214, NA, NA, 10.28405417, 7.845041369, 7.87705336, NA,
NA, 3.377133084, 2.967940792, 2.915310064, NA, NA, 3.233316178,
2.815957854, 2.853379324, NA, NA, 2.666723896, 2.914377219, NA,
NA, 4.171797935, 3.048878466, 2.928824718, NA, NA, 4.479237651,
4.131956214, 4.007895041, NA, NA, 4.519776015, 3.863296489, 4.011682605,
NA, NA, 3.722679065, 4.008923785, NA, NA, 5.663222384, 4.498583965,
4.011828348, NA, NA, 0.7259094, 0.686957844, 0.690230788, NA,
NA, 0.693440783, 0.691612154, 0.687328516, NA, NA, 0.581187607,
0.687999626, NA, NA, 0.952040753, 0.681680334, 0.683923538, NA,
NA, 3.810333838, 3.28639311, 1.520388256, NA, NA, 3.276429106,
0.336977305, 3.998119731, NA, NA, 3.278016524, 4.193948156, NA,
NA, 3.162247377, 1.072403509, 2.824905614, NA, NA, 1.125687092,
0.833751484, 0.810504208, NA, NA, 1.120076008, 0.722188325, 0.815860562,
NA, NA, 1.00910672, 0.809981199, NA, NA, 1.508858442, 1.172058637,
0.81176584, NA, NA, 1.839933156, 1.709077899, 1.713608126, NA,
NA, 1.909498614, 1.709505119, 1.711082298, NA, NA, 1.51769132,
1.713651686, NA, NA, 2.06553118, 1.737291954, 1.713781907, NA,
NA), statistic = c(2.487639287, -1.017282267, 0.129957528, NA,
NA, 2.012895946, -0.398819824, 0.138795545, NA, NA, 2.310747324,
0.022666813, NA, NA, 1.372440964, 0.059992715, 0.012586378, NA,
NA, 9.85806447, 1.821405437, 6.41593787, NA, NA, 12.91642212,
-3.868298175, 6.817529602, NA, NA, 12.99596182, 6.466222833,
NA, NA, 8.898228507, -0.540629227, 6.49023626, NA, NA, 3.534262962,
4.89244528, 1.356761706, NA, NA, 6.841844047, -1.402364893, 1.853246646,
NA, NA, 7.303321237, 1.740859407, NA, NA, 4.658714113, 0.723220863,
1.703692401, NA, NA, 9.920681566, -0.738294595, 2.701753485,
NA, NA, 9.28450456, 0.451623849, 2.596153123, NA, NA, 11.47545423,
2.643219261, NA, NA, 7.89376967, -0.07338185, 2.644090798, NA,
NA, 3.014637102, -0.887411875, 1.161975151, NA, NA, 2.155429497,
0.579191351, 1.087872681, NA, NA, 3.037081209, 1.11157462, NA,
NA, 2.092831481, 0.026041947, 1.109937352, NA, NA, 6.85999806,
1.844604044, 3.640647895, NA, NA, 10.73201718, -4.175336201,
4.138984155, NA, NA, 10.13095612, 3.817769562, NA, NA, 7.271868781,
-1.042976702, 3.926918637, NA, NA, 10.9997373, -0.964754773,
1.257090148, NA, NA, 9.880281628, 0.8555124, 1.161632792, NA,
NA, 12.58977847, 1.216270914, NA, NA, 7.764536942, 0.674507139,
1.175793204, NA, NA, 7.436306566, -0.115459162, 3.11707662, NA,
NA, 8.101745142, -0.712623963, 3.12865113, NA, NA, 9.201616665,
3.117840238, NA, NA, 7.033726533, -1.781608106, 3.192410809,
NA, NA, 1.874390415, -3.037218716, -4.355409753, NA, NA, 2.242480463,
-2.836474455, 1.209838297, NA, NA, 2.044938458, 1.149440575,
NA, NA, 3.903802699, -4.303427628, 1.53739861, NA, NA, 4.737052041,
-0.725491237, 1.082603294, NA, NA, 5.096593419, -1.476422528,
1.268093222, NA, NA, 4.917221697, 1.065878567, NA, NA, 3.961050137,
-0.895709997, 1.127817647, NA, NA, 3.231106918, -1.157293189,
-0.718431933, NA, NA, 2.922187554, -0.730445612, -0.80916175,
NA, NA, 3.118544617, -0.824516625, NA, NA, 2.72490884, -0.638092515,
-0.857196679, NA, NA), group = structure(c(1L, 1L, 1L, 3L, 2L,
1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L,
2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L,
1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L,
3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L,
2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L,
1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L,
3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L,
1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 3L,
2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L,
1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 3L, 2L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L), .Label = c("fixed",
"Residual", "SiteID.x"), class = "factor"), by_2sd = c(TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE),
model = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,
17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 19L, 19L,
19L, 19L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L,
22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 24L, 24L, 24L,
24L, 24L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L,
27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L,
29L, 29L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 32L,
32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L,
34L, 34L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L, 36L, 37L,
37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L,
39L, 40L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 41L, 42L,
42L, 42L, 42L, 42L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L,
44L), .Label = c("AcariInsecticideFor", "AcariNeonicFor",
"AcariNullFor", "AcariPesticideFor", "AraneaeInsecticideFor",
"AraneaeNeonicFor", "AraneaeNullFor", "AraneaePesticideFor",
"ColeopteraInsecticideFor", "ColeopteraNeonicFor", "ColeopteraNullFor",
"ColeopteraPesticideFor", "DipteraInsecticideFor", "DipteraNeonicFor",
"DipteraNullFor", "DipteraPesticideFor", "HemipteraInsecticideFor",
"HemipteraNeonicFor", "HemipteraNullFor", "HemipteraPesticideFor",
"HomopteraInsecticideFor", "HomopteraNeonicFor", "HomopteraNullFor",
"HomopteraPesticideFor", "HymenopteraInsecticideFor",
"HymenopteraNeonicFor",
"HymenopteraNullFor", "HymenopteraPesticideFor",
"LepidopteraInsecticideFor",
"LepidopteraNeonicFor", "LepidopteraNullFor", "LepidopteraPesticideFor",
"OdonataInsecticideFor", "OdonataNeonicFor", "OdonataNullFor",
"OdonataPesticideFor", "OrthopteraInsecticideFor", "OrthopteraNeonicFor",
"OrthopteraNullFor", "OrthopteraPesticideFor",
"ThysanopteraInsecticideFor",
"ThysanopteraNeonicFor", "ThysanopteraNullFor", "ThysanopteraPesticideFor"
), class = "factor"), Order = structure(c(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, 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, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L), .Label = c("Acari", "Araneae", "Coleoptera",
"Diptera", "Hemiptera", "Homoptera", "Hymenoptera", "Lepidoptera",
"Odonata", "Orthoptera", "Thysanoptera"), class = "factor")), row.names =
c(NA,
-209L), class = "data.frame")
Plot code:
#required packages
library(dotwhisker)
library(broom)
library(lme4)
m123456_df<-read.csv("C:/Users/breiley/Desktop/m123456_df.csv")
m123456_df$Order=as.factor(m123456_df$Order)
# Relabel predictors (they will appear as facet labels)
m123456_df <- m123456_df %>%
relabel_predictors(c("(Intercept)" = "Intercept",
neonicarea = "Neonictinoid",
insecticidearea= "All insecticide",
pesticidearea = "All pesticide" ))
m123456_df$Order=as.factor(m123456_df$Order)
# Generate a 'small multiple' plot
small_multiple(m123456_df) +
theme_bw() + theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())+ylab("Coefficient estimate") +
geom_hline(yintercept = 0, colour = "grey60", linetype = 2) +
scale_colour_hue(name = "Order",
breaks = c(0,1,2,3,4,5,6,7,8,9,10),
labels = c("Acari",
"Araneae","Coleoptera","Diptera","Hemiptera","Homoptera",
"Hymenoptera","Lepidoptera","Odonata","Orthoptera","Thysanoptera"))+
theme(legend.position = c(0.02, 0.008),
legend.justification=c(0, 0),legend.title = element_text(size=8),
legend.background = element_rect(color="gray90"),
legend.spacing = unit(-4, "pt"),
legend.key.size = unit(10, "pt"))
ggtitle("Arthropod temporal trends") +
theme(plot.title = element_text(face =
"bold"))+scale_colour_discrete(na.translate = F)
ggsave("C:/Users/breiley/Desktop/ForestPesticide.png",width=10,
height=10,dpi=300)
I'm trying to create boxplots using the code below. I've also included sample data. It's time-series data, I have a date-time field, and I've created a factor for Weekday. The problem I have is that my Weekdays when displayed in the boxplot are out of order. I think it's doing it alphabetically instead of chronologically. Does anyone know how to fix this? Also I'd like to rotate the x-axis labels like 45 degrees, so they'll all fit, and have them all displayed.
Code:
boxplot(OrderCnt ~ Weekday, data=icartdf_factor, main="Orders vs Weekday", xlab="Weekday", ylab="Orders")
Sample Data:
dput(droplevels(icartdf_factor[1:50,]))
structure(list(OrderCnt = c(1L, 1L, 0L, 0L, 0L, 2L, 5L, 12L,
16L, 30L, 27L, 21L, 23L, 27L, 37L, 36L, 35L, 30L, 27L, 17L, 8L,
2L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 6L, 9L, 19L, 28L, 27L, 28L,
22L, 29L, 41L, 35L, 43L, 42L, 27L, 16L, 10L, 2L, 1L, 0L, 0L,
0L), DateTime = structure(c(1398931200, 1398934800, 1398938400,
1398942000, 1398945600, 1398949200, 1398952800, 1398956400, 1398960000,
1398963600, 1398967200, 1398970800, 1398974400, 1398978000, 1398981600,
1398985200, 1398988800, 1398992400, 1398996000, 1398999600, 1399003200,
1399006800, 1399010400, 1399014000, 1399017600, 1399021200, 1399024800,
1399028400, 1399032000, 1399035600, 1399039200, 1399042800, 1399046400,
1399050000, 1399053600, 1399057200, 1399060800, 1399064400, 1399068000,
1399071600, 1399075200, 1399078800, 1399082400, 1399086000, 1399089600,
1399093200, 1399096800, 1399100400, 1399104000, 1399107600), class = c("POSIXct",
"POSIXt")), Weekday = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
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), .Label = c("Friday",
"Saturday", "Thursday"), class = "factor"), hourcol = structure(c(2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L), .Label = c("00", "01",
"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
), class = "factor")), .Names = c("OrderCnt", "DateTime", "Weekday",
"hourcol"), row.names = c(NA, 50L), class = "data.frame")
You just need to specify the desired order within the factor itself.
icartdf_factor$Weekday = factor(icartdf_factor$Weekday,
levels = c("Thursday", "Friday", "Saturday"))
boxplot(OrderCnt ~ Weekday, data=icartdf_factor,
main="Orders vs Weekday", xlab="Weekday", ylab="Orders")
To change the angle of the labels, take a look at How can I change the angle of the value labels on my axes?
I have to plot bargraphs on a map using ggplot2.
library(ggplot2)
q=ggplot(data=mapindia, aes(long, lat, group=group,colour)) + geom_polygon(fill="#FF9999", colour="black") +coord_map()
q=q+geom_point(aes(x=LATITUDE, y=LONGITUDE,group=state_name),data=religion)+coord_map()
q <- q+geom_subplot2d(aes(long,lat,subplot = geom_bar(aes(value,fill=fact))), ref = NULL, width = rel(0.8), data = simdat)
print(q)
But got the following error:
## Using binwidth 2.12
## Using binwidth 2.13
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
## Error in eval(expr, envir, enclos) : object 'group' not found
My data sets
dput(head(mapindia, 100))
structure(list(long = c(72.94823, 72.948775, 72.96202, 72.964302,
72.968211, 72.970708, 72.964092, 72.956172, 72.943367, 72.931651,
72.93708, 72.948365, 72.947281, 72.938496, 72.930794, 72.924501,
72.918209, 72.910397, 72.893036, 72.879257, 72.878837, 72.880537,
72.882884, 72.882518, 72.885103, 72.88869, 72.892337, 72.89675,
72.904402, 72.911528, 72.924481, 72.944418, 72.950934, 72.955821,
72.962662, 72.95561, 72.94823, 73.043316, 73.042348, 73.040185,
73.035095, 73.028379, 73.024913, 73.022964, 73.022209, 73.025029,
73.032294, 73.03815, 73.043791, 73.046396, 73.047159, 73.053124,
73.066142, 73.080464, 73.092181, 73.105632, 73.114977, 73.134375,
73.147935, 73.15217, 73.156084, 73.16586, 73.178224, 73.182115,
73.182861, 73.181982, 73.182834, 73.190533, 73.20268, 73.213414,
73.227186, 73.235096, 73.237463, 73.230614, 73.219987, 73.211622,
73.210741, 73.202164, 73.190022, 73.177, 73.165391, 73.150641,
73.1438, 73.13869, 73.133474, 73.13281, 73.136697, 73.160516,
73.178507, 73.192372, 73.206024, 73.208846, 73.214068, 73.225464,
73.236084, 73.258275, 73.268327, 73.272309, 73.269801, 73.265777
), lat = c(20.466602, 20.459483, 20.44602, 20.43615, 20.424953,
20.412235, 20.398937, 20.402725, 20.410493, 20.404022, 20.392923,
20.385058, 20.379266, 20.374414, 20.375353, 20.377813, 20.380272,
20.382634, 20.390107, 20.390654, 20.405481, 20.415684, 20.428257,
20.443762, 20.453246, 20.460464, 20.466909, 20.47418, 20.482435,
20.486774, 20.501579, 20.496404, 20.491097, 20.487117, 20.477538,
20.469934, 20.466602, 20.219381, 20.232198, 20.240647, 20.24748,
20.255641, 20.261145, 20.266745, 20.276713, 20.279753, 20.284505,
20.287736, 20.293816, 20.299704, 20.309768, 20.311575, 20.320982,
20.333331, 20.339792, 20.343499, 20.343211, 20.346733, 20.349013,
20.353571, 20.362402, 20.374462, 20.372372, 20.361166, 20.351196,
20.342554, 20.331159, 20.330208, 20.330965, 20.330202, 20.329626,
20.325824, 20.314522, 20.304079, 20.303419, 20.292882, 20.28424,
20.276552, 20.275796, 20.266398, 20.258519, 20.251874, 20.241429,
20.228231, 20.216457, 20.204968, 20.193763, 20.18094, 20.184923,
20.182923, 20.183772, 20.186809, 20.198582, 20.209307, 20.209965,
20.198458, 20.186199, 20.173564, 20.166254, 20.15885), order = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 63L), hole = c(FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), piece = 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, 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), .Label = c("1", "2"), class = "factor"), group = 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, 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), .Label = c("0.1", "1.1", "2.1", "3.1", "4.1", "5.1",
"5.2", "6.1", "7.1", "8.1", "9.1", "10.1", "11.1", "12.1", "13.1",
"14.1", "15.1", "16.1", "17.1", "18.1", "19.1", "20.1", "21.1",
"22.1", "23.1", "24.1", "25.1", "26.1", "27.1", "28.1", "29.1",
"30.1", "31.1", "32.1", "33.1", "34.1", "35.1", "36.1", "37.1",
"38.1", "39.1", "40.1", "41.1", "42.1", "43.1", "44.1", "45.1",
"46.1", "47.1", "48.1", "49.1", "50.1", "51.1", "52.1", "53.1",
"54.1", "55.1", "56.1", "57.1", "58.1", "59.1", "60.1", "61.1",
"62.1", "63.1", "64.1", "65.1", "66.1", "67.1", "68.1", "69.1",
"70.1", "71.1", "71.2", "72.1", "73.1", "74.1", "75.1", "76.1",
"77.1", "78.1", "79.1", "80.1", "81.1", "82.1", "83.1", "84.1",
"85.1", "86.1", "87.1", "87.2", "88.1", "88.2", "89.1", "90.1",
"91.1", "92.1", "93.1", "94.1", "95.1", "96.1", "97.1", "98.1",
"99.1", "100.1", "101.1", "102.1", "103.1", "104.1", "105.1",
"106.1", "107.1", "108.1", "109.1", "110.1", "111.1", "112.1",
"113.1", "114.1", "115.1", "116.1", "117.1", "118.1", "119.1",
"120.1", "121.1", "122.1", "123.1", "124.1", "125.1", "126.1",
"127.1", "128.1", "129.1", "130.1", "131.1", "132.1", "133.1",
"134.1", "135.1", "136.1", "137.1", "138.1", "139.1", "140.1",
"141.1", "142.1", "143.1", "144.1", "145.1", "146.1", "147.1",
"148.1", "149.1", "150.1", "151.1", "152.1", "153.1", "154.1",
"155.1", "156.1", "157.1", "158.1", "159.1", "160.1", "161.1",
"162.1", "163.1", "164.1", "165.1", "166.1", "167.1", "168.1"
), class = "factor"), id = c("0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1")), .Names = c("long", "lat", "order", "hole", "piece",
"group", "id"), row.names = c(NA, 100L), class = "data.frame")
dput(head(simdat, 100))
structure(list(state = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 14L, 14L, 14L, 14L), .Label = c("", "Andhra Pradesh", "ASSAM",
"Bihar", "Chandigarh", "CHHATTISGARH", "DADRA & NAGAR HAVELI",
"DAMAN & DIU", "Delhi", "GOA", "GUJARAT", "Haryana", "Himachal",
"Jharkhand", "KARNATAKA", "KERALA", "MAHARASHTRA", "MANIPUR",
"MEGHALAYA", "MP", "Odissa", "PONDICHERRY", "Punjab", "Rajasthan",
"TAMIL NADU", "TRIPURA", "UP", "Uttrakhand", "WEST BENGAL"), class = "factor"),
long = c(78.30000305, 78.30000305, 78.30000305, 78.30000305,
78.30000305, 78.30000305, 78.30000305, 78.30000305, 91.5,
91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 85.12999725, 85.12999725,
85.12999725, 85.12999725, 85.12999725, 85.12999725, 85.12999725,
85.12999725, 76.798553, 76.798553, 76.798553, 76.798553,
76.798553, 76.798553, 76.798553, 76.798553, 81.62999725,
81.62999725, 81.62999725, 81.62999725, 81.62999725, 81.62999725,
81.62999725, 81.62999725, 72.96666718, 72.96666718, 72.96666718,
72.96666718, 72.96666718, 72.96666718, 72.96666718, 72.96666718,
72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396,
72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396,
72.806396, 72.806396, 72.806396, 72.806396, 73.96992109,
73.96992109, 73.96992109, 73.96992109, 73.96992109, 73.96992109,
73.96992109, 73.96992109, 72.40000153, 72.40000153, 72.40000153,
72.40000153, 72.40000153, 72.40000153, 72.40000153, 72.40000153,
75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473,
75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473,
75.959473, 75.959473, 75.959473, 75.959473, 85.33000183,
85.33000183, 85.33000183, 85.33000183), lat = c(17.20000076,
17.20000076, 17.20000076, 17.20000076, 17.20000076, 17.20000076,
17.20000076, 17.20000076, 26.09000015, 26.09000015, 26.09000015,
26.09000015, 26.09000015, 26.09000015, 26.09000015, 26.09000015,
25.37000084, 25.37000084, 25.37000084, 25.37000084, 25.37000084,
25.37000084, 25.37000084, 25.37000084, 30.744196, 30.744196,
30.744196, 30.744196, 30.744196, 30.744196, 30.744196, 30.744196,
21.22999954, 21.22999954, 21.22999954, 21.22999954, 21.22999954,
21.22999954, 21.22999954, 21.22999954, 20.26666641, 20.26666641,
20.26666641, 20.26666641, 20.26666641, 20.26666641, 20.26666641,
20.26666641, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189,
20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189,
20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 15.38429276,
15.38429276, 15.38429276, 15.38429276, 15.38429276, 15.38429276,
15.38429276, 15.38429276, 23.03000069, 23.03000069, 23.03000069,
23.03000069, 23.03000069, 23.03000069, 23.03000069, 23.03000069,
29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748,
29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748,
29.017748, 29.017748, 29.017748, 29.017748, 23.35000038,
23.35000038, 23.35000038, 23.35000038), value = c(134L, 3L,
1098L, 16645L, 123L, 2070L, 37L, 66L, 2L, 4131L, 1L, 21L,
0L, 127L, 5L, 651L, 220L, 260L, 1827L, 17596L, 20L, 0L, 0L,
22L, 16L, 0L, 2L, 0L, 18L, 450L, 0L, 22L, 30L, 41L, 56L,
3L, 53L, 66L, 11L, 4674L, 0L, 28L, 0L, 1L, 328L, 3L, 0L,
33L, 0L, 34L, 5L, 5L, 11L, 0L, 440L, 0L, 0L, 318L, 16L, 122L,
2990L, 35L, 0L, 44L, 1L, 671L, 0L, 43L, 0L, 302L, 0L, 0L,
0L, 40L, 10054L, 379L, 1492L, 160L, 77L, 385L, 4348L, 0L,
157L, 59L, 0L, 12L, 334L, 159L, 2013L, 12L, 0L, 3L, 29L,
0L, 15L, 16L, 0L, 49L, 10L, 329L), variable = structure(list(
fact = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
4L, 7L, 8L, 2L, 3L, 5L, 6L, 3L, 1L, 6L, 4L, 8L, 2L, 7L,
5L, 8L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 7L, 2L, 3L, 1L,
6L, 5L, 4L, 5L, 1L, 7L, 8L, 4L, 6L, 2L, 3L, 7L, 6L, 8L,
1L, 5L, 3L, 4L, 2L, 7L, 6L, 5L, 8L, 4L, 1L, 2L, 3L, 5L,
4L, 8L, 6L, 7L, 3L, 2L, 1L, 2L, 7L, 4L, 5L, 6L, 3L, 8L,
1L, 4L, 7L, 6L, 3L, 2L, 5L, 1L, 8L, 4L, 3L, 7L, 5L, 6L,
2L, 8L, 1L, 2L, 1L, 5L, 3L), .Label = c("Buddhist", "Budhist",
"Christian", "Hindu", "Jain", "Muslim", "Others", "Sikh"
), class = "factor")), .Names = "fact", row.names = c(1L,
29L, 57L, 85L, 113L, 141L, 169L, 197L, 2L, 86L, 170L, 198L,
30L, 58L, 114L, 142L, 59L, 3L, 143L, 87L, 199L, 31L, 171L,
115L, 200L, 172L, 4L, 32L, 60L, 88L, 116L, 144L, 201L, 173L,
33L, 61L, 5L, 145L, 117L, 89L, 118L, 6L, 174L, 202L, 90L,
146L, 34L, 62L, 175L, 147L, 203L, 7L, 119L, 63L, 91L, 35L,
176L, 148L, 120L, 204L, 92L, 8L, 36L, 64L, 121L, 93L, 205L,
149L, 177L, 65L, 37L, 9L, 38L, 178L, 94L, 122L, 150L, 66L,
206L, 10L, 95L, 179L, 151L, 67L, 39L, 123L, 11L, 207L, 96L,
68L, 180L, 124L, 152L, 40L, 208L, 12L, 41L, 13L, 125L, 69L
), class = "data.frame")), .Names = c("state", "long", "lat",
"value", "variable"), row.names = c(NA, 100L), class = "data.frame")
There is an known issue with ggsubplot and R3.1.0. See more on this here and here.
I can plot your data with ggplot but not with ggsubplot
library(ggplot2)
df$fact <- df$variable$fact
df$state <- as.character(df$state)
df <- df[ ,-5]
ggplot(df, aes(x = fact, y = value, fill = fact)) +
geom_bar(stat = 'identity', position = 'dodge') +
facet_wrap(~state) +
theme(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.ticks.x = element_blank()
,strip.text = element_text(size = 8))
If you want to represent your data with pies, you could try something like
library(dplyr)
df1 <- df %>%
group_by(state) %>%
mutate(tsum = sum(value),
prop = value/tsum)
ggplot(df1, aes(x=factor(1), y=prop, fill = fact1)) +
geom_bar(stat="identity") +
coord_polar(theta = 'y') +
facet_wrap(~ state, ncol = 4) +
theme(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.text.y = element_blank()
,axis.title.y = element_blank()
,axis.ticks = element_blank()
,strip.text = element_text(size = 8))
For subplot, something like this should work:
library(maptools)
data(wrld_simpl)
india <- wrld_simpl[wrld_simpl$NAME == 'India', ]
dfindia <- fortify(india)
p <- ggplot() +
geom_polygon(data=dfindia, aes(x=long, y=lat,group=group)) +
geom_coord()
p + geom_subplot(data=simdat,
aes(long, lat, group=state,
subplot = geom_bar(aes(x=fact, y=value, fill = state),
stat="identity")), width = 10, height=10)