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(2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_new_net = structure(c(2L, 2L,
2L, 2L, 14L, 11L, 14L, 12L, 12L, 14L, 14L, 12L, 14L, 14L,
11L, 12L, 21L, 14L, 21L, 11L, 21L, 14L, 11L, 11L, 15L, 2L,
2L, 8L, 10L, 9L, 9L, 22L, 21L, 23L, 23L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 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, 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"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, 4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L, 6L, 7L, 6L, 6L,
7L, 6L, 8L, 7L, 7L, 4L, 5L, 5L, 8L, 6L, 5L, 5L, 6L, 7L, 2L,
5L, 5L, 7L, 5L, 8L, 6L, 8L, 5L, 8L, 7L, 6L, 6L, 7L, 10L,
8L, 10L, 5L, 5L, 6L, 5L, 4L, 5L, 5L, 6L, 6L, 4L, 4L, 3L,
5L, 7L, 8L, 7L, 5L, 10L, 10L, 6L, 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))
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")
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, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 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, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
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 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("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)