Create new conditional columns with factors using fewer scripts - r

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,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L, 4L,
3L, 3L), note_net_type.date_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,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, 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, 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6L, 3L, 5L, 7L, 8L, 7L, 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))

Related

How to reorder x-axis based on y-axis values in R ggplot2

I am trying to reorder (I don't mind whether it is ascending or descending order) the x-axis on my errorplot based on the mean values of the y-axis. I have applied a solution based on this post, however for some reason it seems to be ignoring the reorder command. Any ideas what is happening here?
#Import data.
df <- structure(list(X_Variable = c(4L, 4L, 13L, 18L, 12L, 3L, 15L,
NA, 18L, 4L, 17L, NA, 3L, 15L, 4L, 6L, 12L, NA, 2L, 1L, NA, 15L,
1L, 6L, 1L, 12L, NA, 6L, NA, 15L, NA, 1L, 7L, 15L, 11L, NA, NA,
1L, 1L, 7L, 2L, 2L, 12L, 11L, 15L, 17L, 1L, 4L, 11L, 15L, 2L,
3L, 13L, 17L, 15L, 6L, 3L, 14L, 12L, 8L, 12L, 11L, NA, 2L, 11L,
NA, 4L, 8L, 15L, 4L, 7L, 8L, 15L, 15L, 15L, 6L, 3L, 6L, 8L, 15L,
4L, 2L, 1L, 1L, 7L, 17L, 15L, 1L, NA, 5L, 13L, 1L, 15L, 4L, 15L,
13L, 18L, 1L, 15L, 6L, NA, 6L, NA, 6L, 1L, 16L, 4L, 1L, NA, 2L,
12L, NA, 7L, 2L, 15L, 13L, 13L, 16L, NA, 7L, 2L, 4L, 15L, 11L,
15L, 2L, 5L, 13L, 2L, 9L, 7L, 6L, 15L, 15L, 11L, 3L, 15L, 13L,
NA, 4L, 8L, NA, 4L, 8L, 18L, 4L, 1L, 8L, 5L, 18L), Y_Variable = c(6L,
4L, 5L, 4L, 4L, 3L, 7L, 1L, 1L, 7L, 4L, NA, 5L, 1L, 6L, 1L, 6L,
3L, 6L, 4L, NA, 4L, 6L, 5L, 1L, 4L, 1L, 1L, 6L, 3L, 4L, 1L, 1L,
2L, 3L, 4L, 4L, 2L, 2L, 2L, 4L, 1L, 1L, 5L, 4L, 1L, 4L, 4L, 3L,
3L, 2L, 2L, 1L, 3L, NA, 2L, 4L, 1L, 2L, 2L, 6L, 3L, NA, 2L, 2L,
NA, 4L, 2L, 3L, 6L, 5L, 4L, 1L, 5L, 3L, 1L, 4L, 6L, 1L, 5L, 4L,
2L, 1L, 5L, 4L, 3L, 2L, NA, 4L, 2L, NA, 4L, 5L, 5L, 4L, 2L, 1L,
5L, 2L, 2L, 4L, 1L, 4L, 1L, 5L, 2L, 1L, 3L, NA, 2L, 2L, 2L, 5L,
1L, 1L, 4L, 2L, 2L, NA, 3L, 5L, 7L, 1L, 1L, 1L, 1L, 4L, 1L, 2L,
2L, 3L, 3L, 3L, 4L, 1L, 4L, 3L, 4L, 3L, 6L, 1L, 5L, 4L, 2L, 5L,
2L, 3L, 1L, 1L, 2L)), row.names = c(NA, -150L), class = "data.frame")
#Error plot ordered by Y-Variable.
ggplot(df, aes(x=reorder(X_Variable, Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
I only removed missing values first. The minus sign works fine on my machine.
df1<-df %>%
filter(!is.na(X_Variable), !is.na(Y_Variable))
ggplot(df1, aes(x=reorder(X_Variable, -Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
Edit: Because of missing values, X_Variable 1, 13, and 15 ranked last. Hope this helps.
df %>% group_by(X_Variable) %>%
summarise(
Y_Variable = mean(Y_Variable)) %>%
arrange(Y_Variable)
# A tibble: 18 x 2
X_Variable Y_Variable
<int> <dbl>
1 4 4.71
2 3 3.67
3 12 3.57
4 7 3.29
5 17 2.75
6 18 2.6
7 11 2.57
8 2 2.55
9 5 2.33
10 6 2.3
11 9 2
12 16 2
13 8 1.86
14 14 1
15 1 NA
16 13 NA
17 15 NA
18 NA NA
>

How do I draw a line over the Poisson curve?

How do I draw a line over my Poisson curve in R?
This is the code I used for my plot;
plot(dogbites$daily.dogbites, dpois(dogbites$daily.dogbites, dogbites_lambda),ylab="prob(x)", main="Poisson dog bites")
and this is the plot I got:
I'm hoping to get something like this:
May I know what code can I use for this?
Edit: I tried lines function and type = "o" but I got this instead
> dput(dogbites)
structure(list(daily.dogbites = c(1L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 1L, 3L, 0L, 6L, 9L, 15L, 3L, 4L, 3L,
7L, 6L, 1L, 2L, 3L, 4L, 2L, 5L, 3L, 1L, 6L, 2L, 0L, 0L, 3L, 3L,
6L, 1L, 3L, 2L, 2L, 5L, 6L, 7L, 4L, 10L, 4L, 18L, 4L, 3L, 2L,
5L, 4L, 3L, 2L, 6L, 4L, 6L, 6L, 1L, 2L, 5L, 10L, 4L, 4L, 3L,
0L, 3L, 4L, 2L, 3L, 3L, 5L, 5L, 5L, 8L, 13L, 10L, 12L, 4L, 5L,
3L, 3L, 5L, 4L, 2L, 6L, 4L, 2L, 1L, 3L, 3L, 7L, 5L, 3L, 2L, 5L,
6L, 5L, 3L, 6L, 5L, 3L, 6L, 5L, 9L, 7L, 8L, 12L, 5L, 2L, 6L,
8L, 4L, 2L, 3L, 6L, 6L, 7L, 6L, 5L, 3L, 3L, 6L, 4L, 3L, 6L, 2L,
2L, 6L, 2L, 4L, 5L, 3L, 4L, 5L, 9L, 12L, 9L, 16L, 7L, 3L, 2L,
3L, 0L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 6L, 4L, 6L, 2L, 6L,
5L, 8L, 3L, 3L, 6L, 7L, 5L, 9L, 18L, 22L, 0L, 7L, 5L, 7L, 1L,
5L, 2L, 4L, 1L, 4L, 5L, 3L, 9L, 5L, 4L, 2L, 4L, 4L, 0L, 4L, 4L,
5L, 4L, 9L, 8L, 9L, 7L, 4L, 13L, 12L, 24L, 7L, 4L, 5L, 10L, 2L,
2L, 3L, 8L, 8L, 4L, 6L, 6L, 3L, 7L, 6L, 2L, 6L, 5L, 2L, 1L, 7L,
0L, 8L, 11L, 2L, 10L, 3L, 7L, 9L, 10L, 7L, 2L, 2L, 5L, 2L, 1L,
8L, 4L, 4L, 5L, 3L, 3L, 2L, 4L, 7L, 3L, 2L, 1L, 3L, 7L, 9L, 8L,
2L, 4L, 8L, 7L, 4L, 9L, 21L, 3L, 2L, 1L, 5L, 3L, 4L, 3L, 3L,
4L, 4L, 2L, 5L, 5L, 2L, 3L, 1L, 4L, 4L, 0L, 1L, 7L, 4L, 2L, 2L,
1L, 5L, 6L, 3L, 7L, 7L, 14L, 4L, 1L, 4L, 6L, 6L, 1L, 2L, 3L,
2L, 0L, 8L, 3L, 1L, 5L, 1L, 4L, 3L, 5L, 7L, 0L, 3L, 3L, 5L, 2L,
4L, 7L, 6L, 7L, 9L, 19L, 5L, 0L, 3L, 0L, 1L, 3L, 4L, 1L, 5L,
2L, 4L, 3L, 6L, 3L, 4L, 7L, 5L, 9L, 3L, 7L, 6L, 5L, 3L, 6L, 5L,
3L, 5L, 8L, 12L, 5L, 17L, 3L, 3L, 2L, 4L, 5L, 4L, 2L, 2L, 1L,
3L, 5L, 4L, 3L, 2L, 1L, 2L, 4L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L
)), class = "data.frame", row.names = c(NA, -378L))
> dput(dogbites_lambda)
4.50529100529101
You need to sort the data by the x axis values
set.seed(42)
x = sample(1:25)
y = dpois(x, 5)
graphics.off()
plot(sort(x), y[order(x)], type = "o")

barchart - axis ticks are not adjusted according to the bars

I have to draw a bar chart in R ggplot2 with multiple variables (i.e each bar for BMI, weight, cholesterol, Blood pressure etc) in each group ( i.e. different populations ex: Indian, Korean, Philipinos etc.) But the bars are overflowing to the next group in the axis. for example: the bars of the Indian group is overflowing to Korean group. The axis marks are not adjusted accordingly. I have attached the figure .. can someone please help. Following is my code. dput(data) is also given.
p = ggplot(data = t,
aes(x = factor(Population, levels = names(sort(table(Population), increasing = TRUE))),
y = Snp_Count,
group = factor(Trait, levels = c("BMI", "DBP", "HDL", "Height", "LDL", "TC", "TG", "WC", "Weight"),
ordered = TRUE)))
p = p + geom_bar(aes(fill = Trait),
position = position_dodge(preserve = "single"),
stat = "identity") +
scale_fill_manual(values = c("#28559A", "#3EB650", "#E56B1F", "#A51890", "#FCC133", "#663300", "#6666ff", "#ff3300", "#ff66ff")) +
coord_flip()
structure(list(Trait = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 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, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("BMI",
"DBP", "HDL", "HT", "LDL", "TC", "TG", "WC", "Weight"), class = "factor"),
Association = 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), .Label = "Direct", class = "factor"), TraitClass = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Anthropometric",
"BP", "Lipid"), class = "factor"), Population = structure(c(2L,
3L, 4L, 5L, 7L, 8L, 10L, 11L, 12L, 13L, 22L, 24L, 3L, 5L,
11L, 22L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 18L, 20L, 28L, 5L, 7L, 13L, 14L, 1L, 3L, 5L, 7L,
9L, 11L, 12L, 16L, 18L, 20L, 22L, 5L, 6L, 7L, 10L, 12L, 18L,
20L, 3L, 5L, 6L, 7L, 8L, 11L, 12L, 13L, 14L, 15L, 18L, 19L,
20L, 21L, 22L, 23L, 26L, 28L, 3L, 4L, 5L, 8L, 12L, 22L, 24L,
3L, 5L, 7L, 8L, 17L, 25L, 27L), .Label = c("ACB", "AFR",
"ASW", "ASW/ACB", "CEU", "CHB", "EAS", "Filipino", "FIN",
"GBR", "Hispanic", "Hispanic/Latinos", "JPT", "Korean", "Kuwaiti",
"Micronesian", "Moroccan", "MXL", "Mylopotamos", "Orcadian",
"Pomak", "SAS", "Saudi_Arabian", "Seychellois", "Surinamese",
"Taiwanese", "Turkish", "YRI"), class = "factor"), Snp_Count = c(3L,
12L, 6L, 17L, 2L, 10L, 1L, 6L, 3L, 3L, 10L, 6L, 1L, 1L, 1L,
1L, 2L, 1L, 10L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 3L, 1L, 1L,
2L, 1L, 2L, 20L, 5L, 4L, 1L, 1L, 2L, 7L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 8L, 2L, 4L, 3L, 1L, 2L, 1L, 4L, 20L, 5L,
11L, 2L, 4L, 3L, 4L, 2L, 3L, 4L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 3L, 2L, 4L, 4L, 1L, 4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L
), Gene_Count = c(3L, 9L, 7L, 9L, 2L, 8L, 1L, 7L, 3L, 2L,
8L, 7L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 9L, 6L, 5L, 1L, 1L, 2L, 5L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 6L, 2L, 3L, 3L, 1L, 2L, 1L, 3L,
10L, 4L, 7L, 1L, 3L, 3L, 4L, 1L, 3L, 5L, 1L, 1L, 1L, 3L,
3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 3L, 2L, 3L, 3L, 2L, 3L, 2L,
2L, 2L)), class = "data.frame", row.names = c(NA, -86L))
The total width of each group in your barchart is 0.9 by default, which means that 90% of the area is covered. When you increase the width of the individual bars to 3 they will overlap with other groups, the maximum value for with should thus be 1 and then it will touch the other groups.
I'd suggest in your situation to use facet_wrap instead of a dodged barchart.
Note: geom_col is the same as geom_bar(stat = "identity).
my.df$Trait <- factor(my.df$Trait, levels = c("BMI", "DBP", "HDL", "HT", "LDL", "TC", "TG", "WC", "Weight"))
my.df$Population <- factor(my.df$Population, levels = names(sort(table(my.df$Population), increasing = TRUE)))
ggplot(my.df, aes(x = Trait, y = Snp_Count, fill = Trait)) +
geom_col(width = 1) +
scale_fill_manual(values = c("#28559A", "#3EB650", "#E56B1F", "#A51890", "#FCC133", "#663300", "#6666ff", "#ff3300", "#ff66ff")) +
# Split the data by Population, allow flexible scales and spacing for y axis (Trait)
facet_grid(Population ~ ., scales = "free_y", space = "free_y", switch = "y") +
coord_flip() +
theme(axis.text.y = element_blank(), # Remove Trait labels (indicated by color)
axis.ticks.y = element_blank(), # Remove tick marks
strip.background = element_blank(),
strip.text.y = element_text(angle = 180, hjust = 1), # Rotate Population labels
panel.spacing.y = unit(3, "pt")) # Spacing between groups
Data
my.df <-
structure(list(Trait = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 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, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L),
.Label = c("BMI", "DBP", "HDL", "HT", "LDL", "TC", "TG", "WC", "Weight"), class = "factor"),
Population = structure(c(2L, 3L, 4L, 5L, 7L, 8L, 10L, 11L,
12L, 13L, 22L, 24L, 3L, 5L, 11L, 22L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 18L, 20L, 28L, 5L,
7L, 13L, 14L, 1L, 3L, 5L, 7L, 9L, 11L, 12L, 16L, 18L, 20L,
22L, 5L, 6L, 7L, 10L, 12L, 18L, 20L, 3L, 5L, 6L, 7L, 8L,
11L, 12L, 13L, 14L, 15L, 18L, 19L, 20L, 21L, 22L, 23L, 26L,
28L, 3L, 4L, 5L, 8L, 12L, 22L, 24L, 3L, 5L, 7L, 8L, 17L,
25L, 27L),
.Label = c("ACB", "AFR", "ASW", "ASW/ACB", "CEU",
"CHB", "EAS", "Filipino", "FIN", "GBR", "Hispanic", "Hispanic/Latinos",
"JPT", "Korean", "Kuwaiti", "Micronesian", "Moroccan", "MXL",
"Mylopotamos", "Orcadian", "Pomak", "SAS", "Saudi_Arabian",
"Seychellois", "Surinamese", "Taiwanese", "Turkish", "YRI"), class = "factor"),
Snp_Count = c(3L, 12L, 6L, 17L, 2L,
10L, 1L, 6L, 3L, 3L, 10L, 6L, 1L, 1L, 1L, 1L, 2L, 1L, 10L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 3L, 1L, 1L, 2L, 1L, 2L, 20L,
5L, 4L, 1L, 1L, 2L, 7L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 8L,
2L, 4L, 3L, 1L, 2L, 1L, 4L, 20L, 5L, 11L, 2L, 4L, 3L, 4L,
2L, 3L, 4L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 3L, 2L, 4L, 4L, 1L,
4L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L)),
class = "data.frame", row.names = c(NA, -86L))

Incorrect shape and fill of ggplot legend

I am trying to make a plot showing the cumulative mortality across different feeding levels and temperatures. I have managed to get the graph to look correct, but the legend does not match. I'm sure my code is overly complicated, but it is the only way I have found to achieve the correct visual. I would like the different temperatures to be represented by different shapes and I would like the different feeding levels to be represented by solid or hollow fill (but the same shape as the corresponding temperature). The feeding level only applies at 2 and 5 degrees.
As seen below, the feeding level on the graph does show solid and hollow points, but on the legend it does not. I would also like all the point shown below 'Temperature' in the legend to be solid.
Here is my code:
ggplot(ac_tank_cumulative_mort_summary, aes(Day, mean, shape = factor(Target_Temp),fill=factor(Feeding))) +
geom_point(stat = "identity",size=3.5,color="black") +
geom_line() +
scale_y_continuous(limits = c(0,100)) +
scale_shape_manual(name="Temperature (ºC)",labels=c("0 ºC","2 ºC","5 ºC","7 ºC","9 ºC"),values=c(21,22,23,24,25)) +
scale_fill_manual(name="Feeding",labels=c("High Food","Low Food"),values=c("black","white")) +
xlab("Day of Experiment") +
ylab("Cumulative Mortality (%)") +
ggtitle("Cumulative Percent Mortality \n of Later Stage Arctic Cod") +
theme_bw() +
theme(axis.text = element_text(size = 16, color='black'), axis.title = element_text(size = 16, face = "bold"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.text = element_text(size = 16), legend.title = element_text(size = 16, face = "bold"), plot.title = element_text(size = 18, face = "bold",hjust=0.5))
It produces a graph that looks like this:
A reproducible example:
structure(list(Target_Temp = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 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, 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, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L), Feeding = c(1L, 1L, 1L,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 16L, 17L, 18L,
19L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L), N = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), mean = c(0, 5.23026878966667, 15.1164184233333, 25.0941619566667,
31.02208526, 37.00051361, 39.6671802766667, 43.1015237133333,
46.0402328333333, 49.4934086633333, 52.9560006833333, 54.0859441866667,
55.7620270533333, 57.4569423066667, 57.4569423066667, 58.6369757233333,
60.40180446, 60.9865997833333, 60.9865997833333, 61.58183788,
62.77231407, 0, 5.483691307, 9.37154714766667, 18.2054598866667,
19.8012953533333, 23.7363121066667, 26.4029787733333, 31.31040864,
34.08436863, 36.8583286166667, 38.5098588766667, 39.0474932833333,
40.1227621, 40.66039651, 42.91086732, 43.52815127, 43.52815127,
43.52815127, 43.52815127, 44.1454352233333, 44.1454352233333,
0, 5.32153032133333, 12.76963777, 24.6092796066667, 32.63939764,
41.1558811566667, 43.8225478233333, 47.6157916166667, 49.2030932033333,
52.46723647, 53.0227920266667, 53.57834758, 53.57834758, 54.09116809,
54.6759634133333, 58.1083346633333, 58.6931299833333, 59.2779253066667,
60.90874968, 61.4643052366667, 62.0019396466667, 0, 5.84092792033333,
18.993371995, 28.6523059933333, 32.47031207, 32.9397956366667,
34.27312897, 37.6423639866667, 39.56172328, 43.4211543733333,
44.4015465333333, 46.8111019033333, 49.2206572766667, 49.6901408466667,
50.1803369233333, 53.25726, 56.33418308, 58.2949673933333, 61.3040171633333,
64.8485118866667, 0, 13.1614526916667, 22.6657863833333, 31.59793698,
38.60881636, 41.6744537733333, 42.0077871066667, 42.0077871066667,
45.0654117, 47.1327193933333, 47.6455399066667, 48.6711809333333,
49.6534850133333, 54.2688696266667, 55.3529521233333, 57.4086130966667,
59.4730877466667, 60.9999279933333, 61.54637608, 61.54637608,
0, 11.9041826816667, 20.52782238, 29.3914919133333, 31.8773415066667,
32.9526103233333, 34.2859436566667, 34.9395384266667, 34.9395384266667,
35.5931332, 36.8751844833333, 40.96136817, 43.3312574, 45.71371576,
51.05476461, 65.1266928133333, 73.21981707, 77.7511211166667,
79.56133673, 80.8065805933333, 0, 40, 87.5, 90, 90, 92.5, 92.5,
92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5), sd = c(0,
1.97372461784689, 5.80473942192512, 12.9273738611295, 18.7980078654077,
26.2827168030405, 24.7758104083834, 25.1241212927305, 27.2873150523553,
27.4420059335036, 27.4630003540068, 26.461746133237, 25.0082449931503,
23.6407105265119, 23.6407105265119, 22.6249375824511, 21.0314195173936,
20.4868619144685, 20.4868619144685, 20.0127776006859, 19.1960498751,
0, 0.77902264946986, 1.23794275560703, 3.34864008798032, 3.2335928525848,
5.36060756499091, 5.77844904674583, 4.44080298306709, 4.09533840068484,
3.9968905491584, 4.06201390276555, 3.24181683679238, 1.93161303094676,
1.71565205207569, 2.64834297675624, 2.66829911911964, 2.66829911911964,
2.66829911911964, 2.66829911911964, 3.08417845208611, 3.08417845208611,
0, 0.910533229473384, 2.72480540999204, 6.65751125731073, 8.33148895279841,
9.0007033472894, 9.57771603598199, 11.7084213217991, 12.9037012862438,
14.5648056008555, 15.1859834728865, 15.8412902763179, 15.8412902763179,
14.9818268027929, 15.7149156535738, 18.1464512942252, 18.9773239417533,
19.8251322063165, 18.2151433915608, 18.2852656545651, 17.440878798362,
0, 3.00352826170525, 9.15977839384524, 9.07674417328714, 7.17760460594045,
6.49258187655286, 6.47302852615445, 5.18715642772437, 4.46042901997611,
3.92629364971166, 4.75362293709948, 4.62667657495029, 5.55770752177903,
5.01285084668542, 4.53061089193868, 7.36285237297975, 9.36455421131727,
6.89234052824489, 6.90699192099218, 6.4916961180775, 0, 4.40845912544074,
6.13942103207389, 6.68730640525036, 9.00051763429896, 11.6269296244466,
11.4835770151044, 11.4835770151044, 12.6472892332494, 13.3450448267859,
12.8661920432309, 12.0486504655982, 11.2113054845842, 10.9471632374873,
11.8734160758113, 10.9393380542906, 10.181245628949, 9.03442659821049,
9.64562925636332, 9.64562925636332, 0, 8.39261284702131, 9.64654877195206,
12.3498744833651, 13.5156278842202, 13.1057317249229, 13.3307327422633,
14.420694327421, 14.420694327421, 15.5166855751566, 14.066097040009,
12.5037531860345, 12.3444832323343, 14.1519399447546, 10.2648576647302,
5.60999878946208, 2.3909883546916, 5.27289282704079, 5.52418422785351,
6.39683309409102, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), se = c(0, 1.13953043942009, 3.3513678678241,
7.46362277863804, 10.8530349013219, 15.1743336212701, 14.3043208086713,
14.5054181915108, 15.7543386909395, 15.8436495128116, 15.8557706471406,
15.2776962532519, 14.4385169787554, 13.6489705863157, 13.6489705863157,
13.0625138036266, 12.1424957198072, 11.8280952411691, 11.8280952411691,
11.5543825349881, 11.0828445627664, 0, 0.449768936376239, 0.714726583191068,
1.93333825621461, 1.86691570388948, 3.09494822066744, 3.33618911263724,
2.56389879769188, 2.36444472805801, 2.30760583447808, 2.34520482021369,
1.87166382338554, 1.1152173033873, 0.990532174101631, 1.52902153053667,
1.54054321470217, 1.54054321470217, 1.54054321470217, 1.54054321470217,
1.78065125954076, 1.78065125954076, 0, 0.525696605142557, 1.57316713694826,
3.84371591654131, 4.81018738964856, 5.1965585004535, 5.529696931596,
6.75986020192626, 7.4499554111554, 8.40899443434848, 8.76763164598026,
9.14597320534316, 9.14597320534316, 8.64976173754481, 9.07301078288308,
10.4768585395573, 10.95656308627, 11.4460454160367, 10.5165179404452,
10.557003047867, 10.0694960691379, 0, 1.73408785041418, 5.28840052140387,
5.2404606918127, 4.14399195137642, 3.74849389416348, 3.7372047620474,
2.99480615987537, 2.57522989538443, 2.26684669557854, 2.74450548236037,
2.67121296600089, 3.20874393377633, 2.89417078574127, 2.61574941805425,
4.25094479954333, 5.40662789474487, 3.97929465932875, 3.98775364487541,
3.74798250126929, 0, 2.54522506278467, 3.54459638553631, 3.8609181532248,
5.19645127900848, 6.71281094852307, 6.63004628093031, 6.63004628093031,
7.30191584333562, 7.70476522309248, 7.42829943960477, 6.95629158968482,
6.47285023949184, 6.32034764202606, 6.85511996757007, 6.31582977040099,
5.87814490455939, 5.21602862845074, 5.56890664766469, 5.56890664766469,
0, 4.84547728643206, 5.56943753023738, 7.13020335742892, 7.80325139722136,
7.56659773931127, 7.69650213724068, 8.32579175183782, 8.32579175183782,
8.95856259374746, 8.12106491249661, 7.21904526783764, 7.12709071719501,
8.17062633665946, 5.92641833592515, 3.23893431124941, 1.38043777021046,
3.04430609310005, 3.18938925100431, 3.69321330883456, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Target_Temp",
"Feeding", "Day", "N", "mean", "sd", "se"), row.names = c(NA,
-139L), class = "data.frame")
As aosmith pointed out in the comment you can add this line in order to make sure that a shape with a fill is used in the legend:
guides(fill = guide_legend(override.aes = list(shape = 21)),
shape = guide_legend(override.aes = list(fill = "black")))

R: Ggvis - add_tooltip for bar chart

i'm having troubles using the add_tooltip from ggvis.
I just want to put a tool tip for the sessions by source to my plot.
I'm having troubles understanding the html function that needs to be created for add_tooltip()
I understand i need an "ID" within my data (you can see my data at the bottom). Please, may someone explane this part. I don't understand how ggvis uses the ID for the plot.
Visitas_Por_Fuente$id <- 1:nrow(Visitas_Por_Fuente) #Create the ID column.
Visitas_Por_Fuente %>%
ggvis(~Fuentes, ~sessions) %>%
layer_bars(width = 0.8, fill = ~Fuentes) %>%
add_tooltip(mysessions ,"hover")
mysessions <- function(x) {
if(is.null(x)) return(NULL)
#notice below the id column is how ggvis can understand which session to show
row <- Visitas_Por_Fuente[Visitas_Por_Fuente$id == x$id, ]
#prettyNum shows the number with thousand-comma separator
paste0("Sessions:", " ",prettyNum(row$sessions, big.mark=",",scientific=F))
}
The graph is shown, but when hovering says:
Warning: Unhandled error in observer: non-character argument
observe({
value <- session$input[[id]]
if (is.null(value))
return()
if (!is.list(value$data))
return()
df <- value$data
class(df) <- "data.frame"
attr(df, "row.names") <- .set_row_names(1L)
fun(data = df, location = list(x = value$pagex, y = value$pagey),
session = session)
})
My data:
structure(list(Fuentes = structure(c(3L, 5L, 6L, 6L, 4L, 5L,
5L, 5L, 5L, 7L, 7L, 1L, 6L, 7L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 7L, 3L, 5L, 6L, 6L, 5L, 6L, 5L, 5L, 5L, 5L, 7L, 7L, 6L,
1L, 6L, 5L, 5L, 4L, 5L, 5L, 4L, 6L, 5L, 5L, 5L, 5L, 7L, 3L, 5L,
6L, 6L, 4L, 6L, 5L, 5L, 4L, 4L, 5L, 7L, 7L, 6L, 7L, 5L, 4L, 5L,
4L, 2L, 2L, 6L, 5L, 5L, 5L, 6L, 7L, 3L, 6L, 4L, 6L, 4L, 5L, 5L,
5L, 5L, 4L, 5L, 7L, 7L, 1L, 6L, 7L, 5L, 5L, 4L, 5L, 5L, 4L, 2L,
2L, 5L, 5L, 5L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, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 3L, 6L, 6L, 5L, 5L,
7L, 6L, 5L, 5L, 5L, 5L, 7L, 5L, 4L, 2L, 5L, 5L, 5L, 5L, 5L, 6L,
7L, 3L, 4L, 6L, 5L, 5L, 4L, 5L, 5L, 7L, 1L, 6L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 4L, 4L, 5L, 6L, 3L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 7L,
6L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 3L, 6L, 4L, 6L, 5L, 5L, 7L, 7L,
5L, 6L, 5L, 5L, 5L, 5L), .Label = c("Adwords", "Campañas", "Directo",
"Email", "Referencias", "SEO", "Social Media"), class = "factor"),
sessions = c(197L, 1L, 7L, 13L, 1L, 1L, 10L, 1L, 3L, 3L,
5L, 3L, 566L, 1L, 27L, 159L, 7L, 1L, 6L, 1L, 1L, 4L, 1L,
6L, 10L, 129L, 1L, 7L, 2L, 1L, 10L, 1L, 5L, 6L, 9L, 1L, 28L,
1L, 7L, 386L, 1L, 146L, 1L, 89L, 41L, 9L, 1L, 1L, 1L, 6L,
3L, 4L, 182L, 1L, 5L, 8L, 2L, 1L, 1L, 4L, 1L, 1L, 2L, 3L,
2L, 524L, 4L, 26L, 1L, 152L, 4L, 2L, 3L, 1L, 2L, 2L, 1L,
5L, 10L, 142L, 1L, 1L, 8L, 1L, 3L, 1L, 1L, 1L, 1L, 7L, 4L,
13L, 3L, 375L, 3L, 2L, 147L, 1L, 101L, 29L, 4L, 1L, 1L, 2L,
3L, 1L, 1L, 2L, 1L, 7L, 5L, 5L, 224L, 3L, 12L, 1L, 7L, 2L,
1L, 4L, 141L, 4L, 632L, 2L, 2L, 32L, 1L, 138L, 1L, 1L, 9L,
5L, 1L, 1L, 1L, 2L, 1L, 6L, 3L, 139L, 4L, 1L, 9L, 1L, 1L,
5L, 9L, 8L, 36L, 1L, 537L, 1L, 2L, 5L, 3L, 174L, 1L, 106L,
39L, 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 68L, 1L, 1L, 4L,
2L, 1L, 2L, 1L, 1L, 241L, 1L, 1L, 4L, 9L, 37L, 1L, 1L, 66L,
1L, 1L, 7L, 5L, 4L, 2L, 1L, 2L, 197L, 47L, 39L, 19L, 1L)), .Names = c("Fuentes",
"sessions"), class = "data.frame", row.names = c(NA, -1724L))
In general, you need to give the layer a "key" to be returned when hovering or clicking it which is then used as input for the tooltip function.
A problem I see here is that you are producing a bar chart (i.e. values are summed up per "Fuente" type) but you want to use a tooltip for each single observation (row) in your data. So the problem is that in your chart you don't display each data point (observation) separated and hence it will be difficult, when hovering over a bar, to know what specific data point (observation) you want to return for the tooltip.
In order to show how it might work for layer_points with observation-specific tooltips, I adapted your code like this:
Visitas_Por_Fuente$id <- 1:nrow(Visitas_Por_Fuente) #Create the ID column.
mysessions <- function(x) {
if(is.null(x)) return(NULL)
# get the current session info, based on "id" that is hovered over:
current_session <- Visitas_Por_Fuente[Visitas_Por_Fuente$id == x$id, "sessions"]
# format the value with prettyNum if you like:
paste0("Sessions:", " ",prettyNum(current_session, big.mark=",",scientific=F))
}
Visitas_Por_Fuente %>%
ggvis(~Fuentes, ~sessions) %>%
layer_points(fill = ~Fuentes, key := ~id) %>% # define a key
add_tooltip(mysessions ,"hover")
Here's another version with tool tips for a bar chart showing the total number of sessions per "Fuente" type when hovering over a bar (this is possible because it doesn't require to know what single data point is used - instead we use "Fuente" as key):
mysessions <- function(x) {
if(is.null(x)) return(NULL)
# compute the total number of sessions of the "Fuente" type that is hovered over
total_sessions <-
sum(Visitas_Por_Fuente[Visitas_Por_Fuente$Fuentes == x$Fuentes, "sessions"])
# format the value with prettyNum if you like:
paste0("Total number of Sessions:", " ",
prettyNum(total_sessions, big.mark=",",scientific=F))
}
Visitas_Por_Fuente %>%
ggvis(~Fuentes, ~sessions) %>%
layer_bars(width = 0.8, fill = ~Fuentes, key := ~Fuentes) %>%
add_tooltip(mysessions ,"hover")

Resources