Modify markers/lines for two legends separately in R

My plot below consists of contour lines from a density estimate and several points, which are colored by the number of experiments. The legend for the contour lines is indicated by the alpha values, which is based on the level of the contour. The legend for the points are based on color and shape. When I try to modify the size of the points in the second legend, it also modifies the points for the first (level) legend. I’d like to

  1. Remove the markers in the level legend and keep only the lines.
  2. Remove the lines in the points legend (Num. Experiments) and keep only the markers.

I tried to use the grid package to modify the elements, but I’m lost as two what element in the grobs I should be extracting.

 plt <- ggplot() +   facet_grid(Ylab~subtype)+   geom_point(data=subset(All_subtypes_CD27_57_45,disc_comb_label>0),              aes(x=CD45RO,y=Y,                  color=factor(disc_comb_label),                  shape=factor(disc_comb_label)),              size=0.3,stroke=0.4,alpha=0.7,show.legend = TRUE) +   stat_density_2d(data=subset(All_subtypes_CD27_57_45,disc_comb_label==0),                   geom = "density2d", contour = TRUE,                   aes(x=CD45RO,y=Y,alpha=..level..),                   color = "black",                   bins = 20,                   show.legend=TRUE) +   labs(col="Num. Experiments",shape="Num. Experiments") +   theme_light() +   theme(axis.title.y = element_blank())  plt grid::grid.ls(grid::grid.force())  # Set the size of the point in the legend to 4 mm grid::grid.gedit("key-[-0-9]-1-1", size = unit(4, "mm"))  # save the modified plot to an object plt2 <- grid::grid.grab() ggsave(plt2, filename = 'plt2.jpg') 

Example Plot

Edit

Here is a sample (n=100) of my data. Note: the bins parameter in stat_density_2d should be changed to a smaller number.

structure(list(Y = c(0.284191844029658, 0.558504950709616, 0.315535249343904,  0.300977792187569, 0.476751857332456, 0.484240073554636, 0.307618222739758,  0.382774310068314, 0.251491191205721, 0.28867075547189, 0.48023653568676,  0.687385202552218, 0.348302145404881, 0.284956590469867, 0.449212367426197,  0.0541572395716273, 0.43091149290646, 0.475086982205484, 0.245475455526474,  0.320922298535602, 0.433582014376387, 0.454556959163362, 0.519150252727787,  0.501333864070255, 0.485212214204431, 0.477742661471719, 0.141047727372665,  0.455451223886891, 0.445897410519097, 0.405296464170209, 0.383655314069156,  0.213964454993715, 0.242076596187618, 0.298039605455974, 0.157815389237226,  0.510987159437738, 0.228542986920969, 0.348112572853986, 0.149900990492508,  0.390457313763976, 0.530735405696848, 0.213071719254767, 0.241746672739817,  0.109465638906155, 0.2119121647415, 0.252433268896037, 0.271972455301221,  0.434829111969484, 0.139716827881697, 0.342203160239369, 0.604210909752249,  0.161481369781311, 0.514151082252664, 0.826880955040602, 0.278455388719208,  0.457955173141471, 0.223481509755878, 0.281433841010683, 0.265601344949778,  0.272361939825042, 0.36767244848865, 0.200105787104064, 0.297752168002793,  0.188282485134585, 0.33643615610689, 0.395965507439215, 0.0890174556246681,  0.417612455377232, 0.19887950366616, 0.465049525577064, 0.43789996897205,  0.233188419733737, 0.472491498664791, 0.490773751335154, 0.283266705976633,  0.401634634651268, 0.45359606927769, 0.20839301810666, 0.441058569024316,  0.276163969969656, 0.431188894873133, 0.237522516690592, 0.233077805700025,  0.297329147941213, 0.42710702221155, 0.3907243000129, 0.465552001533375,  0.437707004564491, 0.384245170961772, 0.142554943891278, 0.435330262145555,  0.284103620665539, 0.2032116293577, 0.414955141708168, 0.345017391130214,  0.248463896517405, 0.339817523912329, 0.459771971558186, 0.468755486667213,  0.510785855184823), CD45RO = c(0.525726398529141, 0.304474523564729,  0.612153302344189, 0.612445867565633, 0.249006124636231, 0.0630792385460927,  0.634621606796433, 0.252505566525164, 0.58427415301859, 0.604953653422854,  0.59367781186968, 0.548146880829038, 0.67686535460711, 0.079010054101595,  0.634599315045999, 0.13187972282609, 0.621565232085858, 0.596574983786016,  0.110399227872914, 0.658258800443354, 0.187991482962981, 0.650987717469758,  0.596006502059151, 0.308912127599816, 0.141884410965155, 0.114224151171362,  0.190296706882678, 0.0868622977334288, 0.63084574911242, 0.584149459993462,  0.565119489277156, 0.262145879230163, 0.275739650088701, 0.628409611625278,  0.106602930270878, 0.107132119095279, 0.620913668866589, 0.624362488858585,  0.233756876594812, 0.655615553883786, 0.54873101008282, 0.566443932082788,  0.561891801854012, 0.621932759341326, 0.552259106473471, 0.56920228491713,  0.602947665552384, 0.696337649780824, 0.660389489026038, 0.584038886021908,  0.468975660037085, 0.333155729501406, 0.0495060776252205, 0.16990324928303,  0.193105192142293, 0.181173895819966, 0.150182889940719, 0.543052295456474,  0.283001532733579, 0.303728587073113, 0.183658577822127, 0.592509419580238,  0.714762268117432, 0.128905862697456, 0.638109641089135, 0.654036824302771,  0.339947661585455, 0.594245937801801, 0.490577748124374, 0.22853081037735,  0.185257260802601, 0.533315410258717, 0.626633178546256, 0.199126070652748,  0.188082776802629, 0.635724850685052, 0.638027785005966, 0.608592892283015,  0.613768324429073, 0.680925083206514, 0.722032485457904, 0.143070125871505,  0.633443738551264, 0.308183097921232, 0.619142922059335, 0.335770730782286,  0.291214123731697, 0.142393299180996, 0.252553809505749, 0.423003230545203,  0.568276957780311, 0.484324230817047, 0.285551015338659, 0.524580106114011,  0.680325288283035, 0.348542443973256, 0.581367577522363, 0.321724942946382,  0.0778090215803436, 0.231216025188849), disc_comb_label = c(2,  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 1,  5, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0,  0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0,  1, 6, 2, 0, 1, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 2, 2, 0, 0, 0, 0, 1, 3, 4, 0, 0, 2, 0, 2), Ylab = c("CD27",  "CD57", "CD57", "CD57", "CD27", "CD27", "CD57", "CD27", "CD27",  "CD27", "CD27", "CD57", "CD57", "CD57", "CD27", "CD57", "CD27",  "CD27", "CD57", "CD27", "CD27", "CD27", "CD27", "CD27", "CD27",  "CD27", "CD57", "CD27", "CD27", "CD27", "CD57", "CD57", "CD57",  "CD57", "CD57", "CD27", "CD27", "CD27", "CD57", "CD27", "CD27",  "CD57", "CD27", "CD57", "CD57", "CD27", "CD57", "CD57", "CD57",  "CD27", "CD57", "CD57", "CD27", "CD57", "CD57", "CD27", "CD57",  "CD57", "CD57", "CD27", "CD57", "CD27", "CD57", "CD57", "CD27",  "CD27", "CD57", "CD27", "CD27", "CD27", "CD27", "CD57", "CD27",  "CD27", "CD57", "CD27", "CD27", "CD57", "CD57", "CD57", "CD27",  "CD27", "CD57", "CD57", "CD27", "CD57", "CD27", "CD57", "CD57",  "CD57", "CD27", "CD57", "CD57", "CD57", "CD57", "CD27", "CD27",  "CD27", "CD27", "CD27"), subtype = c("CD4-CD8+", "CD4+CD8+",  "CD4-CD8+", "CD4+CD8-", "CD4+CD8+", "CD4+CD8-", "CD4+CD8+", "CD4-CD8+",  "CD4+CD8-", "CD4+CD8+", "CD4+CD8+", "CD4+CD8+", "CD4+CD8+", "CD4-CD8+",  "CD4+CD8-", "CD4-CD8+", "CD4-CD8+", "CD4+CD8-", "CD4+CD8-", "CD4+CD8-",  "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4-CD8+", "CD4-CD8+", "CD4+CD8+",  "CD4+CD8-", "CD4+CD8-", "CD4-CD8+", "CD4+CD8-", "CD4+CD8-", "CD4+CD8+",  "CD4+CD8-", "CD4+CD8+", "CD4+CD8+", "CD4+CD8+", "CD4+CD8-", "CD4+CD8+",  "CD4-CD8+", "CD4-CD8+", "CD4+CD8-", "CD4-CD8+", "CD4+CD8-", "CD4+CD8-",  "CD4+CD8-", "CD4-CD8+", "CD4+CD8-", "CD4+CD8+", "CD4+CD8-", "CD4-CD8+",  "CD4-CD8+", "CD4+CD8-", "CD4-CD8+", "CD4-CD8+", "CD4+CD8-", "CD4+CD8+",  "CD4+CD8-", "CD4+CD8+", "CD4+CD8+", "CD4-CD8+", "CD4-CD8+", "CD4+CD8+",  "CD4+CD8+", "CD4+CD8-", "CD4+CD8-", "CD4+CD8+", "CD4+CD8-", "CD4+CD8+",  "CD4-CD8+", "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4+CD8+", "CD4-CD8+",  "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4+CD8+", "CD4+CD8-",  "CD4+CD8+", "CD4-CD8+", "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4+CD8+",  "CD4-CD8+", "CD4+CD8-", "CD4+CD8-", "CD4-CD8+", "CD4-CD8+", "CD4+CD8-",  "CD4+CD8-", "CD4+CD8-", "CD4+CD8-", "CD4-CD8+", "CD4-CD8+", "CD4+CD8-",  "CD4+CD8-", "CD4+CD8-")), row.names = c(NA, -100L), class = "data.frame") 
Asked on August 30, 2020 in R.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.