adding geom_text from different dataset to geom_bar - r

I have a bar plot with a facet grid, and I would like to add the number of observations per sub-plot which are stored in a separate dataframe.
The bar plot is produced with
bar.plot <- ggplot(BarDiff.m.s, aes(x=value.change, fill=incompatibility))+
geom_bar(binwidth=1)+
labs(x="score differences", y="count / years since start of PSA")+
geom_vline(aes(xintercept=0), linetype="dotted")+
theme(plot.title=element_text(face="bold", size=10),
legend.position= "bottom")+
scale_fill_brewer(palette="Set1")+
facet_grid(years.since.peace ~ strategy.cm6.YP, space="free")
I tried to add the geom_text by adding after the geom_bar line
geom_text(data=num.obs, aes(label=paste("obs=",num.obs),y=4,x=min(BarDiff.m.s$value.change)))
however, I obtain the error message
Error in eval(expr, envir, enclos) : object 'incompatibility' not found
Apparently, for some reason, I have to consider the "fill" variable in geom_text; I tried to add group=BarDiff.m.s$incompatibility to geom_text, but to no avail.
I have seen How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R? , but if possible I would like to keep the two data.frames separate and understand how to solve the "fill" issue. Any suggestion would be very much welcome! thx.
The pertaining data for the plot is
BarDiff.m.s <- structure(list(value.change = c(-1, -1, -2, -2, 1, NA, 0, -2,
-1, -2, NA, 2, -3, NA, NA, -3, -2, -1, -4, -1, -3, -1, 2, 2,
NA, 1, -1, 0, 0, -2, -2, -2, -1, 1, NA, -1, -1, 0, -2, NA, 0,
-4, NA, NA, NA, -3, -1, -4, -2, -3, -2, -1, 0, NA, NA, 0, -4,
NA, -2, -2, -3, -1, NA, NA, -1, -1, 0, -2, NA, 0, NA, NA, NA,
NA, -4, NA, -4, -2, -3, -2, -2, 2, NA, NA, 0, -4, -2, NA, NA,
NA, NA, NA, NA, -1, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA,
-4, NA, -2, -1, -2, NA, NA, NA, NA, -3, 1), incompatibility = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 1L), .Label = c("territory", "government"), class = "factor"),
years.since.peace = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 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, 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, 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, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L), .Label = c("y0", "y10", "y15", "y20", "diff.y5",
"diff.y10", "diff.y15", "diff.y20"), class = "factor"), strategy.cm6.YP = structure(c(4L,
4L, 5L, 1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L,
4L, 3L, 3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L,
5L, 1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L,
3L, 3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L, 5L,
1L, 1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 3L,
3L, 4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L, 4L, 4L, 5L, 1L,
1L, 4L, 3L, 4L, 3L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 3L, 3L,
4L, 4L, 4L, 1L, 4L, 5L, 1L, 4L, 1L, 4L), .Label = c("none",
"only offered", "communication/\nfacilitation", "procedural",
"directive", "unspecified"), class = "factor")), .Names = c("value.change",
"incompatibility", "years.since.peace", "strategy.cm6.YP"), class = "data.frame", row.names = c(1298L,
1299L, 1335L, 1339L, 1340L, 1341L, 1344L, 1372L, 1379L, 1386L,
1387L, 1402L, 1415L, 1439L, 1449L, 1454L, 1455L, 1456L, 1463L,
1466L, 1470L, 1496L, 1497L, 1498L, 1525L, 1536L, 1542L, 1546L,
1563L, 1617L, 1618L, 1654L, 1658L, 1659L, 1660L, 1663L, 1691L,
1698L, 1705L, 1706L, 1721L, 1734L, 1758L, 1768L, 1773L, 1774L,
1775L, 1782L, 1785L, 1789L, 1815L, 1816L, 1817L, 1844L, 1855L,
1861L, 1865L, 1882L, 1936L, 1937L, 1973L, 1977L, 1978L, 1979L,
1982L, 2010L, 2017L, 2024L, 2025L, 2040L, 2053L, 2077L, 2087L,
2092L, 2093L, 2094L, 2101L, 2104L, 2108L, 2134L, 2135L, 2136L,
2163L, 2174L, 2180L, 2184L, 2201L, 2255L, 2256L, 2292L, 2296L,
2297L, 2298L, 2301L, 2329L, 2336L, 2343L, 2344L, 2359L, 2372L,
2396L, 2406L, 2411L, 2412L, 2413L, 2420L, 2423L, 2427L, 2453L,
2454L, 2455L, 2482L, 2493L, 2499L, 2503L, 2520L))
The data for the number of observations is:
num.obs <- structure(list(years.since.peace = structure(c(5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L), .Label = c("y0",
"y10", "y15", "y20", "diff.y5", "diff.y10", "diff.y15", "diff.y20"
), class = "factor"), strategy.cm6.YP = structure(c(1L, 3L, 4L,
5L, 1L, 3L, 4L, 5L, 1L, 3L, 4L, 5L, 1L, 3L, 4L, 5L), .Label = c("none",
"only offered", "communication/\nfacilitation", "procedural",
"directive", "unspecified"), class = "factor"), num.obs = c(8L,
5L, 14L, 2L, 8L, 5L, 14L, 2L, 8L, 5L, 14L, 2L, 8L, 5L, 14L, 2L
)), .Names = c("years.since.peace", "strategy.cm6.YP", "num.obs"
), row.names = c(NA, -16L), class = "data.frame")

Move the fill aesthetic to geom_bar and change the y position for geom_text should get you what you want.
bar.plot <- ggplot(BarDiff.m.s, aes(x = value.change)) +
geom_bar(aes( fill = incompatibility), binwidth = 1) +
geom_text(data = num.obs, aes(label = paste("obs=", num.obs),y = 4, x = -4)) +
labs(x = "score differences", y = "count / years since start of PSA") +
geom_vline(aes(xintercept = 0), linetype = "dotted") +
theme(plot.title = element_text(face = "bold", size = 10),
legend.position = "bottom") +
scale_fill_brewer(palette = "Set1") +
facet_grid(years.since.peace ~ strategy.cm6.YP, space = "free")
bar.plot
If you want the text labels to be positioned by value.change in the first data set, probably the easiest way to get that is to merge the relevant column into the second data set.

Related

Create new conditional columns with factors using fewer scripts

I would like to know if there is a way to more elegantly rewrite this piece of script. I have tried case_when but it throws an error message when I try to have several of them within one mutate function. Here is the dput for the file
structure(list(todays_date = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 4L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 4L, 4L, 5L, 5L, 5L, 2L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
5L, 5L, 5L, 6L, 6L, 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, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 8L, 9L, 9L,
9L, 9L), .Label = c("04/11/2019", "05/11/2019", "06/11/2019",
"07/11/2019", "08/11/2019", "12/11/2019", "13/11/2019", "14/11/2019",
"15/11/2019"), class = "factor"), p_initials = structure(c(34L,
54L, 1L, 71L, 16L, 77L, NA, 55L, 56L, 122L, 20L, 53L, 116L, 48L,
36L, 14L, 44L, 55L, 89L, 96L, 105L, 83L, 92L, 98L, 38L, 5L, 70L,
47L, 10L, 10L, 107L, 67L, 70L, 24L, 25L, 32L, 65L, 24L, 124L,
87L, 75L, 80L, 26L, 31L, 112L, 40L, 45L, 117L, 10L, 23L, 11L,
69L, 7L, 8L, 6L, 79L, 81L, 46L, 108L, 13L, 3L, 61L, 82L, 65L,
90L, 102L, 101L, 59L, 93L, 70L, 74L, 29L, 62L, 78L, 67L, 13L,
64L, 119L, 22L, 43L, 10L, 38L, 50L, 104L, 3L, 2L, 125L, 13L,
88L, 4L, 96L, 106L, 84L, 109L, 17L, 74L, 10L, 91L, 63L, 89L,
7L, 120L, 12L, 38L, 95L, 27L, 9L, 86L, 42L, 99L, 70L, 110L, 103L,
74L, 111L, 72L, 85L, 68L, 76L, 73L, 70L, 21L, 77L, 37L, 8L, 66L,
70L, 123L, 94L, 61L, 115L, 25L, 120L, 67L, 119L, 19L, 71L, 21L,
34L, 57L, 42L, 57L, 100L, 18L, 30L, 19L, 105L, 113L, 39L, 60L,
15L, 33L, 95L, 121L, 52L, 97L, 102L, 5L, 58L, 81L, 114L, 119L,
28L, 3L, 7L, 51L, 35L), .Label = c("BA", "BB", "BD", "BE", "BH",
"BI", "BM", "BS", "BY", "CA", "CB", "CD", "CE", "CF", "CG", "CGA",
"CGG", "CI", "CK", "CL", "CM", "CO", "CP", "CS", "CT", "CZ",
"DK", "DO", "DPH", "DT", "GA", "GB", "GG", "IA", "IB", "Ik",
"IK", "IM", "IP", "IS", "ITF", "KA", "KB", "KBA", "KF", "KG",
"KJ", "KK", "KM", "KO", "KP", "KR", "KS", "KY", "NB", "ND", "NF",
"NG", "NI", "NJ", "NK", "NKD", "NL", "NM", "NR", "NRBS", "NT",
"NWD", "NY", "OA", "OB", "OC", "OD", "OH", "OHD", "OI", "OJ",
"OK", "OL", "OM", "OP", "OPI", "OS", "OSP", "OT", "OTL", "PR",
"PS", "SA", "SG", "SH", "SJ", "SLP", "SM", "SP", "SS", "TA",
"TBC", "TE", "TG", "TKP", "TM", "TMB", "TP", "TR", "TS", "WJ",
"WR", "YH", "YKI", "YM", "ZA", "ZB", "ZE", "ZH", "ZK", "ZM",
"ZN", "ZP", "ZS", "ZSS", "ZT", "ZTM", "ZTN", "ZZ"), class = "factor"),
village = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 8L, 1L, 1L,
1L, 8L, 8L, 8L, 8L, 6L, 6L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L,
8L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 1L, 1L, 1L,
1L, 8L, 8L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 2L,
2L, 7L, 7L, 7L, 4L, 4L, 4L, 7L, 7L, 6L, 6L, 6L, 6L, 1L, 1L,
1L, 1L, 7L, 7L, 7L, 8L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 1L, 4L, 4L, 4L, 4L, 3L, 6L, 6L, 8L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 1L, 1L, 1L, 3L,
3L, 3L, 3L, 3L, 3L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 4L, 2L, 3L,
3L, 3L, 3L, 2L, 2L, 2L, 7L, 7L), .Label = c("banembanto",
"bankore", "damzoussi", "pissy", "sabsin", "tanghin", "toundou",
"watenga"), class = "factor"), compound_id = c("40080", "40093",
"40113", "040127", "240043", "240060", "250035", "230047",
"230033", "230049", "250014", "250031", "250002", "250051",
"220040", "220080", "250056", "250045", "250061", "250042",
"250811", "230068", "230104", "230144", "250062", "40144",
"40814", "030015", "030022", "030108", "30156", "30001",
"30002", "30052", "30089", "30069", "30083", "030094", "30144",
"30161", "30192", "30004", "030006", "030025", "30055", "30202",
"30205", "30239", "30259", "30809", "40053", "40086", "40109",
"040116", "40823", "30197", "30216", "30237", "30159", "30167",
"30219", "30223", "260041", "260803", "260055", "260015",
"230098", "230102", "230111", "230145", "250805", "250810",
"260004", "260023", "260032", "240065", "260025", "260075",
"260049", "30012", "030023", "030030", "30057", "40055",
"40118", "80044", "80068", "80075", "30203", "30229", "30238",
"80001", "80007", "220041", "220042", "220022", "220083",
"230115", "230048", "230097", "230072", "80055", "80803",
"80807", "250809", "250806", "220034", "220019", "220064",
"220840", "220001", "220118", "220175", "220834", "220070",
"220099", "220098", "220141", "220805", "220849", "230174",
"030110", "30146", "30190", "30215", "240006", "220097",
"220823", "250016", "240010", "240042", "240049", "240080",
"240073", "240067", "30265", "30822", "30823", "240004",
"230040", "230057", "230078", "230158", "240021", "240053",
"240054", "240064", "240066", "240086", "250009", "250028",
"250039", "250053", "250063", "230150", "230164", "30828",
"40094", "240007", "240013", "240071", "240078", "040018",
"040125", "40147", "80034", "80049"), new_compound_id = c(40080L,
NA, NA, NA, NA, NA, NA, NA, 230033L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 30156L, NA, NA, 30052L, NA, NA, NA, NA, NA, NA, 30192L,
NA, NA, NA, NA, 30202L, NA, NA, NA, NA, 40053L, NA, NA, NA,
NA, 30197L, 30216L, 30237L, NA, NA, 30219L, 30223L, NA, NA,
260055L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
260075L, 260049L, NA, NA, NA, NA, NA, NA, NA, 80068L, NA,
30203L, 30229L, NA, NA, 80007L, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 220840L, NA, NA, NA,
NA, NA, NA, NA, NA, 220805L, NA, NA, NA, NA, 30190L, NA,
NA, NA, NA, 250016L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 30828L, 40094L, NA, NA, NA, NA, NA, NA, NA, NA,
NA), num_sleep_space = c(2L, 3L, 2L, 2L, 3L, 4L, 2L, 3L,
6L, 4L, 8L, 5L, 1L, 2L, 4L, 4L, 3L, 6L, 3L, 10L, 2L, 3L,
9L, 8L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L,
2L, 3L, 4L, 2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 2L,
2L, 3L, 3L, 5L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 7L,
6L, 21L, 2L, 2L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
12L, 14L, 14L, 12L, 15L, 17L, 11L, 16L, 14L, 14L, 11L, 14L,
21L, 2L, 2L, 2L, 2L, 2L, 4L, 21L, 9L, 9L, 23L, 23L, 23L,
23L, 23L, 14L, 1L, 14L, 14L, 14L, 13L, 14L, 14L, 4L, 4L,
4L, 21L, 21L, 21L, 21L, 21L, 9L, 21L, 21L, 21L, 21L, 21L,
21L, 23L, 23L, 23L, 23L, 23L, 4L, 4L, 4L, 4L, 14L, 12L, 16L,
18L, 14L, 14L, 14L, 23L, 23L, 14L, 4L, 4L, 2L, 14L, 12L,
14L, 14L, 14L, 16L, 12L, 12L, 14L, 12L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 18L, 4L, 2L, 19L, 19L, 16L, 20L, 2L, 3L, 5L,
2L, 2L), .Label = c("12/07/2019", "15/06/2019", "15/07/2019",
"16/06/2019", "16/07/2019", "17/06/2019", "17/10/2019", "18/06/2019",
"19/06/2019", "20/06/2019", "20/07/2019", "21/07/2019", "22/06/2019",
"22/07/2019", "23/06/2019", "23/07/2019", "24/06/2019", "24/07/2019",
"25/06/2019", "25/07/2019", "29/06/2019", "29/10/2019", "30/06/2019"
), class = "factor"), note_net_type.brand_net_given = structure(c(6L,
6L, 6L, 6L, 6L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 2L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 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, 3L, 5L, 6L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 1L, 6L, 9L, 9L, 6L, 12L, 1L, 11L, 12L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 4L, 7L, 3L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 4L, 7L, 6L, 12L, 13L, 12L, 6L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 12L, 12L, 7L, 7L, 1L, 12L, 12L, 12L,
10L, 7L, 5L, 7L, 7L), .Label = c("", "Pema.net", "PERMA .NET",
"PERMA,NET", "PERMA. NET", "Perma.net", "PERMA.NET", "Perman.net",
"Permanet", "PERMANET", "Permanet.2", "PERMANET.2", "PERMANT.2"
), class = "factor"), note_net_type.help_hang_net = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
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, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("no", "yes"), class = "factor"), net_shape = 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, 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 = "rectangular", class = "factor"), other_net_shape = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), num_old_net = c(2L, 3L, 2L, 2L, 4L, 6L, 3L, 3L, 4L,
2L, 4L, 5L, 1L, 3L, 6L, 4L, 3L, 2L, 4L, 4L, 3L, 1L, 4L, 4L,
3L, 0L, 2L, 0L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 5L, 4L, 3L, 6L,
6L, 4L, 5L, 6L, 4L, 6L, 5L, 6L, 6L, 5L, 4L, 4L, 4L, 3L, 6L,
6L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 4L, 6L,
5L, 1L, 3L, 4L, 5L, 4L, 5L, 0L, 0L, 2L, 4L, 3L, 4L, 4L, 5L,
4L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 2L, 2L,
3L, 2L, 5L, 4L, 5L, 3L, 3L, 7L, 2L, 3L, 2L, 3L, 3L, 3L, 3L,
2L, 3L, 4L, 2L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 3L, 2L,
4L, 2L, 2L, 5L, 5L, 1L, 4L, 3L, 3L, 5L, 3L, 4L, 5L, 7L, 7L,
7L, 7L, 8L, 3L, 7L, 5L, 3L, 3L, 4L, 3L, 9L, 8L, 4L, 4L, 6L,
4L, 1L, 1L, 4L, 5L, 4L, 3L, 3L), num_hh_members = c(4L, 5L,
4L, 3L, 4L, 6L, 5L, 6L, 7L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, 2L, 4L, 6L, 4L, 10L, 5L,
5L, 5L, 5L, 6L, 12L, 5L, 5L, 4L, 7L, 5L, 5L, 5L, 4L, 5L,
5L, 5L, 6L, 5L, 9L, 5L, 5L, 5L, 6L, 9L, 9L, 6L, 10L, 6L,
5L, 5L, 11L, 10L, 3L, 6L, 5L, 5L, 11L, 8L, 5L, 9L, 10L, 18L,
12L, 12L, 19L, 6L, 15L, 10L, 9L, 7L, 10L, 8L, 22L, 30L, 5L,
6L, 19L, 11L, 5L, 15L, 7L, 7L, 6L, 5L, 6L)), class = "data.frame", row.names = c(NA,
-167L))
and the script I want to rewrite
comp_df <- comp_df %>% mutate(`sleep space category` = ifelse(num_sleep_space == 1, "1", ifelse(num_sleep_space >=2
& num_sleep_space <=4 ,"2-4",ifelse(num_sleep_space >=5 & num_sleep_space <=9,
"5-9", ifelse(num_sleep_space >9, ">9", NA)))),
`sleep space category` = factor(`sleep space category` , levels=c("1","2-4","5-9",">9")),
`number of nets given` = ifelse(note_net_type.num_net_given == 1, "1",
ifelse(note_net_type.num_net_given >=2 & note_net_type.num_net_given <=4 ,"2-4",
ifelse(note_net_type.num_net_given >=5 & note_net_type.num_net_given <=9,"5-9",
ifelse(note_net_type.num_net_given >9, ">9", NA)))),
`number of nets given` = factor(`number of nets given`, levels = c("1","2-4","5-9",">9")),
`net surplus/gap` = num_sleep_space - note_net_type.num_net_given,
`number of household members` = ifelse(hh_member_count >= 1 & hh_member_count<= 5, "1-5",
ifelse(hh_member_count >=6 & hh_member_count <=10,"6-10",ifelse(hh_member_count >10, ">10", NA)))) %>%
mutate(`number of household members` = factor(`number of household members`,
levels = c("1-5","6-10",">10")))
I can see why you want to refactor your code!
You are trying to reinvent the cut function using ifelse statements and without taking advantage of the ability to seperate logic out into simple chunks using functions.
Your whole complex code can be replaced with this:
cut4 <- function(x) cut(x, c(0, 1.5, 4.5, 9.5, 20), c("1", "2-4", "5-9", ">9"))
cut3 <- function(x) cut(x, c(0, 5.5, 10.5, 50), c("1-5", "6-10", ">10"))
comp_df <- comp_df %>%
mutate(`sleep space category` = cut4(num_sleep_space),
`number of nets given` = cut4(note_net_type.num_net_given),
`net surplus/gap` = num_sleep_space - note_net_type.num_net_given,
`number of household members` = cut3(hh_member_count))

How to add color in emmeans graph?

I would like to have one color for each level in x axis. I have tried different ways to enter the colour via col argument, but it doesn't seem to work. So far:
library(emmeans)
library(ggplot2)
df <- structure(list(Scanner = structure(c(4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a", "b", "c", "d", "e",
"f"), class = "factor"), Reta = structure(c(1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("A", "B", "C", "D", "E",
"F"), class = "factor"), erro = c(0.0120000000000005, 0.0289999999999999,
0.088000000000001, 0.00600000000000023, -0.0289999999999964,
0.106999999999999, 0.0850000000000009, 0.172999999999998, 0.183999999999997,
0.208000000000006, 0.192, 0.0869999999999997, -0.0140000000000029,
-0.0420000000000016, -0.0350000000000037, 0.00600000000000023,
0, -0.0100000000000016, 0.167000000000002, 0.276, 0.262, 0.0790000000000006,
0.426000000000002, 0.202999999999999, -0.181000000000004, 0.0560000000000009,
-0.0219999999999985, -0.264999999999993, 0.106000000000002, 0.154999999999998,
-0.0420000000000016, 0.0670000000000002, 0.176000000000002, -0.18,
0.215000000000003, 0.189, -0.036999999999999, 0.169, 0.103000000000002,
-0.622999999999998, 0.268999999999998, 0.106999999999999, -0.0140000000000029,
0.169999999999998, 0.115000000000002, -0.622, 0.276000000000003,
0.0969999999999978, -0.0320000000000036, 0.155999999999999, 0.116,
-0.290999999999997, 0.283000000000001, 0.0439999999999969, 0.0940000000000012,
-0.117000000000001, 0.0249999999999986, 0.00900000000000034,
0.0760000000000005, 0.109999999999999, 0.0549999999999997, 0.0470000000000006,
-0.027000000000001, 0.0130000000000052, 0.036999999999999, 0.0139999999999993,
0.0420000000000016, 0.0459999999999994, -0.109999999999999, 0.007000000000005,
0.0339999999999989, 0.104999999999997, -0.240000000000002, 0.0940000000000012,
-0.0570000000000022, -0.352999999999994, 0.0129999999999981,
0.113, -0.251000000000005, 0.0760000000000005, -0.00200000000000244,
NA, 0.112000000000002, 0.0839999999999996, -0.242000000000004,
0.0530000000000008, -0.134999999999998, -0.446999999999996, 0.118000000000002,
0.075999999999997, -0.0769999999999982, -0.0590000000000011,
-0.0870000000000033, -0.445999999999998, 0.158999999999999, 0.0829999999999984,
-0.270000000000003, -0.0210000000000008, -0.0840000000000032,
-0.189999999999998, 0.116999999999997, 0.0519999999999996, -0.0960000000000036,
-0.0859999999999985, -0.177, -0.271999999999998, 0.0679999999999978,
0.0439999999999969)), row.names = c(NA, -108L), class = c("tbl_df",
"tbl", "data.frame"))
m1 <- lm(erro ~ Scanner*Reta, data = df)
l1 <- emmeans(m1, "Scanner", "Reta")
emmip(l1, ~ Reta |Scanner,
col = rep(cols, each = 108/6),
CIs = TRUE) +
geom_hline(yintercept = 0, linetype = 2) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(y = "Erro absoluto (mm)")
Right now, I think one of solution like this:
cols = RColorBrewer::brewer.pal(6,"Paired")
emmip(m1 ,Scanner~Reta,CIs =TRUE) + facet_wrap(~Scanner) +
geom_hline(yintercept = 0, linetype = 2) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(y = "Erro absoluto (mm)")+
scale_color_manual(values=cols)

change the order of a common legend, in a superimposed graph

I would like to change the order of my legend, and not to display them in alphabetical order as you can see below. I would like to have
"NONE","LIGHT","MEDIUM","HEAVY","V_COLD","COLD","MEDIUM","HOT".
Is it possible? I tried with several arguments but without success.
Below, my table :
structure(list(SOUNAME = 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), .Label = "BALLYSHANNON (CATHLEENS FALL)", class = "factor"),
year_month = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L), .Label = c("2013-03",
"2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09",
"2013-10", "2013-12"), class = "factor"), pre_type = structure(c(4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L), .Label = c("HEAVY", "LIGHT", "MEDIUM",
"NONE"), class = "factor"), pre_value = c(13L, 2L, 11L, 5L,
9L, 3L, 10L, 7L, 2L, 6L, 13L, 10L, 10L, 1L, 15L, 4L, 16L,
2L, 7L, 5L, 2L, 2L, 17L, 9L, 7L, 3L, 13L, 6L, 5L, 2L, 10L,
14L, 1L, 5L, 19L, 6L), tem_type = structure(c(4L, 3L, 2L,
1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L,
2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 1L, 4L,
3L, 2L, 1L), .Label = c("COLD", "HOT", "MEDIUM", "V_COLD"
), class = "factor"), tem_value = c(0L, 7L, 0L, 23L, 0L,
29L, 0L, 1L, 0L, 29L, 2L, 0L, 0L, 21L, 9L, 0L, 0L, 5L, 25L,
0L, 0L, 18L, 13L, 0L, 0L, 21L, 9L, 0L, 0L, 26L, 5L, 0L, 0L,
24L, 0L, 7L), cnt_vehicle = c(NA, 2754406, NA, NA, NA, 2846039,
NA, NA, NA, 3149377, NA, NA, NA, 3058810, NA, NA, NA, 3362614,
NA, NA, NA, 3415716, NA, NA, NA, 3020812, NA, NA, NA, 3076665,
NA, NA, NA, 2775306, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L
)), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value",
"tem_type", "tem_value", "cnt_vehicle", "x"), row.names = c(NA,
-36L), class = "data.frame")
Below my graph:
ggplot(data = b_complet_2013, aes(x = x, y = pre_value*100000, fill = pre_type), stat = "identity") +
scale_x_continuous(breaks=(1:9)+0.2, labels=unique(b_complet_2013$year_month)) +
geom_bar(stat = "identity", width=0.3) +
xlab("date") + ylab ("Number of days of précipitations(left) and temperatures (ritght)") +
ggtitle("Precipitation per month") +
geom_bar(data=b_complet_2013,aes(x=x+0.4, y=tem_value*100000, fill=tem_type), width=0.3, stat = "identity") +
xlab("date") + ylab("Number of days of precipitations(left) and temperatures (ritght)") +
ggtitle("Impact of weather on road traffics") + theme( axis.title.y = element_text(color = "blue", face = "bold")) +
theme(axis.text.y = element_text(color = "blue", face = "bold", size=9)) + theme( axis.title.y.right = element_text(color = "black", face = "bold")) +
theme(axis.text.y.right = element_text(color = "black", size = 9, face = "bold")) +
geom_line(mapping = aes(x= x+0.2, y = as.numeric(cnt_vehicle)), colour = I("blue"), size = 0.8) +
geom_point(aes(x= x+0.2, y = as.numeric(cnt_vehicle), colour = I("blue")), show.legend=FALSE, stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(~./100000,name="Number of days of precipitations(left) and temperatures (ritght)")) +
theme( plot.title = element_text(size = 17)) + theme(axis.title.x = element_text(size = 12)) + theme(axis.title.y = element_text(size = 12)) +
labs(y = "Number of vehicles", color ="black") +
theme(panel.background = element_rect(linetype = "dashed", fill="white"), plot.background = element_rect(linetype = "dashed",fill="grey90" ))

Merge and edit multiple legends when facets and geom_line are plotted separately

Following this guide I have plotted the following graph using the following code. I did split my dataset into one that contains the data that goes in all plots 'control', and the rest 'dfnocontrol'.
ggplot(dfnocontrol,aes(y=value,x=year)) + geom_line(data=dfnocontrol,
aes(color=survivorship),size=1.5) + facet_wrap(~density,nrow=2) +
geom_line(data=dfcontrol,aes(linetype=simulname),color='grey',size=1.5)
I have tried many ways to have only one legend, or to edit the existing two legend but nothing seems to work. scale_fill_manual() seems to be ignored, even though I don't get any error message. I was forced to use linetype to make the 'control' appear in the legend. How can I merge these two legends?
edit: these are the data for control
structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975,
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control",
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70",
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80",
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90",
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L,
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")
and data for the rest
structure(list(year = c(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, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(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, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557,
390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103,
390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798,
390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214,
390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448,
390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404,
390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326,
388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041,
388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871,
383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656,
389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375,
389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947,
388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L,
23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80",
"d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90",
"d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70",
"d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80",
"d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control",
"d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"),
survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L,
4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control",
"s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L,
6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L,
21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L,
38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L,
56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L,
74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L,
92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")
Since you provided no data, I will give you an example using the economics data set.
library(wesanderson) # for the colours
library(tidyverse)
data("economics")
We will need two data sets for this task. Variable unemploy will serve as our 'control' (6th column). All variables will be scaled.
First data set:
economics_gathered <- economics[, 1:5] %>% # exclude unemploy
modify_if(is.numeric, scale) %>%
gather(key, value, -date)
Second data set:
economics_control <- economics[, c(1, 6)] %>%
dplyr::rename(control = unemploy) %>%
gather(some_other_key, value, 2) %>%
mutate(value = scale(value))
Now we can plot:
ggplot() +
geom_line(data = economics_control, aes(x = date, y = value, col = some_other_key)) +
geom_line(data = economics_gathered, aes(date, value, col = key)) +
scale_colour_manual(values = c("grey", wes_palette("GrandBudapest"))) +
facet_wrap(~key, scales = "free_y")
To which the result is the plot below.
EDIT
With the data provided by the OP the following code
ggplot() +
geom_line(data = dfcontrol, aes(year, value, col = survivorship), size = 1.5) +
geom_line(data = dfnocontrol, aes(year, value, col = survivorship), size = 1.5) +
facet_wrap( ~ density, nrow = 2) +
scale_colour_manual(values = c("grey", "forestgreen", "red", "blue"))
gives this plot:
DATA
1)
dfcontrol <- structure(list(year = 1:2, psize = structure(c(6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(392.884450281975,
392.76842677951), simulname = structure(c(1L, 1L), .Label = c("control",
"d02s70", "d02s80", "d02s90", "d05s70", "d05s80", "d05s90", "d07s70",
"d07s80", "d07s90", "d1s70", "d1s80", "d1s90", "d2s70", "d2s80",
"d2s90", "d3s70", "d3s80", "d3s90", "d4s70", "d4s80", "d4s90",
"d5s70", "d5s80", "d5s90"), class = "factor"), survivorship = structure(c(1L,
1L), .Label = c("control", "s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "survivorship"), row.names = 2501:2502, class = "data.frame")
2)
dfnocontrol <- structure(list(year = c(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, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), psize = structure(c(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, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("all plants",
"all plants no-seedl", "seedlings", "SmallerT10", "SmallerT10 no-seedl",
"LargerT10", "10-30", "30-50", "50+"), class = "factor"), value = c(391.933827876557,
390.784233661738, 391.931768654094, 390.777949423224, 391.930831801103,
390.775125884957, 391.904131913644, 390.671681105517, 391.903377880798,
390.669377819171, 391.902842713777, 390.667498067697, 391.874743014214,
390.557893743236, 391.874006362415, 390.555639401299, 391.8735511448,
390.554149478021, 391.84367266143, 390.443618794749, 391.843064602404,
390.442149462261, 391.842594963982, 390.440725187945, 391.72267802326,
388.998242801555, 391.722309813432, 388.996838950063, 391.721745089041,
388.995715149179, 384.967818982887, 383.215849576989, 384.967407490871,
383.214728664341, 384.96689031843, 383.213390281481, 391.897592532656,
389.445606459513, 391.897234485415, 389.444632515097, 391.89681267375,
389.443358475326, 391.402389493961, 388.987279260992, 391.401979078947,
388.985920091544, 391.401583421483, 388.984891027315), simulname = structure(c(2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L,
23L, 24L, 24L, 25L, 25L), .Label = c("control", "d02s70", "d02s80",
"d02s90", "d05s70", "d05s80", "d05s90", "d07s70", "d07s80", "d07s90",
"d1s70", "d1s80", "d1s90", "d2s70", "d2s80", "d2s90", "d3s70",
"d3s80", "d3s90", "d4s70", "d4s80", "d4s90", "d5s70", "d5s80",
"d5s90"), class = "factor"), density = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("control",
"d02", "d05", "d07", "d1", "d2", "d3", "d4", "d5"), class = "factor"),
survivorship = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L,
4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 2L,
3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("control",
"s70", "s80", "s90"), class = "factor")), .Names = c("year",
"psize", "value", "simulname", "density", "survivorship"), row.names = c(6081L,
6082L, 9845L, 9846L, 14345L, 14346L, 17985L, 17986L, 21797L,
21798L, 26297L, 26298L, 30567L, 30568L, 34528L, 34529L, 38744L,
38745L, 43144L, 43145L, 47519L, 47520L, 51983L, 51984L, 56483L,
56484L, 60983L, 60984L, 65483L, 65484L, 69983L, 69984L, 74483L,
74484L, 78983L, 78984L, 83483L, 83484L, 87983L, 87984L, 92483L,
92484L, 96983L, 96984L, 101483L, 101484L, 105983L, 105984L), class = "data.frame")

boxplot with multiple factor labels using base R functions

How can one possibly reproduce the ggplot-based boxplot shown in this answer but using base R boxplot function?
Sample date from the above link:
d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500))
# first factor has 20+ levels
d$f1<-factor(d$f1)
# second factor a,b,c
d$f2<-factor(d$f2)
boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)
It would be great if the groups on the x-axis are spaced from each other.
I have limited knowledge about ggplot2.
EDIT
While waiting for more suggestions using base R functions, I am making some progress with ggplot2.
Using this sample data how can I produce a plot with well aligned x-axis as the one in the link above?
The following does not give me the correct alignment (I want the numbers 1:8 aligned at the center of each group):
library(ggplot2)
ggplot(dat3, aes(x = ID, y = value, group=interaction(obs, ID), fill=obs)) +
geom_boxplot() +
scale_fill_manual(values = c("yellow", "orange"))
dat3=structure(list(values = c(0, 0, 0, 0, 0, 0, 0, 0, -0.0169491525423729,
0, 0, 0, 0, 1, 1, 0.64367816091954, 0.64367816091954, 0, 0, -0.0163934426229508,
-0.021978021978022, 0.109195402298851, 0, 0, 0, 0, 0.207650273224044,
0.4375, 0, 0, 0, 0, 0.302325581395349, 0.303370786516854, 0.270588235294118,
-0.0188679245283019, 0.156462585034014, 0.092436974789916, 0.69,
-0.021978021978022, 0.64367816091954, 0.614906832298137, 0.612903225806452,
0.274853801169591, 0, 0.303370786516854, 0, 0, -0.03125, 0.229813664596273,
0.557142857142857, 0, 0.109195402298851, 0.0746268656716418,
0.180616740088106, 0.210526315789474, 0.310344827586207, 1, 1,
0.0825688073394495, 0.294117647058824, 0, 0.4375, 0, 0.230769230769231,
0.347826086956522, -0.0163934426229508, 0.156462585034014, 0,
0, 0, 1, 0, 0, 0, 0.483333333333333, 0.483333333333333, 0, 0,
0, 0, 0, -0.0169491525423729, 0, 0.310344827586207, 0, 0.296875,
0.302325581395349, 0, 0, 0, 0, 0, 0, 0.482758620689655, 0, 0,
0, 0, 0, 0, 0, 0, 0.150684931506849, 0.150684931506849, 0, 0,
-0.021978021978022, -0.021978021978022, 0.270588235294118, 0,
0, 0.482758620689655, 0.482758620689655, 0.272727272727273, 0.272727272727273,
0, 1, 0, 0, 0.642857142857143, 0.211864406779661, 0.156462585034014,
-0.0449438202247191, -0.0449438202247191, 0.389763779527559,
0.389763779527559, -0.021978021978022, 0.211864406779661, 0.213197969543147,
0.213197969543147, 0.358620689655172, -0.0163934426229508, 0.483333333333333,
0, 0, 0.362139917695473, 0.362139917695473, 0.261904761904762,
0.483333333333333, 1, 1, 0.236453201970443, 0.302325581395349,
0.310344827586207, 1, 1, 0.358974358974359, 0.358974358974359,
-0.0606060606060606, 0.0721649484536082, 0.615384615384615, 0.615384615384615,
0.347826086956522, 1, 0, 0, 0, -0.0273972602739726, -0.0273972602739726,
-0.0169491525423729, -0.0256410256410256, 0.107142857142857,
0.107142857142857, 0.302325581395349, -0.0163934426229508, -0.0264900662251656,
0.311111111111111, 0.311111111111111, 0.156462585034014, 0.156462585034014,
-0.0483091787439614, 0.311111111111111, -0.0333333333333333,
-0.0333333333333333, 0.311111111111111), ind = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L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obs",
"capa"), class = "factor")), .Names = c("values", "ind", "ID",
"obs"), row.names = c(NA, 176L), class = "data.frame")
You can specify the location of the boxes using at option.
set.seed(1)
d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500))
# first factor has 20+ levels
d$f1<-factor(d$f1)
# second factor a,b,c
d$f2<-factor(d$f2)
boxplot(x~f2*f1,data=d, at = (1:80)[-4*(1:20)], col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)
axis(1,at=seq(2,80,4),labels=1:20,cex.axis=0.7)

Resources