Add background to ggplot2 plot based on third variable - r

I would like to add a background to the following plot, based on a third variables (variable p in the data below). However, I don't get to make it work. There are some previous related post like for instance this one. However, it does not seem to work in my case.
THE DATA
out121<-structure(list(MEp_R = c(-0.00798875313192999, -0.0148676123843391,
-0.00682061802389229, -0.00342320747900698, -0.0151338635205871,
-0.0107317027491587, -0.0251293011821259, -0.00442888716467913,
-0.00336413112985843, -0.00684252019402579, -0.0179441683421641,
-0.0183345869013467, -0.00648428720725909, -0.0214876248035767,
-0.016436676184706, -0.0374053532385283, -0.0149373698200334,
-0.00812201774657575, -0.00845365671158477, -0.00770113356603315,
-0.0235236091649431, -0.00319728180470263, -0.00375490467048723,
-0.0105497059284769, -0.0113949568001174, -0.00796452607813328,
-0.018162596356658, -0.0305161849884928, -0.0182695024428088,
-0.00733874764247203, -0.0258767466756409, -0.00800032458837883,
-0.0254092616701198, -0.00796500399596014, -0.018098828213091,
-0.00403853247796117, -0.00537848845812282, -0.0150529001843169,
-0.0184434922700636, -0.028401757025326, -0.00394247092784099,
-0.00389984372897549, -0.0212293138913338, -0.00323995037136412,
-0.00435171103575741, -0.0142661346880167, -0.00763574799777637,
-0.0243480163192293, -0.0334550531174489, -0.00398665257789114,
-0.0269010788141356, -0.0339462390051263, -0.00731023368759553,
-0.00273217917938973, -0.00721679095419613, -0.0284960447574926,
-0.0256689850381392, -0.00418816161208601, -0.0207469376148546,
-0.0159256736739084, -0.00313049343988148, -0.0280083118773398,
-0.00305634712789695, -0.00222991072333599, -0.00728714579423012,
-0.00807506060034409, -0.0152635924760586, -0.0332733629111364,
-0.00760293035610356, -0.0139815979579726, -0.00677123700835164,
-0.0215960340688805, -0.0338156227799011, -0.0253065053249637,
-0.018647649832361, -0.0354963987663701, -0.022253017915301,
-0.00671809517372551, -0.00732452244298519, -0.0319724784878375,
-0.0342471218743405, -0.0350620385653106, -0.0230236881980878,
-0.0346963784053919, -0.00282689342327851, -0.00325484604261175,
-0.033849668042083, -0.00413139898221564, -0.0228993329834933,
-0.00559908152238757, -0.0183537460976775, -0.00472047771208315,
-0.0251909804124543, -0.0311011436015412, -0.0229936133519116,
-0.00308542036103775, -0.0228912921993044, -0.012939181841082,
-0.0230877632656367, -0.032591064432755, -0.00735469284998116,
-0.0303916947589657, -0.00314093237962542, -0.0231490730231191,
-0.0054652608506885, -0.025241520518079, -0.00445136785085343,
-0.0128593850454578, -0.00673762742746708, -0.0226916858622337,
-0.0161806428228128, -0.0209040965403646, -0.020750711338677,
-0.0181057154876848, -0.0178080592822426, -0.0176328580881027,
-0.0176110802336996, -0.0176155620180982, -0.0178561500821362,
-0.0154770868242239, -0.0155229482583411, -0.0180674673481902,
-0.0178800497751207, -0.0154828650185023, -0.0154750397213182,
-0.0152368261892578, -0.0154977482766045, -0.0182346532616592,
-0.0183274537639035, -0.0168011783244315, -0.0168016019401702,
-0.0149946562681369, -0.0158317352920475, -0.0167520543181098,
-0.0166157299693747, -0.0158279779461525, -0.0149977762443961,
-0.0180750003460435, -0.0151529841212906, -0.017923432133514,
-0.017928916003054, -0.0179127560726451, -0.0180570721490242,
-0.0177899027947602, -0.0158531006505291, -0.0174593473238637,
-0.0151291673194569, -0.0179473924690324, -0.017693119507179,
-0.0177884007129345, -0.0148359633238028, -0.0178372767093443,
-0.0179573607500675, -0.0178478763689906, -0.0159047471769866,
-0.0179332447429962, -0.0148569280408044, -0.0179181675050371,
-0.0166326656412793, -0.0148692975727, -0.0159248001099628, -0.0181012191592349,
-0.0178451421528243, -0.0165514761611982, -0.0157213399905123,
-0.0157870112143077), MEp_Irr = c(0.0267063409682918, 0.0348657385506534,
0.0342691355007968, -0.115640975842031, 0.0387492569408993, 0.0381593243875236,
0.0427698849005908, -0.0173766138908777, -0.137295803791003,
0.0255837365606775, 0.033333234010886, 0.04219733855297, 0.0283972511296412,
0.042063245938688, 0.0381434891186468, 0.116843385842082, 0.0303337697359422,
0.0270649725588233, 0.0408800022550086, 0.0212509208656095, 0.0411728527295687,
-0.124549799025409, -0.0144478716604061, 0.0423901861335305,
0.0415985024073151, 0.037704775866327, 0.0422604508495823, 0.0763588031748094,
0.0426357961926886, 0.0329608493967286, 0.0496266735787044, 0.040016714764635,
0.0519859953665068, 0.0394245716448228, 0.0342680692030386, -0.0267193515702238,
-0.0177971867890813, 0.0381256479675564, 0.0355367325792796,
0.0727607935148329, -0.0326388375297887, -0.0178079477006394,
0.0410572010103166, -0.117374678254156, -0.020320609244612, 0.0370392597214022,
0.0353728091272744, 0.0449625606410676, 0.0753850352272533, -0.0343858665221561,
0.0698051617341533, 0.077951540991422, 0.0237442438741627, -0.058297571549435,
0.0243284890185037, 0.0772458962449727, 0.0543866044441634, -0.00428096536358291,
0.04038564508393, 0.0354424565177792, -0.131352692076439, 0.0720546321152786,
-0.0665837597449708, -0.127968519873726, 0.0330986089266455,
0.0259981472748394, 0.0335310580145865, 0.0796945930317802, 0.020663774953603,
0.0293980728446607, 0.0362259277862459, 0.0432046089419517, 0.0685995447124759,
0.0469963521357076, 0.0354789715561056, 0.0709742762976496, 0.0452791300187091,
0.0331219798716994, 0.0295434908925642, 0.08744851179291, 0.0799768054034634,
0.0815503850718988, 0.0440574836496075, 0.0839865621030131, -0.0688754800689186,
-0.116779517177347, 0.0694952520459083, -0.0264949177920162,
0.0413932242242241, 0.0172278601886947, 0.0420753996022112, -0.144255178922228,
0.0470894777847475, 0.076135949143235, 0.041744283972074, -0.0630340966211796,
0.0413829074872194, 0.0396332210621789, 0.041111622411323, 0.102474470476636,
0.0325757173791441, 0.0760653479136575, -0.123211363037203, 0.0410342604744772,
-0.0174831909563214, 0.0530941558728644, -0.0418843636393472,
0.038435679009942, 0.0336208563560449, 0.0399056440442261, 0.0265540871197283,
0.0444367528340963, 0.0435895606359242, 0.0215615450224718, 0.0209324963084151,
0.0202690410290722, 0.0194304147117461, 0.0197015376925802, 0.0232944851025464,
0.0194971575601002, 0.0195128222684842, 0.0229793934050297, 0.022601136427616,
0.020063571212835, 0.0199659939198503, 0.0189019086482273, 0.020421297722678,
0.0238829270946739, 0.0242153520276344, 0.0261331103214652, 0.0259380402516094,
0.0195777353030817, 0.0234436006049973, 0.0253584791696736, 0.025820695095831,
0.0242819014695358, 0.0195352848772439, 0.0287263051849994, 0.0195826041035442,
0.025491942513768, 0.0244647468662558, 0.0257233178691396, 0.0262291526865683,
0.0247672209152699, 0.0245726192470798, 0.0273106213867739, 0.0199937372665979,
0.027112767769544, 0.027267180444377, 0.0261432756569257, 0.0198848926943087,
0.0256279615107693, 0.0279085914888886, 0.0285149093762463, 0.0240689981341775,
0.0266901583531034, 0.0200700646179756, 0.0262578329932839, 0.0263488901038499,
0.0201551487864926, 0.0244667611324202, 0.0262710081046724, 0.0264923641418946,
0.0252091848629891, 0.0230331062657154, 0.0230721882488657),
p = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.33226762955019e-15,
1.99840144432528e-15, 1.31006316905768e-14, 2.42694753183059e-13,
3.68407526707415e-10, 1.61860724823271e-08, 1.27863204824052e-06,
2.38962085785666e-06, 4.89028683459125e-06, 1.29586352866085e-05,
1.3194564602248e-05, 5.02157467259234e-05, 6.66173260168712e-05,
0.000452773851216115, 0.00058440281230121, 0.000733510241929469,
0.000880660946449119, 0.0136063339510712, 0.0151237168548795,
0.0353698786443037, 0.0615641433741911, 0.0627726749529298,
0.0763356770320813, 0.0824154857507584, 0.0929893345348154,
0.0947385432373617, 0.0958710673877281, 0.104039435798143,
0.117891950799349, 0.137872040413549, 0.173995516565528,
0.216611706295959, 0.277748137930015, 0.29066025684483, 0.294964122909925,
0.360934324888994, 0.390659280716865, 0.399257628135196,
0.415075121018395, 0.415906223140706, 0.429535524186567,
0.4346739056448, 0.461459259127042, 0.492938667179821, 0.552802494760443,
0.606427873500538, 0.608777827159917, 0.621809390011274,
0.65212833158726, 0.720281808796569, 0.725498755645982, 0.732029138010702,
0.873071745150822, 0.902596817430848, 0.965286951991386,
0.979065101832578)), datalabel = "Written by R. ", time.stamp = " 6 Aug 2017 22:23", .Names = c("MEp_R",
"MEp_Irr", "p"), formats = c("%9.0g", "%9.0g", "%9.0g"), types = c(255L,
255L, 255L), val.labels = c("", "", ""), var.labels = c("MEp_R",
"MEp_Irr", "p"), row.names = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "100", "101", "102", "103", "104",
"105", "106", "107", "108", "109", "110", "111", "112", "113",
"114", "115", "116", "117", "118", "119", "120", "121", "122",
"123", "124", "125", "126", "127", "128", "129", "130", "131",
"132", "133", "134", "135", "136", "137", "138", "139", "140",
"141", "142", "143", "144", "145", "146", "147", "148", "149",
"150", "151", "152", "153", "154", "155", "156", "157", "158",
"159", "160", "161", "162", "163", "164", "165", "166"), version = 12L, class = "data.frame")
THE PLOT
out121$perc1<-c(1:166)
l64<-ggplot(out121,aes(perc1))
l65<-l64+geom_line(aes(y=MEp_R,colour="var1"),size=1.3)+
geom_line(aes(y=MEp_Irr,colour="var2"),size=1.3)+
scale_colour_manual(values=c("blue3","green"), name="MEt")+
scale_x_discrete(name="Threshold (in percentage)",breaks=c(0, 250, 500,750,1000),
labels=c("0", "25", "50","75","100")) +
scale_y_continuous(name="MEp",limits = c(-0.7, 0.5),breaks=c(-0.3,-0.1,0,0.1,0.3,0.5))
l66<-l65+ theme_bw()+ggtitle("Full sample ") +
theme(plot.title = element_text(lineheight=.8, face="bold"),legend.position="bottom")+
guides(col=guide_legend(ncol=2,title.position="top"))+
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
THE QUESTION
I now would like to add a background to this plot, based on variable "p".
The variable only has a few values bigger than 0.05. So only for those points the background should be coloured.
That means, points lower than 0.05 should have a white background, the other points should have a red (for instance) background.
I made some unfortunate efforts based on the following code but I don't get the scaling (white/blue right, not even on a continuous basis) and I don't get the color in the background. Could somebody help me please? Thank you very much!
geom_rect(aes(xmin = 0, xmax = 1000, ymin = -Inf, ymax = Inf,fill = p))+
scale_color_gradient2(midpoint=0.05, low="white", mid="light blue",high="blue", space ="Lab" )
One of the errors I for instance get is:
"Scale for 'colour' is already
present. Adding another scale for
'colour', which will replace the
existing scale." But even if that is solved, it doesn't solve the fact that I only have one color on my background graph.

First
out121$p_above <- out121$p > 0.05
and then
l66 + geom_rect(aes(xmin = perc1 - 0.5, xmax = perc1 + 0.5,
ymin = -Inf, ymax = Inf, fill = p_above), alpha = 0.4) +
scale_fill_manual(values = c("white", "red"))

Related

Select same name object with two dimension

Below is the sample data. May I ask how can I select all ge_v at once? If there is only single number inside [], I know I can use sprintf ('ge_v[%d]', seq(10)) to select ge_v[1]...ge_v[10]. How can I do the same thing for two dimensional data?
structure(list(rn = c("ge_v[27,1]", "ge_v[28,2]", "ge_v[29,3]",
"ge_v[30,4]", "ge_v[31,5]", "ge_v[32,6]", "ge_v[33,7]", "ge_v[34,8]",
"ge_v[35,9]", "ge_v[36,10]", "ge_v[37,11]"), mean = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), row.names = c("70", "71", "72", "73",
"74", "75", "76", "77", "78", "79", "80"), class = "data.frame")
Perhaps this helps
sprintf ('ge_v[%d,%d]', 27:36, 1:10)

Error in as.vector(data) when creating a mask for SECR

I am trying to create a mask in SECR using a .shp file. Always getting this error: Error in as.vector(data) :
no method for coercing this S4 class to a vector when i use the make.mask
this is my code:
fence <- rgdal :: readOGR('/Rdata/SECR', layer = 'building')
OGR data source with driver: ESRI Shapefile Source: "/Rdata/SECR",
layer: "building" with 2492 features
It has 1 fields
library(secr)
#This is secr 3.2.1. For overview type ?secr
> qmask = make.mask(Quenda_traps,
+ buffer = 300,
+ type = "trapbuffer",
+ poly = fence,
+ poly.habitat = "FALSE")
Error in as.vector(data) : no method for coercing this S4 class to
a vector
dput(Quenda_traps)
structure(list(x = c(390576.21, 390637.85, 390594.93, 390528.49,
390646.58, 390488.12, 390681.01, 390499.98, 390632.29, 390677.26,
390642.7, 390710.33, 390690.37, 390741.81, 390588.01, 390655.06,
390575.97, 390246.66, 390340.13, 390236.33, 390309.59, 390295.93,
390164.11, 390065.71, 390120.42, 390117.17, 390091.7, 389875.57,
390179.69, 390157.45, 390164.94, 390151.02, 390172.17, 390246.28,
390263.25, 390256.32, 390308.2, 390135.06, 390093.3, 389914.13,
389916.76, 389869.37, 389809.17, 389782.5, 389818.78, 389802.75,
389818.78, 389771.52, 389792.74, 389791.25, 389905.36, 389832.62,
389886.16, 389863.21, 389908.68, 389912.46, 389902.05, 389528.11,
389661.6, 389689.54, 389657.88, 389678.71, 389569.25, 389618.44,
389564.87, 389615.37, 389662.18, 389630.96, 389713.09, 389654.91,
389744.37, 389762.02, 389715.87, 389696.2), y = c(6451727.44,
6451613.91, 6451566.89, 6451511.85, 6451416.66, 6451402.77, 6451287.32,
6451177.83, 6451164.84, 6451108.78, 6451188.57, 6450929.54, 6450855.04,
6450723.66, 6450716.47, 6450451.11, 6450343.83, 6451821.46, 6451645.08,
6451553.05, 6451588.21, 6451541.5, 6451509.03, 6451442.56, 6451358.89,
6451222.49, 6451221.11, 6451303.03, 6451115.63, 6450989, 6450994.62,
6450797.13, 6450761.88, 6450717.22, 6450719.62, 6450399.14, 6450403.03,
6450352.38, 6450197.83, 6451841.15, 6451684.86, 6451526.92, 6451419.83,
6451441.72, 6451316.83, 6451367.43, 6451316.83, 6451235.39, 6451105.9,
6450981.72, 6450992.93, 6450910.1, 6450935.07, 6450787.37, 6450685.86,
6450684.79, 6450600.42, 6451656.26, 6451534.65, 6451395.26, 6451267.42,
6451262.1, 6451257.59, 6451248.14, 6451138.91, 6451096.22, 6451132.2,
6450964.46, 6450964.24, 6450844.98, 6450864.8, 6450717.54, 6450620.58,
6450519.48)), class = c("traps", "data.frame"), row.names = c("1001",
"1002", "1003", "1004.1", "1004.2", "1005.1", "1005.2", "1006.1",
"1006.2", "1006.3", "1006.4", "1007", "1008", "1009", "1010",
"1011", "1012", "2001", "2002", "2003.1", "2003.2", "2003.3",
"2004", "2005", "2006", "2007.1", "2007.2", "2008.1", "2008.2",
"2009.1", "2009.2", "2010.1", "2010.2", "2011.1", "2011.2", "2012.1",
"2012.2", "2013", "2014", "3001", "3002", "3003", "3004.1", "3004.2",
"3005.1", "3005.2", "3005.3", "3006", "3007", "3008.1", "3008.2",
"3009.1", "3009.2", "3010", "3011.1", "3011.2", "3012", "4001",
"4002", "4003", "4004.1", "4004.2", "4005.1", "4005.2", "4006",
"4007.1", "4007.2", "4008.1", "4008.2", "4009.1", "4009.2", "4010",
"4011", "4012"), detector = "multi", usage = structure(c(1, 1,
1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
1, 1, 1, 0, 1... ), .Dim = c(74L, 85L), .Dimnames = list(
c("1001", "1002", "1003", "1004.1", "1004.2", "1005.1", "1005.2",
"1006.1", "1006.2", "1006.3", "1006.4", "1007", "1008", "1009",
"1010", "1011", "1012", "2001", "2002", "2003.1", "2003.2",
"2003.3", "2004", "2005", "2006", "2007.1", "2007.2", "2008.1",
"2008.2", "2009.1", "2009.2", "2010.1", "2010.2", "2011.1",
"2011.2", "2012.1", "2012.2", "2013", "2014", "3001", "3002",
"3003", "3004.1", "3004.2", "3005.1", "3005.2", "3005.3",
"3006", "3007", "3008.1", "3008.2", "3009.1", "3009.2", "3010",
"3011.1", "3011.2", "3012", "4001", "4002", "4003", "4004.1",
"4004.2", "4005.1", "4005.2", "4006", "4007.1", "4007.2",
"4008.1", "4008.2", "4009.1", "4009.2", "4010", "4011", "4012"
), c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
"32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
"42", "43", "44", "45", "46", "47", "48", "49", "50", "51",
"52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71",
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84", "85"))), spacex = 0.240000000048894, spacey = 0.21999999973923, spacing = 79.5867492192872)

Change color of a specific coordinate

I want to change the color of a certain coordinate, actually is the same coordinate which has the annotation.
Any ideas?
p1 <- ggplot(HiBAP1517, aes(BPM, Yld)) +
geom_point(shape=16) +
geom_smooth(method=lm, se = F) +
theme(axis.title.x = element_text(color="black", size=14, face="bold"),
axis.title.y = element_text(color="black", size=14, face="bold"))
p2 <- p1 +
annotate(geom="text", x=1879, y=892.02, label="Rialto",
color="darkorange", size = 5, hjust=1, vjust=1.3, fontface =2)
p3 <- p2 +
annotate(geom="text", x=1654.75, y=834.2375, label="Savannah",
color="firebrick1", size = 5, hjust=1, vjust=1, fontface =2)
pfinal <- p3 +
labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
y = expression("GY"*(gm^{-2})))
This is my output, but I would like to change the color (instead of black) of those 2 coordinates:
Data sample:
Genotype,BPM,Yld
1,1767.793447,747.0708034
2,2074.815941,775.8880562
3,2197.933995,854.3810136
5,2085.627286,845.9306447
6,1908.97774,841.4318038
7,2120.24666,875.5534429
8,2226.617509,764.3849451
9,2035.68002,810.2658242
10,2153.727,861.7024631
11,1993.568134,782.5763292
12,2013.199982,822.6565187
13,2078.275912,837.2819632
14,2042.456487,802.6913977
16,1840.058841,767.6509829
17,2013.338146,801.2064103
18,2087.151352,822.1910199
19,1988.038384,859.573342
20,2083.092896,887.2783898
21,2072.905795,861.3044422
23,1849.744525,723.5014595
24,1785.04038,747.4940519
25,2078.402869,835.7669124
26,1698.390774,681.256732
27,2065.842661,852.3073467
28,2020.285009,811.6889063
29,2039.137248,821.7951099
30,1855.665106,781.0350726
31,1792.32475,744.9001931
32,1992.616447,860.7054072
33,2025.79755,834.1452611
34,2023.274784,835.4102703
35,1703.837196,682.9995098
36,1740.44177,713.3121368
37,1970.331012,816.5239645
38,1990.223669,838.9949534
39,2081.559891,822.5936391
40,1968.990856,852.1259441
41,2178.322511,920.80226
42,1887.572381,721.0746569
43,2103.964882,821.6521912
44,2097.040605,873.0062511
45,1864.779016,755.1746154
46,1935.743565,895.4951282
47,2191.797365,888.7284615
48,1968.150754,863.7490909
49,1858.735915,759.7144347
50,1933.34954,774.4202087
51,1680.540128,717.2402198
52,1748.214736,783.3395385
53,2183.694734,855.5897436
54,2142.662802,912.635349
55,1892.205584,776.5070164
56,2230.304238,887.8378102
57,2141.882287,903.7212821
58,1983.755009,815.5541958
59,1954.653032,743.0290819
60,1801.192428,718.5391635
61,1920.709571,808.6727692
62,1796.291216,699.0526007
63,2026.074655,909.3961954
64,1863.574774,729.9547929
65,1924.971832,770.2818388
66,2129.910527,794.0297343
67,2090.201938,809.6094569
68,1987.074651,731.8146606
69,2053.104282,839.4181954
70,1872.403668,787.2339391
71,1961.144455,824.335206
72,2135.414422,881.9237509
73,1857.780642,779.9428159
74,2058.696424,840.2234927
76,2169.489819,805.3868184
77,1891.844601,756.8752683
78,2099.708756,830.6765073
79,1976.981377,786.4878009
81,1932.909878,800.0033701
82,2101.603045,834.2990498
83,1867.872044,735.4201911
84,1870.947954,703.6186056
85,2135.962836,798.3315211
86,1859.497846,762.135947
87,1966.35974,776.6730353
88,2088.086246,808.0767316
89,1964.134743,851.5441764
90,2211.81001,866.3412008
91,1881.56405,805.7430148
92,1921.941058,725.2508829
93,1576.551861,606.5037422
95,2249.995426,882.4130493
96,2092.694714,778.8794369
97,2099.861152,840.9202391
98,1837.6733,760.0247786
99,1986.16533,796.1227279
100,1981.047087,747.7190033
Rialto,1879,892.02
Savannah,1654.75,834.2375
I assume you want to change the color of the points corresponding to the labels, since their precise coordinates along the x/y-axis aren't shown, and you'll have more work pinpointing them before the question about color comes up. If that's not the case, I'll delete my answer.
# define color corresponding to each genotype
HiBAP1517$color <- case_when(HiBAP1517$Genotype == "Savannah" ~ "firebrick1",
HiBAP1517$Genotype == "Rialto" ~ "darkorange",
TRUE ~ "black")
# plot
ggplot(HiBAP1517,
# specify color aesthetic here for both geom_point & geom_text to inherit
aes(x = BPM, y = Yld, color = color)) +
geom_point(shape = 16) +
geom_smooth(color = "#3366FF", # maintain default color for geom_smooth line
method = lm, se = F) +
# position labels based on their coordinates, rather than hard-code them via annotate()
geom_text(aes(label = ifelse(Genotype %in% c("Savannah", "Rialto"),
Genotype, "")),
hjust = 1, vjust = 1) +
# use defined colors directly
scale_color_identity() +
# other aesthetic parameters, irrelevant to the question at hand
labs(x = expression("AGDM"[PM]^{}*(gm^{-2})),
y = expression("GY"*(gm^{-2}))) +
theme_classic() +
theme(axis.title.x = element_text(color="black", size=14, face="bold"),
axis.title.y = element_text(color="black", size=14, face="bold"))
Data used:
> dput(HiBAP1517)
structure(list(Genotype = c("1", "2", "3", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "16", "17", "18", "19", "20",
"21", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
"44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
"55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65",
"66", "67", "68", "69", "70", "71", "72", "73", "74", "76", "77",
"78", "79", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "95", "96", "97", "98", "99", "100",
"Rialto", "Savannah"), BPM = c(1767.793447, 2074.815941, 2197.933995,
2085.627286, 1908.97774, 2120.24666, 2226.617509, 2035.68002,
2153.727, 1993.568134, 2013.199982, 2078.275912, 2042.456487,
1840.058841, 2013.338146, 2087.151352, 1988.038384, 2083.092896,
2072.905795, 1849.744525, 1785.04038, 2078.402869, 1698.390774,
2065.842661, 2020.285009, 2039.137248, 1855.665106, 1792.32475,
1992.616447, 2025.79755, 2023.274784, 1703.837196, 1740.44177,
1970.331012, 1990.223669, 2081.559891, 1968.990856, 2178.322511,
1887.572381, 2103.964882, 2097.040605, 1864.779016, 1935.743565,
2191.797365, 1968.150754, 1858.735915, 1933.34954, 1680.540128,
1748.214736, 2183.694734, 2142.662802, 1892.205584, 2230.304238,
2141.882287, 1983.755009, 1954.653032, 1801.192428, 1920.709571,
1796.291216, 2026.074655, 1863.574774, 1924.971832, 2129.910527,
2090.201938, 1987.074651, 2053.104282, 1872.403668, 1961.144455,
2135.414422, 1857.780642, 2058.696424, 2169.489819, 1891.844601,
2099.708756, 1976.981377, 1932.909878, 2101.603045, 1867.872044,
1870.947954, 2135.962836, 1859.497846, 1966.35974, 2088.086246,
1964.134743, 2211.81001, 1881.56405, 1921.941058, 1576.551861,
2249.995426, 2092.694714, 2099.861152, 1837.6733, 1986.16533,
1981.047087, 1879, 1654.75), Yld = c(747.0708034, 775.8880562,
854.3810136, 845.9306447, 841.4318038, 875.5534429, 764.3849451,
810.2658242, 861.7024631, 782.5763292, 822.6565187, 837.2819632,
802.6913977, 767.6509829, 801.2064103, 822.1910199, 859.573342,
887.2783898, 861.3044422, 723.5014595, 747.4940519, 835.7669124,
681.256732, 852.3073467, 811.6889063, 821.7951099, 781.0350726,
744.9001931, 860.7054072, 834.1452611, 835.4102703, 682.9995098,
713.3121368, 816.5239645, 838.9949534, 822.5936391, 852.1259441,
920.80226, 721.0746569, 821.6521912, 873.0062511, 755.1746154,
895.4951282, 888.7284615, 863.7490909, 759.7144347, 774.4202087,
717.2402198, 783.3395385, 855.5897436, 912.635349, 776.5070164,
887.8378102, 903.7212821, 815.5541958, 743.0290819, 718.5391635,
808.6727692, 699.0526007, 909.3961954, 729.9547929, 770.2818388,
794.0297343, 809.6094569, 731.8146606, 839.4181954, 787.2339391,
824.335206, 881.9237509, 779.9428159, 840.2234927, 805.3868184,
756.8752683, 830.6765073, 786.4878009, 800.0033701, 834.2990498,
735.4201911, 703.6186056, 798.3315211, 762.135947, 776.6730353,
808.0767316, 851.5441764, 866.3412008, 805.7430148, 725.2508829,
606.5037422, 882.4130493, 778.8794369, 840.9202391, 760.0247786,
796.1227279, 747.7190033, 892.02, 834.2375)), class = "data.frame", row.names = c(NA,
-96L))

How to delete a single cell?

So, I am trying to delete a single cell from my data. The problem is that a normal function doesn't work:
data[-1,]
data[-1,-1]
data$x = NULL
etc
So that's how my main code looks like:
data_rd <- data_rd[-1,] ## I could delete one row but can't the next one.
## Create an empty matrix
name_cols <- paste0("F", rep(1:20, each = 4), "_", 1:4)
name_columns <- c("Description", name_cols)
mat_master_EOD <- matrix(0, nrow = length(data_rd[,1]),ncol = 81)
colnames(mat_master_EOD) <- name_columns
rownames(mat_master_EOD) <- data_rd[,1]
The cell which I want to delete:
http://imageshack.com/a/img32/6227/hy0s.jpg
I used rbind.fillfunction to group some of data files, that might be the reason why I am not able to remove this cell.
EDIT:
That's only what I can do for you:
dput(data_rd[1:3, 1:3])
structure(list(X = c("Accession", "ZZ_FGCZCont0025", "ZZ_FGCZCont0099"
), X.1 = structure(c(29L, 22L, 20L), .Label = c("", "1", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20",
"21", "23", "25", "27", "28", "29", "3", "34", "4", "5", "6",
"7", "8", "9", "Peptide count", "22", "24", "26", "30", "31",
"32", "33", "35", "36", "37", "40", "41", "45", "46", "50", "55",
"63", "73", "38", "48", "56", "68", "82", "39", "42", "43", "44",
"58", "67", "76", "90", "59", "49", "54", "71", "65", "51", "53",
"57", "61", "64", "74", "75", "47", "52", "60", "72", "69", "70",
"62"), class = "factor"), X.2 = structure(c(23L, 9L, 15L), .Label = c("",
"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "2", "28", "3", "4", "5", "6", "7", "8", "9", "Peptides used for quantitation",
"20", "21", "22", "23", "24", "25", "26", "27", "29", "30", "33",
"37", "39", "41", "45", "46", "49", "32", "34", "35", "36", "44",
"68", "40", "43", "47", "66", "88", "42", "59", "58", "52", "71",
"31", "38", "51", "48", "50", "62"), class = "factor")), .Names = c("X",
"X.1", "X.2"), row.names = 2:4, class = "data.frame")
And for the new created matrix:
> dput(mat_master_EOD[1:10, 1:10])
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(10L,
10L), .Dimnames = list(c("Accession", "ZZ_FGCZCont0025", "ZZ_FGCZCont0099",
"ZZ_FGCZCont0126", "ZZ_FGCZCont0146", "AT1G19570", "ZZ_FGCZCont0158",
"AT5G38480", "ZZ_FGCZCont0050", "AT1G07370"), c("Description",
"F1_1", "F1_2", "F1_3", "F1_4", "F2_1", "F2_2", "F2_3", "F2_4",
"F3_1")))
I'm reaching here, but is this is what you're trying to get:
Accession Peptide count Peptides used for quantitation
3 ZZ_FGCZCont0025 34 15
4 ZZ_FGCZCont0099 29 28
If so, you can do this:
setNames(data_rd[-1,], sapply(data_rd[1, ], as.character))
Note that your Peptide... columns are factors, which is terribly confusing for numeric columns. You should consider re-importing the data using something like read.csv(file="blahblah", header=TRUE) which I think will resolve most your problems from the get go.

Computing angle between two vectors (with one vector having a specific X,Y position)

I am trying to compute the angle between two vectors, wherein one vector is fixed and the other vector is constantly moving. I already know the math in this and I found a code before:
theta <- acos( sum(a*b) / ( sqrt(sum(a * a)) * sqrt(sum(b * b)) ) )
I tried defining my a as:
a<-c(503,391)
and my b as:
b <- NM[, c("X","Y")]
When I apply the theta function I get:
Warning message:
In acos(sum(a * b)/(sqrt(sum(a * a)) * sqrt(sum(b * b)))) : NaNs produced
I would appreciate help to solve this.
And here is my sample data:
structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "103", "104", "105", "106", "107", "108", "109",
"110"), class = "factor"), T = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6 ), X =
c(528.04, 528.04, 528.04, 528.04, 528.04, 528.04), Y = c(10.32,
10.32, 10.32, 10.32, 10.32, 10.32), V = c(0, 0, 0, 0, 0, 0),
GD = c(0, 0, 0, 0, 0, 0), ND = c(NA, 0, 0, 0, 0, 0), ND2 = c(NA,
0, 0, 0, 0, 0), TID = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("t1",
"t10", "t100", "t101", "t102", "t103", "t104", "t105", "t106",
"t107", "t108", "t109", "t11", "t110", "t12", "t13", "t14",
"t15", "t16", "t17", "t18", "t19", "t2", "t20", "t21", "t22",
"t23", "t24", "t25", "t26", "t27", "t28", "t29", "t3", "t30",
"t31", "t32", "t33", "t34", "t35", "t36", "t37", "t38", "t39",
"t4", "t40", "t41", "t42", "t43", "t44", "t45", "t46", "t47",
"t48", "t49", "t5", "t50", "t51", "t52", "t53", "t54", "t55",
"t56", "t57", "t58", "t59", "t6", "t60", "t61", "t62", "t63",
"t64", "t65", "t66", "t67", "t68", "t69", "t7", "t70", "t71",
"t72", "t73", "t74", "t75", "t76", "t77", "t78", "t79", "t8",
"t80", "t81", "t82", "t83", "t84", "t85", "t86", "t87", "t88",
"t89", "t9", "t90", "t91", "t92", "t93", "t94", "t95", "t96",
"t97", "t98", "t99"), class = "factor")), .Names = c("A", "T", "X", "Y", "V", "GD", "ND", "ND2", "TID"), row.names = c(NA, 6L),
class = "data.frame")
Your function is not vectorized. Try this:
theta <- function(x,Y) apply(Y,1,function(y,x) acos( sum(x*y) / ( sqrt(sum(x^2)) * sqrt(sum(y^2)) ) ),x=x)
a<-c(503,391)
b <- DF[, c("X","Y")]
theta(a,b)
# 1 2 3 4 5 6
#0.6412264 0.6412264 0.6412264 0.6412264 0.6412264 0.6412264
There is a problem with the acos and atan functions in this application, as you cannot compute angles for the full circle, only for the plus quadrant. In 2D, you need two values to specify a vector, and you need two values (sin and cos) to define it in degrees/radians up to 2pi. Here is an example of the acos problem:
plot(seq(1,10,pi/20)) ## A sequence of numbers
plot(cos(seq(1,10,pi/20))) ## Their cosines
plot(acos(cos(seq(1,10,pi/20)))) ## NOT Back to the original sequence
Here's an idea:
angle <- circular::coord2rad(x, y)
plot(angle)
where "(x,y)" has "angle"
as.numeric(angle)
gives the angle in radians (0,360). To report geographical directions, convert to degrees, and other things, you can use the added parameters for the circular function, e.g.:
x <- coord2rad(ea,eo, control.circular = list(type = "directions",units = "degrees"))
plot(x)
as.numeric(x)

Resources