Stuck on a "recycled leaving remainder" warning - r

I am having a tough time with a problem I keep encountering with the following code:
#require(gdata)
require(jfreels)
require(mondate)
require(lubridate)
require(gdata)
# source this file
asof=as.Date('2017-05-31')
gm_quarter_value<-quarter(asof)
gm_year_value<-year(asof)
# load in and format data ----- the block of code where error occurs.
history_original<-
read.xls("//mhistory.xlsx",sheet='Allocation',na.strings="N/A")
mh<-data.table(history_original)
mh$date<-as.Date(mh$date)
setkey(mh,'date','mani')
mh[mani!='Cash',vami:=vami(return),by=fixed.name]
#load(file='data/history/mh.rda')
I have attempted to fix the error by reading up online but nothing seems to work as all explanations infer I am creating a data table (dt. ) rather than importing a data table through an Excel file.
The warning messages that popup from my code are the following:
Warning messages:
1: In `[.data.table`(cgmh, strategy != "Cash", `:=`(vami, vami(return)), :
Supplied 145 items to be assigned to group 3 of size 146 in column 'vami' (recycled leaving remainder of 1 items).
2: In `[.data.table`(mh, mani != "Cash", `:=`(vami, vami(return)), :
Supplied 71 items to be assigned to group 21 of size 72 in column 'vami' (recycled leaving remainder of 1 items).
3: In `[.data.table`(mh, mani != "Cash", `:=`(vami, vami(return)), :
Supplied 44 items to be assigned to group 40 of size 45 in column 'vami' (recycled leaving remainder of 1 items).
4: In `[.data.table`(mh, mani!= "Cash", `:=`(vami, vami(return)), :
Supplied 27 items to be assigned to group 41 of size 28 in column 'vami' (recycled leaving remainder of 1 items).
Here is some of the data from dput(head(mh, 50))
structure(list(date = structure(c(12752, 12752, 12752, 12752,
12752, 12752, 12752, 12752, 12752, 12783, 12783, 12783, 12783,
12783, 12783, 12783, 12783, 12783, 12783, 12783, 12814, 12814,
12814, 12814, 12814, 12814, 12814, 12814, 12814, 12814, 12814,
12814, 12842, 12842, 12842, 12842, 12842, 12842, 12842, 12842,
12842, 12842, 12842, 12842, 12873, 12873, 12873, 12873, 12873,
12873), class = "Date"), fixed.name = structure(c(17L, 18L, 46L,
47L, 70L, 4L, 60L, 59L, 69L, 17L, 18L, 8L, 46L, 47L, 70L, 4L,
24L, 60L, 59L, 69L, 17L, 17L, 8L, 46L, 47L, 70L, 4L, 24L, 60L,
59L, 69L, 73L, 17L, 17L, 8L, 46L, 47L, 70L, 4L, 24L, 60L, 59L,
69L, 73L, 17L, 17L, 60L, 8L, 46L, 47L), .Label = c("1",
"2", "3", "4", "5", "6", "7", etc. class = "factor"),
manager = structure(c(24L, 44L, 62L, 70L, 71L, 5L, 43L, 85L,
99L, 24L, 44L, 12L, 62L, 70L, 71L, 5L, 32L, 43L, 85L, 99L,
24L, 27L, 12L, 62L, 70L, 71L, 7L, 32L, 43L, 85L, 101L, 107L,
24L, 27L, 12L, 62L, 70L, 71L, 7L, 32L, 43L, 85L, 101L, 107L,
24L, 27L, 43L, 12L, 62L, 70L), .Label = c(lots of data here ), class = "factor"), strategy = structure(c(1L,
1L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 4L,
4L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L,
4L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L), .Label = c("Cash", "Discretionary", "Quantitative",
"Trend Follower"), class = "factor"), gross_allocation = c(NA,
NA, 0.142404915838961, 0.0474683052796535, 0.0474683052796535,
0.189873221118614, 0.113923932671168, 0.261393169848591,
0.189873221118614, NA, NA, 0.0732346506007515, 0.127227083260399,
0.0504539213350938, 0.0500494951829262, 0.150598347942872,
0.097646200801002, 0.102648545851004, 0.139406844436169,
0.146469301201503, NA, NA, 0.0712445227169108, 0.123964691130067,
0.0476800374705463, 0.0485758686645857, 0.145697372074475,
0.0928750257506969, 0.0983016698067595, 0.13403145511268,
0.142043313516008, 0.0946955423440056, NA, NA, 0.0835168624939674,
0.106588512365316, 0.0643739492830623, 0.066140094839865,
0.120819106420193, 0.079432110728629, 0.0819557073526206,
0.11538840144924, 0.166281136586948, 0.127042633304666, NA,
NA, NA, 0.105856694204315, 0.106336463704097, 0.0628120217517739
), net_allocation = c(0, 0, 0.143494748241449, 0.0478315827471496,
0.0478315827471496, 0.191326330988599, 0.114795798593159,
0.263393625693896, 0.191326330988599, 0, 0, 0.0780974349815385,
0.135674967809812, 0.0538040641787497, 0.0533727841103034,
0.160598085609968, 0.104129913308718, 0.109464414314636,
0.148663465723197, 0.156194869963077, 0, 0, 0.0713080226118237,
0.124075180253433, 0.047722534454923, 0.0486191640989275,
0.145827231430156, 0.0929578048072517, 0.0983892856046405,
0.134150916693587, 0.142169916027154, 0.0947799440181028,
0, 0, 0.082564194313903, 0.10537266827038, 0.0636396423266499,
0.0653856416444407, 0.11944093541624, 0.0785260368878884,
0.0810208471071804, 0.114072177933095, 0.164384384924578,
0.125593471175645, 0, 0, NA, 0.0983666777386705, 0.0988125005761266,
0.0583676823484184), attribution = c(-0.000443266946085618,
0, 0.010051776300819, 0.00317113437469883, 0.00238472696113082,
0.00802893915484529, 0.00972706935855351, 0.00968370136548388,
0, -0.000440070107589608, 0.000121585143389376, 0.000229810333585158,
0.000600276113576124, -0.00128820140991525, 3.99695193738757e-05,
-0.000361129826732395, -0.00187724283985821, -0.00128385273636259,
-0.0011990465227359, 0, -0.00036254125256041, 0.000633512231325974,
-0.000512960563561758, -0.00347960664253897, 0.00141283713223096,
0.00251341429314942, -0.00912636717243622, -0.00308688292194134,
-0.00566091419913372, -0.00359918817341097, -0.0014308969925891,
0.00156261612460105, -0.000632849158692302, 2.08436840409153e-05,
0.00133540685104398, -0.000313375712697167, -0.00159815288576456,
-0.00473252239700671, -0.0113934901937967, 0.000777818917761139,
-0.0012305250368858, 0.00126422585853451, 0.00915133063638428,
0.00468022553426879, -0.000688569622908879, 0.00112718749151131,
NA, -0.000994649965118036, 0.00325597011568602, 0.00780374962611487
), cash_change = structure(c(2143L, 1161L, 1202L, 1967L,
1777L, 2512L, 1181L, 1178L, 1161L, 22L, 1771L, 2164L, 1294L,
496L, 2492L, 1000L, 686L, 493L, 460L, 1161L, 755L, 1348L,
47L, 995L, 1895L, 2235L, 338L, 942L, 92L, 1018L, 566L, 1960L,
802L, 2200L, 1936L, 1010L, 678L, 61L, 515L, 1589L, 550L,
1899L, 1693L, 1236L, 2100L, 1819L, 1L, 438L, 2460L, 1592L
), .Label = c("", "-0.179999999993015", "-0.309999999999945",
"-1", "-1.61999999999898", "-1.74622982740402e-010", "-100068.974874801",
"-100235.36", "-100276", "-100319", "-100330.48", "-100416.569999998",
"-100940.009999999", "-1014043.74000001", "-101446.620000001",
"-101573.619999999", "-101748.387580004", "-1018471.4", "-101919.760000002",
"-102261.04", "-102748.5", "-102775.78", "-103320.27", "-104101.220000001",
"-104371.649999999", "-104377.770000001", "-104541.310000001",
"-104628.85", "-104824.74", "-10491.2799999993", "-104981.119999999",
"-10503.46", "-1050639.386", "-105344.909999999", "-105556.61794",
"-105789.020000001", "-105974.700000001", "-106033.25", "-1064338.6",
"-106699.800000001", "-107116.92676", "-107245", "-107366.670000001",
"-1075850.947517", "-108219.72", "-10825.3", "-10833.8904",
"-1086.06", "-109098.68", "-109781", "-110030", "-110055.35",
"-110720.6", "-110939.54", "-111091.300000001", "-111159.81",
"-112.5", "-112523.399999999", "-112567.65", "-112924.3754",
"-112983.76", "-113459", "-11359.29", "-1137.7200000002",
"-113724.59", "-114064.922", "-114153.33", "-114339", "-1148311.78",
"-11499.2599999998", "-115498", "-115633.560000002", "-115669",
"-116181.349999999", "-116263.77", "-116345.49", "-116450.220000001",
"-1165174.434", "-116681.39", "-1169.39000000013", "-117.260000000359",
"-117.820000000007", "-117650.72", "-117976", "-118088.83",
"-118127", "-118366.880000001", "-118910.431612", "-11904.6899999995",
"-119076.620000001", "-119227.389999999", "-119560.31", "-119656",
"-119847.8272", "-12015", "-120228.99", "-121215.200000001",
"-12130.3399999999", "-121305.060000001", "-121359.48", "-121372",
"-12180.6916476834", "-121957.979999999", "-122016.87", "-122204",
"-122583.029999999", "-122763.76", "-12296.059", "-1231586.02",
"-12317.6600000001", "-123233.93", "-123985.18", "-12435.3400000001",
"-1243584.30698176", "-124386.42", "-124440.090000002", "-124663.23",
"-124749.83", "-124793.56", "-125286.95", "-125752.57", "-125991.149999999",
"-126087.13", "-126237.84", "-12664.3299999998", "-12678.8099999996",
"-127087.609999999", "-127365.219999999", "-127413.159999999",
"-127539", "-127599.52", "-127817", "-127918.43", "-128162.890000001",
"-128527.47", "-128537.842920001", "-12891.7100000009", "-1289347.356",
"-130006.02", "-1303381.25", "-13075.7300000004", "-132065.08",
"-132862", "-1332167", "-133229.1", "-133275.790000001",
"-133338.311793", "-133503.7", "-1335110.79028567", "-1337.90000000037",
"-13387.7999999998", "-133920.21", "-134013.22", "-13407",
"-134145.310000001", "-134485", "-134609", "-134663.096483",
"-134869.25", "-134924.48", "-13493", "-135069.81", "-13507.7385",
"-135282.92", "-135678", "-135838.550345", "-135966.47",
"-135980.300000001", "-136106.209999999", "-136170.24", "-136216.82",
"-13623.1699999999", "-136498.119999999", "-136566.42", "-13666.98",
"-13692.48", "-136986.177008999", "-137760.81", "-137788.919999999",
"-13814.404000001", "-138491.18", "-138764.109999999", "-139008.85",
"-139111.35", "-139225.75", "-14007.25", "-140087.68", "-140461",
"-140480.869999999", "-140559.73", "-140789.98", "-141094",
"-141172.939999999", "-141442.25", "-14176.7831290001", "-142115.64",
"-1422133.03", "-142394.86", "-14317.4900000002", "-143382.365999999",
"-143496.060000001", "-143889.82", "-144339.269999998", "-145025.73",
"-145032.54", "-145230.810000001", "-14555.0599999996", "-1463.14999999991",
"-1467083.61", "-147.699999999721", "-147371", "-1478130.07",
"-148036.200000003", "-1484752.36627733", "-148638", "-14909.0599999996",
"-1495.34999999963", "-149909.27", "-150808.970000001", "-151104",
"-151515", "-151554.5", "-151600.35", "-151761.93", "-152666.139999999",
"-1527.53999999957", "-153321.930489", "-153351.430000002",
"-153826.57", "-154261.23", "-154344.79", "-154366.819999998",
"-155071.159999999", "-1552270", "-155300.539999999", "-155664.83",
"-156528.430000002", "-157231.1", "-157309.49", "-157588.36",
"-157756.130000001", "-15811.5100000007", "-158116", "-158301.819999998",
"-15851.169999999", "-158916.79", "-15900.6599999992", "-159261.640000001",
"-159841.81", "-16028.8700000005", "-16041.0600000005", "-160788.28",
"-160931.31", "-161112.47", "-161391.54", "-161571.82", "-162772",
"-163080.23", "-1631776.18961502", "-16370.216", "-163878.1142",
"-16407.5000000009", "-164132.29", "-164310.990000002", "-16477.2000000002",
"-164922", "-1654145.89", "-16681.0199999996", "-166813.67",
"-167432", "-16806.3499999996", "-168587.42", "-168676.27",
"-168736.3815498", "-169282.720000001", "-169582.54", "-169808.25",
"-169880.369999999", "-17057.9499999993", "-171058.639999999",
"-17214.3399999999", "-172574", "-1727156.3", "-17324.3899999999",
"-1733011.377417", "-173827.036639996", "-173844.989699999",
"-174051.67", "-174113", "-174613.67", "-174662.9", "-175170.82",
"-175250.83", "-175571.09", "-176099.709999999", "-176272.018000001",
"-176316.539999999", "-176970.13", "-177463.85", "-177831.319999999",
"-178211.209999999", "-178706.12", "-179790.199999997", "-180461.18",
"-18086.96", "-18128.4466440002", "-181300.99", "-181974.76",
"-182119.993906", "-183227.55", "-18338", "-18377", "-184355.62",
"-184513.06", "-1851550.63567736", "-185359.37", "-186026.2",
"-18608.7799999993", "-18620.5700000003", "-186253.428",
"-186376", "-187126.5761172", "-187427", "-18747", "-18759.1000000006",
"-18766.4999999981", "-18767.1799999997", "-189286.97", "-1895.0700000003",
"-190057.699999999", "-190305.23", "-19074", "-190989.26",
"-191427.800000001", "-192097.08", "-192347.11", "-192681.0327519",
"-192751.78", "-193205.17", "-194234.0916", "-1943373.03",
"-194383", "-195419.41", "-19568.4000000004", "-196017.789999999",
"-196032.99", "-196138.35", "-196535.11", "-196541.58", "-196720",
"-196810.279999999", "-197097.19", "-197401.92", "-1982330.27000001",
"-19880", "-19903.3842000002", "-199137.73", "-199310.61",
"-200074.399999999", "-20135.3700000001", "-201950.129999999",...... NA,
NA, NA, 1.00175057961823, 1.07428786780012, 1.1734795592287
)), .Names = c("date", "name", "job", "mani",
"gross_allocation", "net_allocation", "attribution", "cash_change",
"return", "vami"), sorted = c("date", "strategy"), class = c("data.table",
"data.frame"), row.names = c(NA, -50L), .internal.selfref = <pointer: 0x0000000000120788>)
I am fairly new to coding and would appreciate any feedback to help learn more about this warning. Is it the underlying Excel sheet or the code that is the problem?

Related

Widening Data and Changing Columns

I have managed to delete a little bit of code that did the below task and can't for the life of me figure out how I did it before.
I want to widen the data that has two factors spread over 8 different 'waves'. There are four 'Paper' factors, each with the same four internal factors 'Response'. The output from a previously required function gives the following dataframe:
[
And I would like to make it look like this:
The single column of the first tibble has become the single row of the second tibble.
As you can see, the second tibble has extra factors of Paper but these can just be joined row wise.
I really wasn't sure how to attack this, but thought it would be done using the pivot_wider function. When I tried
times_correct <- times_19 %>%
pivot_wider( id_cols = c('Stay/remain in the EU`', 'Leave the EU', 'I would/will not vote', 'Don\'t know'), names_from = eurrefcolnames)
I got the error that I can't subset columns that don't exist which makes sense: I need to manually add the correct 'Waves'. I think this is relatively simple, but can't for the life of me figure out how I did it!
Here is the dput of the various tibbles:
structure(list(resp = structure(c(3L, 2L, 4L, 1L, NA, NA, NA,
NA), .Label = c("Don't Know", "Leave", "Remain", "Will Not Vote"
), class = "factor"), `Stay/remain in the EU` = c(316L, 290L,
313L, 324L, 338L, 320L, 325L, 335L), `Leave the EU` = c(157L,
123L, 159L, 154L, 134L, 189L, 187L, 181L), `I would/will not vote` = c(2L,
3L, 3L, 3L, 2L, 2L, 2L, 0L), `Don't know` = c(56L, 51L, 55L,
50L, 57L, 20L, 17L, 0L), Paper = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "Times", class = "factor")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
structure(list(resp = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L,
1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("Don't Know",
"Leave", "Remain", "Will Not Vote"), class = "factor"), euRefVoteW1 = c(316L,
157L, 2L, 56L, 190L, 339L, 4L, 70L, 819L, 79L, 9L, 71L, 1294L,
1311L, 150L, 523L, 1715L, 2587L, 133L, 630L), euRefVoteW2 = c(290L,
123L, 3L, 51L, 175L, 282L, 3L, 62L, 777L, 74L, 5L, 62L, 1091L,
925L, 80L, 371L, 1528L, 2044L, 83L, 517L), euRefVoteW3 = c(313L,
159L, 3L, 55L, 199L, 334L, 4L, 69L, 835L, 81L, 10L, 57L, 1348L,
1289L, 139L, 508L, 1766L, 2563L, 156L, 586L), euRefVoteW4 = c(324L,
154L, 3L, 50L, 215L, 328L, 2L, 61L, 848L, 70L, 10L, 55L, 1397L,
1267L, 128L, 492L, 1853L, 2494L, 143L, 583L), euRefVoteW6 = c(338L,
134L, 2L, 57L, 241L, 286L, 2L, 77L, 853L, 68L, 5L, 57L, 1519L,
1133L, 112L, 520L, 2017L, 2284L, 106L, 667L), euRefVoteW7 = c(320L,
189L, 2L, 20L, 186L, 384L, 2L, 34L, 832L, 109L, 8L, 34L, 1449L,
1456L, 87L, 292L, 1906L, 2785L, 55L, 328L), euRefVoteW8 = c(325L,
187L, 2L, 17L, 187L, 384L, 1L, 34L, 836L, 118L, 5L, 24L, 1462L,
1522L, 72L, 228L, 1898L, 2852L, 56L, 268L), euRefVoteW9 = c(335L,
181L, 0L, 0L, 206L, 385L, 0L, 6L, 844L, 102L, 0L, 4L, 1572L,
1462L, 0L, 21L, 2018L, 2827L, 0L, 20L), Paper = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L), .Label = c("Times", "Telegraph", "Control", "No_Paper",
"Rest"), class = "factor")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
eurrefcolnames = c('euRefVoteW1','euRefVoteW2', 'euRefVoteW3', 'euRefVoteW4', 'euRefVoteW6',' euRefVoteW7', 'euRefVoteW8', 'euRefVoteW9')
EDIT:
Here is the function that create the initial dataframes, is there an edit I could make here perhaps ?
tally_reader_number <- function(input_dataframe,newspaper_name) {
#function takes the input of in_all_waves, tallies the number of different eu ref responses using map_df for a given newspaper factor (defined above)
# and returns a dataframe of responese for each wave with the newspaper factor as a column
returned_dataframe <- input_dataframe %>%
filter(Paper == newspaper_name) %>%
ungroup() %>% #function refuses to work without this
select(-Paper) %>%
map_df(table) %>% # use map_df from the purrr package to "table" each column
rownames_to_column("response") %>% #convert the rownames to a column named response
mutate(resp = case_when(response == 1 ~ "Remain", #change the resulting numbers to the correct responses
response == 2 ~ "Leave",
response ==3 ~ "Will Not Vote",
response == 4 ~ "Don't Know")) %>%
select(resp, everything(), -response) %>% #reorder the columns with resp at the front, removing response
mutate(Paper = newspaper_name)
returned_dataframe$Paper <- as.factor(returned_dataframe$Paper)
returned_dataframe$resp <- as.factor(returned_dataframe$resp)
returned_dataframe
}

How can I add a legend to this graph?

I have a graph that I would like to add a legend to:
So far I have been able to either add a legend which defaults to its own colours, or one which changes the colours to a gradient fill which isn't what I want.
What am I doing wrong and how can I fix it?
Plot code:
ggplot(df) +
geom_line(aes(date, actual, group = 1),
colour = "forestgreen") +
geom_line(aes(date, plan, group = 1),
colour = "red") +
geom_smooth(aes(date, difference),
method = "auto",
se = FALSE,
linetype = "dashed") +
scale_x_date(date_breaks = "4 days" , date_labels = "%d-%b-%y") +
facet_grid(sector ~ ., scales = "free")
dput:
structure(list(date = structure(c(17959, 17959, 17959, 17959,
17959, 17960, 17960, 17960, 17960, 17960, 17961, 17961, 17961,
17961, 17961, 17962, 17962, 17962, 17962, 17962, 17963, 17963,
17963, 17963, 17963, 17964, 17964, 17964, 17964, 17964, 17965,
17965, 17965, 17965, 17965, 17966, 17966, 17966, 17966, 17966,
17967, 17967, 17967, 17967, 17967, 17968, 17968, 17968, 17968,
17968, 17969, 17969, 17969, 17969, 17969, 17970, 17970, 17970,
17970, 17970, 17971, 17971, 17971, 17971, 17971, 17972, 17972,
17972, 17972, 17972, 17973, 17973, 17973, 17973, 17973, 17974,
17974, 17974, 17974, 17974, 17975, 17975, 17975, 17975, 17975,
17976, 17976, 17976, 17976, 17976, 17977, 17977, 17977, 17977,
17977, 17978, 17978, 17978, 17978, 17978, 17979, 17979, 17979,
17979, 17979, 17980, 17980, 17980, 17980, 17980, 17981, 17981,
17981, 17981, 17981, 17982, 17982, 17982, 17982, 17982, 17983,
17983, 17983, 17983, 17983, 17984, 17984, 17984, 17984, 17984,
17985, 17985, 17985, 17985, 17985, 17986, 17986, 17986, 17986,
17986, 17987, 17987, 17987, 17987, 17987, 17988, 17988, 17988,
17988, 17988, 17989, 17989, 17989, 17989, 17989, 17990, 17990,
17990, 17990, 17990, 17991, 17991, 17991, 17991, 17991, 17992,
17992, 17992, 17992, 17992, 17993, 17993, 17993, 17993, 17993
), class = "Date"), sector = structure(c(1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("ADVANCE CLINICIAN",
"CALL HANDLERS", "CLINICIANS", "GP", "PHARMACIST"), class = "factor"),
actual = c(16L, 232L, 104L, 40L, 12L, 23L, 231L, 90L, 47L,
12L, 34L, 245L, 109L, 61L, 0L, 32L, 226L, 99L, 50L, 0L, 25L,
247L, 103L, 58L, 7L, 84L, 362L, 89L, 57L, 8L, 50L, 333L,
86L, 44L, 11L, 14L, 276L, 71L, 68L, 7L, 24L, 263L, 93L, 62L,
12L, 42L, 241L, 92L, 42L, 13L, 42L, 242L, 106L, 60L, 8L,
50L, 262L, 101L, 58L, 8L, 38L, 340L, 80L, 42L, 0L, 32L, 312L,
73L, 39L, 8L, 14L, 219L, 81L, 54L, 15L, 19L, 239L, 100L,
50L, 20L, 15L, 245L, 104L, 58L, 13L, 38L, 233L, 90L, 57L,
8L, 50L, 236L, 94L, 41L, 7L, 78L, 370L, 106L, 61L, 8L, 34L,
328L, 91L, 45L, 8L, 16L, 247L, 54L, 57L, 20L, 19L, 263L,
93L, 43L, 15L, 26L, 231L, 88L, 58L, 13L, 50L, 234L, 87L,
61L, 8L, 68L, 245L, 113L, 50L, 15L, 68L, 352L, 89L, 38L,
8L, 22L, 288L, 98L, 18L, 11L, 13L, 201L, 34L, 40L, 8L, 12L,
208L, 41L, 45L, 8L, 5L, 215L, 48L, 45L, 0L, 53L, 210L, 45L,
55L, 0L, 20L, 220L, 71L, 23L, 8L, 12L, 349L, 80L, 54L, 8L,
2L, 287L, 52L, 35L, 0L), plan = c(43L, 291L, 28L, 32L, 11L,
37L, 262L, 25L, 32L, 11L, 39L, 260L, 22L, 32L, 11L, 34L,
266L, 90L, 32L, 11L, 39L, 269L, 93L, 32L, 11L, 66L, 422L,
152L, 61L, 14L, 54L, 373L, 133L, 53L, 14L, 43L, 291L, 106L,
32L, 11L, 37L, 262L, 90L, 32L, 11L, 39L, 260L, 94L, 32L,
11L, 34L, 266L, 90L, 32L, 11L, 39L, 269L, 93L, 32L, 11L,
66L, 422L, 152L, 61L, 14L, 54L, 373L, 133L, 53L, 14L, 43L,
291L, 106L, 32L, 11L, 37L, 262L, 90L, 32L, 11L, 39L, 260L,
94L, 32L, 11L, 34L, 266L, 90L, 32L, 11L, 39L, 269L, 93L,
32L, 11L, 66L, 422L, 152L, 61L, 14L, 54L, 373L, 133L, 53L,
14L, 43L, 291L, 106L, 32L, 11L, 37L, 262L, 90L, 32L, 11L,
39L, 260L, 94L, 32L, 11L, 34L, 266L, 90L, 32L, 11L, 39L,
269L, 93L, 32L, 11L, 66L, 422L, 152L, 61L, 14L, 54L, 373L,
133L, 53L, 14L, 43L, 283L, 73L, 32L, 11L, 37L, 283L, 99L,
32L, 11L, 39L, 264L, 88L, 32L, 11L, 34L, 260L, 90L, 32L,
11L, 39L, 275L, 98L, 32L, 11L, 66L, 424L, 156L, 61L, 14L,
54L, 360L, 130L, 53L, 14L), difference = c(-27L, -59L, 76L,
8L, 1L, -14L, -31L, 65L, 15L, 1L, -5L, -15L, 87L, 29L, -11L,
-2L, -40L, 9L, 18L, -11L, -14L, -22L, 10L, 26L, -4L, 18L,
-60L, -63L, -4L, -6L, -4L, -40L, -47L, -9L, -3L, -29L, -15L,
-35L, 36L, -4L, -13L, 1L, 3L, 30L, 1L, 3L, -19L, -2L, 10L,
2L, 8L, -24L, 16L, 28L, -3L, 11L, -7L, 8L, 26L, -3L, -28L,
-82L, -72L, -19L, -14L, -22L, -61L, -60L, -14L, -6L, -29L,
-72L, -25L, 22L, 4L, -18L, -23L, 10L, 18L, 9L, -24L, -15L,
10L, 26L, 2L, 4L, -33L, 0L, 25L, -3L, 11L, -33L, 1L, 9L,
-4L, 12L, -52L, -46L, 0L, -6L, -20L, -45L, -42L, -8L, -6L,
-27L, -44L, -52L, 25L, 9L, -18L, 1L, 3L, 11L, 4L, -13L, -29L,
-6L, 26L, 2L, 16L, -32L, -3L, 29L, -3L, 29L, -24L, 20L, 18L,
4L, 2L, -70L, -63L, -23L, -6L, -32L, -85L, -35L, -35L, -3L,
-30L, -82L, -39L, 8L, -3L, -25L, -75L, -58L, 13L, -3L, -34L,
-49L, -40L, 13L, -11L, 19L, -50L, -45L, 23L, -11L, -19L,
-55L, -27L, -9L, -3L, -54L, -75L, -76L, -7L, -6L, -52L, -73L,
-78L, -18L, -14L)), row.names = c(NA, -175L), class = c("tbl_df",
"tbl", "data.frame"))
Place the colour argument in aes and just write the label for it there. You can rename the title with +labs(colour="Legend Title"), and manipulate the colours with scale_fill_manual(values)
ggplot(df) +
geom_line(aes(date, actual, group = 1,colour = "actual")
) +
geom_line(aes(date, plan, group = 1,colour ="plan")) +
geom_smooth(aes(date, difference,colour="difference"),
method = "auto",
se = FALSE,
linetype = "dashed") +
scale_x_date(date_breaks = "4 days" , date_labels = "%d-%b-%y")+
facet_grid(sector ~ ., scales = "free")+
labs(colour="Method")+
scale_colour_manual(values=c("forestgreen","red","blue"))
Hope this is helpful!

order geom_point by specific facet

I have a ggplot related question, which should be easy but I could not find the answer yet. I am trying to plot a faceted plot with the code below and this dataset (11 kB).
ggplot(plot.dat, aes(x = estimate, y = reorder(countryyear, estimate))) +
geom_point() +
geom_segment(aes(x=conf.low, xend=conf.high, yend=countryyear)) +
facet_grid(. ~ facet) +
xlab("Random Effect Estimate") +
ylab("") + scale_x_continuous(breaks=c(seq(0, 5, 1)), limits=c(0, 5)) +
ggtitle("Random Slopes in Country*Year Groups from Northwestern Europe") +
theme_minimal() + theme(plot.title = element_text(hjust = 0.5))
I would like countryyear to be organized by the values of estimate in the Extreme Right facet. Not quite sure how to order by values of a specific facet. Any ideas are welcome! Thanks.
Update: Here is the dput structure of a random subset of the dataset. It has some missing values, but it should work for the sake of the example. I also updated the download link above, that has the full version.
structure(list(estimate = c(1.41056902925372, 0.854859208455895,
1.16012834593894, 0.871339033194504, 0.803272289946221, 1.17540386134493,
0.996313357490551, 1.49940694539732, 1.33773365908762, 2.7318703090905,
1.19131935418045, 1.12765907711738, 0.746741192261761, 0.985847015192172,
0.912357310925342, 1.11582763712164, 1.21854572824977, 0.675712547978394,
0.566955524699616, 1.32611743759365, 0.519648352294682, 0.591013596394243,
1.30944973684044, 0.613722269599125, 1.13293279727271, 0.950788678552604,
1.1599446923567, 1.11493952112913, 0.95336321045095, 1.39002327097034,
0.794207546872633, 0.788545101449259, 1.01096883872495, 0.897407203907834,
1.38391605229103, 1.35754760293107, 1.0718508539761, 0.542191158958878,
0.757132752456427, 1.44172863221312, 1.04842251986171, 0.77260404885379,
0.879288027642055, 1.09372353598088, 0.745484830381145, 1.21211217249353,
0.628009608902132, 1.34864488674734), countryyear = structure(c(1L,
2L, 4L, 5L, 7L, 9L, 10L, 12L, 13L, 26L, 28L, 29L, 31L, 32L, 34L,
36L, 37L, 39L, 40L, 57L, 59L, 60L, 62L, 63L, 65L, 67L, 68L, 70L,
71L, 73L, 75L, 76L, 89L, 90L, 92L, 94L, 95L, 103L, 104L, 106L,
108L, 109L, 111L, 128L, 130L, 132L, 133L, 135L), .Label = c("AT02",
"AT04", "AT06", "AT14", "AT16", "BE02", "BE04", "BE06", "BE08",
"BE10", "BE12", "BE14", "BE16", "BG06", "BG08", "BG10", "BG12",
"CH14", "CZ02", "CZ04", "CZ08", "CZ10", "CZ12", "CZ14", "CZ16",
"DE02", "DE04", "DE06", "DE08", "DE10", "DE12", "DE14", "DE16",
"DK02", "DK04", "DK06", "DK08", "DK10", "DK12", "DK14", "EE04",
"EE06", "EE08", "EE10", "EE12", "EE14", "EE16", "ES02", "ES04",
"ES06", "ES08", "ES10", "ES12", "ES14", "ES16", "FI02", "FI04",
"FI06", "FI08", "FI10", "FI12", "FI14", "FI16", "FR06", "FR08",
"FR10", "FR12", "FR14", "FR16", "GB02", "GB04", "GB06", "GB08",
"GB10", "GB12", "GB14", "GB16", "GR02", "GR04", "GR08", "GR10",
"HU02", "HU06", "HU08", "HU10", "HU12", "HU14", "HU16", "IE02",
"IE04", "IE06", "IE08", "IE10", "IE12", "IE14", "IE16", "IT04",
"IT12", "IT16", "LT10", "LT12", "LT14", "NL02", "NL04", "NL06",
"NL08", "NL10", "NL12", "NL14", "NL16", "NO14", "PL02", "PL04",
"PL06", "PL08", "PL10", "PL12", "PL14", "PL16", "PT02", "PT04",
"PT06", "PT08", "PT10", "PT12", "PT14", "PT16", "SE02", "SE04",
"SE06", "SE08", "SE10", "SE12", "SE14", "SE16", "SI02", "SI04",
"SI06", "SI08", "SI10", "SI12", "SI14", "SI16", "SK04", "SK06",
"SK08", "SK10", "SK12"), class = "factor"), facet = structure(c(1L,
3L, 1L, 4L, 5L, 3L, 4L, 1L, 1L, 1L, 5L, 5L, 4L, 5L, 3L, 1L, 2L,
4L, 5L, 2L, 1L, 4L, 2L, 5L, 2L, 3L, 4L, 3L, 2L, 5L, 5L, 4L, 2L,
5L, 4L, 5L, 3L, 1L, 4L, 5L, 3L, 5L, 4L, 1L, 5L, 2L, 4L, 1L), .Label = c("Intercept",
"Extreme Left", "Center", "Right", "Extreme Right"), class = "factor"),
conf.low = c(1.16824810706745, 0.686215051613965, 0.910277310292764,
0.591705078386698, 0.37357342399703, 0.947951001435781, 0.663296044193037,
1.18794112232166, 1.06645119085865, 2.33578182814618, 0.580210898576738,
0.564235690522211, 0.530859530342114, 0.516191258265551,
0.730992343373883, 0.862424540370486, 0.827891784352444,
0.427638276259852, 0.275692447335368, 0.829763907986328,
0.370078643492081, 0.321852705445509, 0.83550621863293, 0.289836810427436,
0.847226120408727, 0.780056160572728, 0.873143885861924,
0.869757467125519, 0.615741777890997, 0.649483531741787,
0.349657606457465, 0.523294407847395, 0.670109418373736,
0.36656743494149, 0.952201390937053, 0.777207016700884, 0.888128473009524,
0.397085597526946, 0.479828726362257, 0.614533313431094,
0.813336887981082, 0.3129232351085, 0.61435321820328, 0.854801028643867,
0.346698059397102, 0.805414039007076, 0.434676644041643,
1.07780736338027), conf.high = c(1.70315275860739, 1.06494933995261,
1.47855797769819, 1.28312522319126, 1.7272277157504, 1.45743211956315,
1.49652679976667, 1.8925358720741, 1.67802460909168, 3.19512520208851,
2.44607918797515, 2.25369471581694, 1.05041423643869, 1.8828182806291,
1.13872035780431, 1.44368725318228, 1.79353596677755, 1.06769546329854,
1.16593171156554, 2.11938292490653, 0.729667639003753, 1.08526995489865,
2.05223919950836, 1.29954170985538, 1.51498719434776, 1.15888977865399,
1.54095070825389, 1.4292376699955, 1.47610807594453, 2.97492484321718,
1.80395225460704, 1.18824770090216, 1.52521060717706, 2.19697554354282,
2.01136404338166, 2.37122858469145, 1.29357889999432, 0.740322123703373,
1.19469713534712, 3.38237391450413, 1.35145693795059, 1.90755095606211,
1.25847381058047, 1.39942645489832, 1.60297301142912, 1.82417470710871,
0.907332092210651, 1.68753999308876)), row.names = c(1L,
9L, 17L, 25L, 33L, 41L, 49L, 57L, 65L, 128L, 136L, 144L, 152L,
160L, 168L, 176L, 184L, 192L, 200L, 283L, 291L, 299L, 307L, 315L,
323L, 331L, 339L, 347L, 355L, 363L, 371L, 379L, 442L, 450L, 458L,
466L, 474L, 512L, 520L, 528L, 536L, 544L, 552L, 640L, 648L, 656L,
664L, 672L), class = "data.frame")

R - Lattice xyplot - How do you add error bars to groups and summary lines?

I'm posting this question because the very similar question here has not been answered until now.
I have been asked to plot the mean +/- SEM of my whole cohort of patients over the xyplot() that depicts the values of all patients. The data used represents intraoperative cardiovascular findings from patients undergoing surgery.
This is my data.frame called df
dput(df)
structure(list(Name = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("DE", "JS", "KG", "MK", "TG", "WT"), class = "factor"),
Time = structure(c(1L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L,
4L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 2L, 3L, 4L, 5L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L), .Label = c("T1", "T2", "T3", "T4", "T5", "T6", "T7",
"T8"), class = "factor"), Dobut = structure(c(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, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("No", "Yes"
), class = "factor"), DobutDose = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
4L, 6L, 8L, 8L, 8L, 8L, 8L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 5L, 5L, NA), CI = c(1.4, 2.3, 1.3, 1.8, 2.1,
2, 2.1, 2.1, 2.3, 1.9, 1.6, 2, 2.4, 2.7, 2.6, 2.7, 2.6, 2.3,
2.4, 2.6, 0.9, 2.5, 2.1, 1.6, 1.5, 1.8, 2, 2, 1.9, 2.1, 2.3,
2, 2.4, 2.3, 2.6, 2.4, 2, 2.2, 1.6, 2.1, 2.5, 2.8), SvO2 = c(57L,
65L, 47L, 45L, 51L, 60L, 56L, 70L, 85L, 75L, 79L, 82L, 73L,
77L, 78L, 73L, 71L, 73L, 80L, 74L, 41L, 66L, 51L, 51L, 49L,
54L, 68L, 48L, 80L, 70L, 71L, 69L, 74L, 79L, 77L, 77L, 75L,
74L, 70L, 79L, 80L, 79L), SVRI = c(4000L, 1983L, 4000L, 2444L,
1981L, 2120L, 2514L, 2971L, 2157L, 3747L, 4300L, 3200L, 2867L,
1778L, 1169L, 1215L, 1262L, 1461L, 1600L, 1692L, 4978L, 1760L,
2019L, 2650L, 2827L, 2356L, 1800L, 2840L, 2063L, 2248L, 1948L,
2160L, 1733L, 2296L, 2677L, 2100L, 2640L, 2655L, 3950L, 2210L,
2848L, 2543L), MAP = c(80L, 65L, 86L, 74L, 67L, 65L, 74L,
90L, 70L, 90L, 96L, 94L, 100L, 82L, 60L, 61L, 62L, 62L, 69L,
71L, 70L, 71L, 77L, 73L, 75L, 77L, 61L, 85L, 65L, 74L, 70L,
67L, 69L, 74L, 92L, 71L, 88L, 93L, 89L, 79L, 97L, 97L), CVP = c(10L,
8L, 21L, 19L, 15L, 12L, 8L, 12L, 8L, 11L, 10L, 14L, 14L,
22L, 22L, 20L, 21L, 20L, 21L, 16L, 14L, 16L, 24L, 20L, 22L,
24L, 16L, 14L, 16L, 15L, 14L, 13L, 17L, 8L, 5L, 8L, 22L,
20L, 20L, 21L, 8L, 8L), PAP = c(23L, 22L, 36L, 36L, 34L,
32L, 22L, 33L, 28L, 36L, 36L, 40L, 37L, 37L, 40L, 35L, 35L,
34L, 38L, 36L, 45L, 43L, 55L, 49L, 52L, 54L, 43L, 47L, 27L,
25L, 23L, 22L, 28L, 21L, 20L, 25L, 33L, 33L, 38L, 35L, 33L,
29L), PCWP = c(15L, 11L, 28L, 26L, 23L, 21L, 11L, 26L, NA,
NA, 25L, 25L, NA, 27L, NA, NA, NA, NA, NA, NA, 30L, NA, NA,
NA, NA, NA, NA, NA, 19L, NA, NA, NA, NA, NA, 16L, NA, NA,
NA, NA, NA, NA, NA)), .Names = c("Name", "Time", "Dobut",
"DobutDose", "CI", "SvO2", "SVRI", "MAP", "CVP", "PAP", "PCWP"
), class = "data.frame", row.names = c(NA, -42L))
Now the first xyplot I made for the variable CI looks like this
require(lattice)
xyplot(CI~Time, groups=Name, data=df, ty=c("l", "p"),
+ ,xlab="Measurement Time Point",
ylab=expression("CI"~(l/min/m^"2")), main="Cardiac Index")
Now I was able to add the mean (black line) of the whole cohort, by doing the following
xyplot(CI~Time, groups=Name, data=df, ty=c("l", "p"),
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.linejoin(x, y, horizontal = FALSE,..., col="black", lty=1, lwd=4)
}
,xlab="Measurement Time Point",
ylab=expression("CI"~(l/min/m^"2")), main="Cardiac Index")
Now I'd like to add +/- SE to the mean as a line above/below the mean, but nowhere can I find how to do this.
What I can do is using the latticeExtra package is add the loess line +/- SE, as below, but that's not the correct mathematical function I'm looking for. I've left the mean line in there to illustrate the difference between the two.
require(latticeExtra)
xyplot(CI~Time, groups=Name, data=df, ty=c("l", "p"),
+ panel = function(x, y, ...) {
+ panel.xyplot(x, y, ...)
+ panel.linejoin(x, y, horizontal = FALSE,..., col="black", lty=1, lwd=4)
+ panel.smoother(x,y,se=TRUE, col.se="grey")
+ }
+ ,xlab="Measurement Time Point",
ylab=expression("CI"~(l/min/m^"2")), main="Cardiac Index")
I have performed an extensive search through SO and the internet, but I haven't been able to find the right function to do this.
Help is very much appreciated! Thanks.
You could create your own panel function to plot a +/- SD region. For example
#new panel function
panel.se <- function(x, y, col.se=plot.line$col, alpha.se=.25, ...) {
plot.line <- trellis.par.get("plot.line")
xs <- if(is.factor(x)) {
factor(c(levels(x) , rev(levels(x))), levels=levels(x))
} else {
xx <- sort(unique(x))
c(xx, rev(xx))
}
means <- tapply(y,x, mean, na.rm=T)
vars <- tapply(y,x, var, na.rm=T)
Ns <- tapply(!is.na(y),x, sum)
ses <- sqrt(vars/Ns)
panel.polygon(xs, c(means+ses, rev(means-ses)), col=col.se, alpha=alpha.se)
}
and then you can use it like
#include new panel function
xyplot(CI~Time, groups=Name, data=df, ty=c("l", "p"),
panel = function(x, y, ...) {
panel.se(x,y, col.se="grey")
panel.xyplot(x, y, ...)
panel.linejoin(x, y, horizontal = FALSE,..., col="black", lty=1, lwd=4)
}
,xlab="Measurement Time Point",
ylab=expression("CI"~(l/min/m^"2")), main="Cardiac Index")
which results in

Bubble Plot of Negative and Positive values in space ggplot2 R

I would like to make a bubble plot using ggplot2 in R. My code and data are found below.
Please leave the colors as they are. I am having difficulties in scaling positive and negative values equally. For example, -3 is scaled smaller than +3. I would like negatives and positives to be scaled proportionately irrespective of sign.
Identify negative from positive values using some kind of outline linetype for bubbles and include it in the legend.
Also remove the "Mean" part of the legend.
Thanks very much for your great help.
#=====================================================================
library(ggplot2)
if (dev.cur() == 1) x11(width=8,height=6)
par(mfcol=c(1,1))
p<-ggplot(site.resiudal, aes(x=Eastings, y=Northings, size=Mean,label=site.resiudal$Site,legend = FALSE))+
#theme(legend.position="none")+
geom_point(shape=21)+
geom_point(aes(colour = factor(Region)))+
scale_area(range=c(1,15))+
scale_alpha(guide = 'none')+
scale_x_continuous(name="Longitude", limits=c(-120,-95))+
scale_y_continuous(name="Latitude", limits=c(48,61))+
geom_text(size=4)+
scale_colour_manual(name="Region",labels = c("A", "B","C","D", "E"),values = c("1" = "firebrick3","2" = "palegreen4","3" = "sandybrown","4" = "red","5" = "gray0"))+
theme(legend.title = element_text(colour="black", size=16, face="plain"))+
theme(legend.text = element_text(colour="black", size = 16, face = "plain"))
p
#Data[["sign"]] = ifelse(Data[["Mean"]] >= 0, "positive", "negative")
#=================================================
structure(list(Site = structure(c(101L, 102L, 105L, 107L, 108L,
110L, 111L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 16L, 20L, 47L, 52L, 53L, 55L, 91L, 92L, 93L, 94L, 95L,
96L, 99L, 15L, 17L, 18L, 19L, 21L, 114L, 23L, 26L, 36L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L,
86L, 87L, 88L, 89L, 98L, 100L, 103L, 104L, 106L, 109L, 112L,
113L, 115L, 116L, 117L, 119L, 42L, 44L, 46L, 48L, 49L, 50L, 51L,
54L, 56L, 57L, 58L, 90L, 97L, 118L, 120L, 22L, 24L, 25L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 37L, 38L, 39L, 40L, 41L,
43L, 45L), .Label = c("G100", "G101", "G102", "G103", "G104",
"G105", "G106", "G107", "G108", "G109", "G110", "G111", "G112",
"G113", "G114", "G115", "G116", "G117", "G118", "G119", "G120",
"GG10", "GG11", "GG12", "GG13", "GG14", "GG15", "GG16", "GG17",
"GG18", "GG19", "GG20", "GG21", "GG22", "GG23", "GG24", "GG25",
"GG26", "GG27", "GG28", "GG29", "GG30", "GG31", "GG32", "GG33",
"GG34", "GG35", "GG36", "GG37", "GG38", "GG39", "GG40", "GG41",
"GG42", "GG43", "GG44", "GG45", "GG46", "GG47", "GG48", "GG49",
"GG50", "GG51", "GG52", "GG53", "GG54", "GG55", "GG56", "GG57",
"GG58", "GG59", "GG60", "GG61", "GG62", "GG63", "GG64", "GG65",
"GG66", "GG67", "GG68", "GG69", "GG70", "GG71", "GG72", "GG73",
"GG74", "GG75", "GG76", "GG77", "GG78", "GG79", "GG80", "GG81",
"GG82", "GG83", "GG84", "GG85", "GG86", "GG87", "GG88", "GG89",
"GG90", "GG91", "GG92", "GG93", "GG94", "GG95", "GG96", "GG97",
"GG98", "GG99", "GGG1", "GGG2", "GGG3", "GGG4", "GGG5", "GGG6",
"GGG7", "GGG8", "GGG9"), class = "factor"), Name = structure(c(53L,
87L, 29L, 92L, 36L, 76L, 102L, 103L, 119L, 2L, 9L, 11L, 45L,
47L, 49L, 54L, 90L, 30L, 105L, 66L, 78L, 107L, 81L, 42L, 41L,
43L, 59L, 110L, 24L, 27L, 56L, 61L, 64L, 118L, 40L, 21L, 44L,
70L, 108L, 25L, 58L, 98L, 83L, 5L, 19L, 26L, 31L, 38L, 55L, 60L,
71L, 74L, 75L, 85L, 95L, 120L, 109L, 1L, 67L, 20L, 50L, 63L,
106L, 111L, 116L, 62L, 6L, 99L, 114L, 73L, 84L, 89L, 93L, 97L,
115L, 80L, 10L, 12L, 88L, 79L, 15L, 17L, 33L, 35L, 94L, 100L,
3L, 16L, 37L, 101L, 117L, 8L, 39L, 48L, 86L, 113L, 23L, 13L,
69L, 96L, 104L, 32L, 65L, 82L, 14L, 22L, 18L, 46L, 68L, 72L,
77L, 91L, 112L, 4L, 7L, 28L, 51L, 57L, 52L, 34L), .Label = c("ANEROID",
"ARBORG", "ATHABASCA", "BANFF", "BANGOR", "BATTLEFORD", "BEAVER MINES",
"BEAVERLODGE", "BERENS RIVER", "BIRTLE", "BISSETT", "BRANDON",
"BUFFALO NARROWS", "CALGARY", "CALMAR", "CAMPSIE", "CAMROSE",
"CARWAY", "CEYLON", "CHAPLIN", "CHURCHILL", "CLARESHOLM", "COLD LAKE",
"COLLINS BAY", "CORONATION", "COTE", "CREE LAKE", "CROWSNEST",
"CYPRESS RIVER", "DAUPHIN", "DAVIDSON", "DRUMHELLER", "EDMONTON",
"EDSON", "ELK POINT", "EMERSON AUT", "ENILDA-BERG", "ESTEVAN",
"FAIRVIEW", "FLIN FLON", "FORT CHIPEWYAN", "FORT MCMURRAY", "FORT VERMILION",
"GILLAM", "GIMLI", "GLEICHEN", "GRAND RAPIDS", "GRANDE PRAIRIE",
"GREAT FALLS", "HIGH POINT", "HIGHWOOD", "HINTON VALLEY", "HUDSON BAY",
"INDIAN BAY", "INDIAN HEAD", "ISLAND FALLS", "JASPER WARDEN",
"JENNER", "KEG RIVER RS", "KELLIHER", "KEY LAKE", "KINDERSLEY",
"KLINTONEL", "LA RONGE", "LACOMBE 2", "LANGRUTH WEST", "LEADER",
"LETHBRIDGE", "LOON LAKE", "LYNN LAKE", "MANOR", "MEDICINE HAT",
"MELFORT", "MOOSE JAW", "MOOSOMIN", "MORDEN", "MOUNTAIN VIEW",
"NEEPAWA MURRAY", "NINETTE", "NIPAWIN", "NORWAY HOUSE", "OLDS",
"ONEFOUR", "OUTLOOK", "PASWEGIN", "PEACE RIVER", "PELLY", "PIERSON",
"PILGER", "PINAWA WNRE", "PINCHER CREEK ", "PORTAGE PRAIRIE",
"PRINCE ALBERT", "RANFURLY", "REGINA", "ROCKY MT HOUSE ", "SASKATOON",
"SCOTFIELD", "SCOTT", "SION", "SLAVE LAKE", "SPRAGUE", "STEINBACH",
"STETTLER NORTH", "SWAN RIVER", "SWIFT CURRENT", "THE PAS", "THOMPSON",
"TONKIN", "URANIUM CITY ", "VAL-MARIE", "VAUXHALL", "WABASCA RS",
"WASECA", "WASKESIU LAKE", "WEST POPLAR", "WHITECOURT", "WHITESAND DAM",
"WINNIPEG", "YELLOW GRASS"), class = "factor"), Mean = c(-0.020525899,
0.333863493, 0.210353772, NA, NA, 0.093520458, 0.341295298, NA,
-0.175074657, 0.09834825, 0.075610648, NA, -0.117503802, 0.18309367,
0.25246942, 0.221329766, 0.072167004, -0.094766032, NA, NA, 0.19783711,
-0.166351357, -0.0996169, -0.038555432, -0.028092042, 0.297855371,
0.108263891, 0.002057761, 0.327731415, NA, 0.180100638, 0.193837736,
-0.003306948, 0.178881894, 0.3655509, -0.235975798, -0.176154056,
-0.080433735, -0.110955273, -0.228010105, 0.048103255, -0.116681527,
-0.073042421, NA, NA, 0.035356012, 0.297171565, -0.197834719,
0.036412958, 0.055218077, NA, -0.236229087, 0.265211081, 0.271625885,
-0.293179359, 0.113744571, -0.207770026, 0.100471248, -0.071569464,
NA, NA, NA, -0.052716493, 0.057385851, 0.090340517, -0.30456625,
-0.234420722, 0.082287977, 0.009973663, NA, -0.06405062, 0.074703356,
-0.208329196, -0.272401078, 0.217991554, -0.043619919, -0.208901155,
-0.020022401, 0.111495318, NA, 0.38239749, 0.199136959, -0.177740258,
NA, 0.147515615, 0.309306538, 0.298741467, 0.068170296, NA, -0.02102765,
0.001754313, -0.010196512, 0.108254156, -0.228183063, -0.196261239,
NA, -0.167054722, 0.039949534, 0.154337034, -0.020855461, 0.136010278,
NA, 0.096997744, NA, -0.241963754, 0.660176529, 0.423554314,
0.190305726, -0.210778787, -0.261148915, NA, 0.054264129, -0.098706619,
-0.138776994, NA, NA, NA, -0.113823745, 0.373292721, -0.047060083
), Eastings = c(-102.5800018, -101.8700027, -99.08000183, -98.26999664,
-97.23000336, -98.08000183, -95.59999847, -96.76999664, -97.23000336,
-97.08000183, -97.02999878, -95.69999695, -97.01999664, -99.27999878,
-96, -95.19999695, -96.06999969, -100.0500031, -101.2300034,
-98.80000305, -99.56999969, -101.0999985, -97.84999847, -111.2200012,
-111.1200027, -116.0299988, -117.6200027, -108.4800034, -103.6999969,
-107.1299973, -102.3499985, -105.6200027, -105.2699966, -103.1500015,
-101.8799973, -94.06999969, -94.72000122, -101.0800018, -97.87000275,
-111.4499969, -111.1999969, -111.3499985, -110.4700012, -102.2799988,
-104.6500015, -101.7799988, -105.9800034, -102.9700012, -103.6500015,
-103.75, -102.0999985, -105.5500031, -101.6699982, -103.9199982,
-104.6699982, -104.1800003, -102.2300034, -107.3000031, -109.5,
-106.6500015, -107.9300003, -108.9199982, -107.7300034, -107.8499985,
-106.3799973, -109.1800003, -108.25, -108.8300018, -109.4000015,
-104.5999985, -107.0500031, -105.1500015, -105.6699982, -106.7200012,
-106.0699997, -104, -101.0500031, -99.94999695, -101.2699966,
-99.65000153, -113.8499985, -112.8199997, -113.5800018, -111.0699997,
-111.7300034, -114.1200027, -113.2799988, -114.6800003, -116.3000031,
-114.7799988, -115.7799988, -119.4000015, -118.5299988, -118.8799973,
-117.4499969, -113.8300018, -110.2799988, -108.4300003, -109.0999985,
-114.9199982, -112.7200012, -112.8700027, -113.75, -114.0999985,
-114.0199966, -113.7300034, -113.3799973, -113.0500031, -112.8000031,
-110.7200012, -113.6299973, -113.9800034, -112.1299973, -115.5500031,
-114.1800003, -114.4800034, -114.3700027, -118.0299988, -117.5299988,
-116.4499969), Northings = c(52.88000107, 52.08000183, 49.54999924,
49.95000076, 49, 49.18000031, 49.02000046, 49.52999878, 49.91999817,
50.93000031, 52.34999847, 51.02999878, 50.63000107, 53.15000153,
50.47000122, 49.61999893, 50.18000031, 51.09999847, 52.11999893,
50.41999817, 50.15000153, 53.97000122, 53.97000122, 56.65000153,
58.77000046, 58.38000107, 57.75, 59.56999969, 58.18000031, 57.34999847,
55.52999878, 57.25, 55.15000153, 56.22999954, 54.77000046, 58.72999954,
56.34999847, 56.86999893, 55.79999924, 52.06999969, 50.72000122,
51.58000183, 49.11999893, 50.90000153, 49.38000107, 51.52000046,
51.27000046, 49.22000122, 50.54999924, 51.25, 49.61999893, 50.33000183,
50.13000107, 51.97999954, 50.43000031, 49.81999969, 51.20000076,
49.72000122, 50.90000153, 50.47000122, 50.97999954, 49.68000031,
50.27000046, 49.36999893, 49, 51.52000046, 52.77000046, 52.36999893,
53.13000107, 52.81999969, 51.47999954, 52.41999817, 53.22000122,
52.16999817, 53.91999817, 53.33000183, 50.43000031, 49.91999817,
49.18000031, 49.41999817, 53.27999878, 53.02999878, 53.31999969,
53.88000107, 53.41999817, 53.88000107, 54.72000122, 54.13000107,
55.41999817, 55.27999878, 54.15000153, 55.20000076, 56.08000183,
55.18000031, 56.22999954, 55.97000122, 54.41999817, 55.83000183,
54.04999924, 52.41999817, 52.33000183, 51.47000122, 52.45000076,
51.77999878, 51.11999893, 49.93000031, 49, 50.88000107, 49.63000107,
50.02000046, 49.13000107, 49.52000046, 50.04999924, 51.20000076,
49.47000122, 49.63000107, 50.54999924, 52.93000031, 53.40000153,
53.58000183), Region = c(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, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)), .Names = c("Site",
"Name", "Mean", "Eastings", "Northings", "Region"), class = "data.frame", row.names = c(NA,
-120L))
#============================================
use abs()
eg
instead of size=Mean use size=abs(Mean)
Then, you can track the sign using shape (or some other aesthetic, but color and size are already taken)
replace:
geom_point(shape=21)+
geom_point(aes(colour = factor(Region))) +
with the single line
geom_point(aes(shape=factor(sign(Mean)), colour = factor(Region))) +
If you'd like, you can also add lines such as
scale_shape_discrete(name="Mean Is", breaks=c(-1, 1), labels=c("Negative", "Positive"))
guides(size=FALSE)

Resources