Search code examples
rggplot2conditional-statementsgeom-point

Marking conditional geom_points with cross or outline color


I want to make a conditional geom_point , where sids that are x in the data are marked for example by a cross in the point or alternative outline color. Note that I don't want to add additional points to the plot.

My code:

ggplot(plotdata, aes(x = Value)) +
  geom_point(aes(y = ssp1, colour = region),alpha = 0.2,size=10) +
  geom_text(aes(y = ssp1, label = NA),size = 10)+
  ggnewscale::new_scale_color() +
  geom_point(aes(y = ssp3, colour = region),alpha=0.2,size=10) +
  geom_text(aes(y = ssp3, label = NA),size = 10)+
  geom_point(aes(y = ssp3),
             shape = 21,size = 12,colour = "black")

I've (unsuccessfully) tried adding:

geom_point(aes(y = ssp3),
             shape = 21,size = 12,colour = ifelse(sids == "x", "black", "red"))) +

geom_point(data = plotdata[which(plotdata$sids == "x"),], 
  aes(shape = 50,size = 50,colour = "red"))

Reproducible data:

plotdata <- structure(list(Country = c("Afghanistan", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", 
"Austria", "Azerbaijan", "Bahamas", "Bangladesh", "Barbados", 
"Belarus", "Belgium", "Belize", "Benin", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Bulgaria", "Burkina Faso", "Burundi", 
"Cambodia", "Cameroon", "Canada", "Chad", "Chile", "China", "Colombia", 
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Democratic Republic of the Congo", 
"Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", 
"Egypt", "El Salvador", "Estonia", "Ethiopia", "Fiji", "Finland", 
"France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", 
"Grenada", "Guatemala", "Guinea", "Guyana", "Haiti", "Honduras", 
"Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", 
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", 
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Lithuania", "Luxembourg", 
"Madagascar", "Malawi", "Malaysia", "Mali", "Malta", "Mauritania", 
"Mauritius", "Mexico", "Mongolia", "Montenegro", "Morocco", "Mozambique", 
"Namibia", "Nepal", "New Caledonia", "New Zealand", "Nicaragua", 
"Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea", 
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Republic of Moldova", 
"Romania", "Rwanda", "Saint Lucia", "Saint Vincent and the Grenadines", 
"Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", 
"Slovakia", "Slovenia", "South Africa", "Spain", "Sri Lanka", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", "Tunisia", 
"Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", 
"United Republic of Tanzania", "Uruguay", "Uzbekistan", "Vanuatu", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), Value = c(38.1, 38.5, 
73.8, 42.9, 97.5, -139, 73.5, -132.6, 17.8, 31.5, 99, 11.2, 100, 
9.4, 65.1, 21.4, 42.2, 25, 81.6, -28.3, -268.8, 6.4, 22.9, -7.1, 
31.4, -85.6, 3.8, 53.9, 4.3, 63.6, 96.2, 89.5, -44.8, 77.7, 98.4, 
19.8, -8.4, 98.8, 100, 65.8, 34.7, 47.4, 53.5, -138.4, 6.6, 89.2, 
-12.9, -104.4, 91.2, 71.7, 63.2, -0.3, 36.2, 32.4, 100, 52.9, 
25.5, 16.1, 82.5, 60.2, -90.5, 83.4, -6.4, 14, 36.3, 50, 54.2, 
95.9, 38.1, 99.6, 68.9, 100, -87.2, 41.1, 96.2, 18.5, -2.9, -252.5, 
92.7, 100, 60.7, -194.2, 7.3, 19.6, 4.4, 73.7, 4.3, 95.9, 58.6, 
95.3, 39.1, 28.2, 87.7, 65.7, 45.9, 69.2, 14.2, 84.9, 44.8, 40.6, 
13.7, 17.6, 44.6, 82.2, -20.2, 64.5, 100, -74.6, 54.2, 28.5, 
-18.1, 74.8, -57.1, -51.4, 34, 100, 89.6, 93.7, 43.3, -52.6, 
95.5, 38.8, -49.8, 21.6, 7.8, 36.4, 20.9, 33.5, -27, -18.6, 52.7, 
29.2, 50.4, -36.5, 21.2, 100, 65.4, 15.1, 8.9, -334.7, 100, 2.6, 
-113.1, 29.1, 100, 10.3, 92.8, -0.1, 60.3), ssp1 = c(6.87719604039575, 
-1.06100837333492, 2.59543525196996, 7.74909469087916, 1.377, 
10.3318111947289, -0.180534273291218, 10.6863086552024, -2.8434429513538, 
0.8295835298604, 14.0714872007582, 9.11373284351027, 13.834847330905, 
-0.422973062226785, -1.12106200160775, 13.5603205241274, 3.32653289854384, 
-2.39840998223208, 39.0828127635901, 5.40019194625597, -2.37642348260683, 
-1.80087476513633, 26.0569890396984, 1.8740956699509, 1.9199373912491, 
13.6775030956447, 0.91531195468484, 8.29296606482521, 14.19398140075, 
12.1672597709284, 1.06201287428673, 13.8436295615977, -1.28049911332916, 
6.36680281629815, -2.16911180718123, 2.47990039474395, 5.18645876036465, 
2.78562206607004, 4.67093204472536, 14.2178250264376, 13.9734947853149, 
3.80608747886628, 13.7526633970167, 3.37025317716788, 5.96575877565631, 
-2.60698394985916, 9.26426320499438, 6.66024582653063, -3.16241148606594, 
0.0628149886250168, 0.681560296085797, 4.32037502371113, 12.4500313543188, 
-1.07955855645901, 13.9589139439942, 13.7433812989495, -0.21431253200414, 
15.2482565847712, 12.2110460196517, 13.6752546528255, -2.08811059482957, 
3.67079209287298, 3.41664545080416, 6.92130090642129, 9.53371114259265, 
5.96513477862029, 3.98671832745579, 3.66142880690897, 1.3452892823872, 
14.1782670530732, 13.5509485401262, 2.77458006214854, 0.616970969614392, 
9.84752995853506, 1.70128642891372, 2.82563702390358, 11.772267636272, 
3.76180722567503, 0.681139075065755, 6.54103467691194, 12.7692014216183, 
2.67182954510156, -3.42062320698536, 5.27036168510605, 17.6160396053099, 
8.374852741348, -2.69282959883473, -2.56778355024776, -0.89032205839622, 
-0.585695539225483, 13.7366364475876, 0.0820039838000084, -1.90552842715223, 
5.14157480957494, 7.39861862785589, 15.2664817313064, 12.6892781227389, 
-2.78344323119485, -1.40287983117524, 12.4312519721005, 4.3698505825319, 
9.699439764416, 9.889258044527, 4.15757865847257, 11.6162007892663, 
13.1305077081529, -2.53753025307064, 10.8289058140878, 11.3367901580171, 
8.90323006110098, -1.26348619917293, 4.78331223456758, -0.506894723966887, 
-1.88455225318295, 13.0922363764111, 7.36209923785002, 13.9568271020089, 
8.42952239060954, 0.0257094692375058, -2.15177413293949, 11.6315929263403, 
6.13336692575858, -3.09064087895074, -2.85169783459753, 5.11971334941515, 
4.99811902863518, 12.7930552399297, 2.27453897486697, 5.55036600011847, 
3.56163167904334, -1.2761086913776, -0.0316805700694275, 4.25863478283838, 
10.1688044768348, 5.68472004148339, 13.9225435723809, 3.29714347391458, 
0.0787167671816411, 5.58156633046249, -0.982484142765789, 6.87196138412805, 
5.05135778134877, 8.09871120916213, 4.26725972653938, 5.661, 
8.19713984029529, 1.91549371207647, 32.1329403465877, 50.4218900613334
), ssp3 = c(20.095265606683, 10.4927314729628, 12.4376500629579, 
18.5882557266773, 1.664, 14.6353441343252, 12.0273301926656, 
16.6141977202554, 7.87973958734101, 13.3981252946914, 18.5286885419408, 
20.9150480971352, 16.7784853659327, 8.48151021407169, 8.82670438173037, 
17.0029196476309, 16.0210691894781, 8.4980873926903, 86.8001620836472, 
13.3218857281687, 7.82222823501068, 9.02507622866414, 56.9209570739615, 
15.3959976183153, 12.2348154715234, 16.9909569760022, 3.41565737951074, 
15.259126181742, 21.7075122809396, 17.9151893044619, 11.2519582951635, 
17.9032194794042, 9.63083745607234, 15.0220862355254, 7.81125224129036, 
14.5872675246356, 17.250239256418, 13.2479984997126, 6.13669645504627, 
20.1598499252078, 21.4825228142268, 12.7695343935556, 17.2424026235644, 
14.3812672415291, 14.3772246120346, 12.9911015663651, 19.8888837045986, 
15.4602995530912, 7.10400058276914, 9.09401702831024, 12.5366397029655, 
12.8376451103572, 21.6172749875706, 8.50439517946764, 17.0405117386743, 
17.4090297307004, 11.4876936836549, 20.2724386262285, 17.8509723421539, 
16.8860151519022, 7.40791011301382, 14.6978086441983, 17.5112511883045, 
17.2313021634546, 19.6967434629905, 16.0784535351506, 13.2634312821103, 
11.529923820201, 10.9782534266604, 17.7241012521462, 18.7272480044671, 
11.8629080612917, 12.3152868465769, 26.7282056624674, 15.3961288164741, 
15.6860322900639, 22.8816317431949, 16.0032167193318, 9.93834937463715, 
14.6589507197544, 17.0771884152097, 12.2403191542961, 6.48830765315486, 
17.8200553865727, 42.6177029820878, 19.4085722788309, 7.72525381293569, 
7.36474146264451, 9.09907478245312, 10.2939894844613, 17.3462228962, 
11.2179958539545, 8.89972180889966, 14.3555073601475, 20.4875910880693, 
33.4255309012712, 32.5556166859486, 9.90643453958774, 12.7450587566419, 
18.4983348192052, 20.6056299965601, 16.693991530089, 20.7770635320112, 
16.8815240811413, 19.4769132563399, 16.9724042644811, 13.1366363441094, 
20.8488626331758, 17.9274754623928, 17.3590915211959, 8.73779487641076, 
13.6790971600394, 7.7823127266393, 8.68937695011447, 32.5136500502681, 
11.1629856739021, 17.0360874770546, 17.3979143494193, 10.2854499669361, 
8.39386157432628, 31.1652263084885, 14.7356990749187, 7.67331475087449, 
8.37962523464408, 16.8181541350166, 14.0256666199924, 23.2354030548315, 
14.0958225767172, 12.0708871110594, 15.0654880792265, 8.49120231184184, 
8.98518609030765, 16.1794455230258, 18.7951484033536, 15.6383660380437, 
17.0745606749833, 11.7321928120145, 9.38762811334998, 19.7797954224522, 
8.42087168278456, 18.8103435788398, 18.6500425388502, 12.8674194023239, 
16.8676526369899, 13.229, 19.583497195285, 13.2467595038187, 
74.7040022702355, 112.823771429281), region = c("Southern Asia", 
"Southern Europe", "Northern Africa", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Latin America and the Caribbean", "Western Asia", "Australia and New Zealand", 
"Western Europe", "Western Asia", "Latin America and the Caribbean", 
"Southern Asia", "Latin America and the Caribbean", "Eastern Europe", 
"Western Europe", "Latin America and the Caribbean", "Sub-Saharan Africa", 
"Southern Europe", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Eastern Europe", "Sub-Saharan Africa", "Sub-Saharan Africa", 
"South-eastern Asia", "Sub-Saharan Africa", "Northern America", 
"Sub-Saharan Africa", "Latin America and the Caribbean", "Eastern Asia", 
"Latin America and the Caribbean", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Southern Europe", "Latin America and the Caribbean", "Western Asia", 
"Sub-Saharan Africa", "Northern Europe", "Sub-Saharan Africa", 
"Latin America and the Caribbean", "Latin America and the Caribbean", 
"Latin America and the Caribbean", "Northern Africa", "Latin America and the Caribbean", 
"Northern Europe", "Sub-Saharan Africa", "Melanesia", "Northern Europe", 
"Western Europe", "Sub-Saharan Africa", "Sub-Saharan Africa", 
"Western Asia", "Western Europe", "Sub-Saharan Africa", "Southern Europe", 
"Latin America and the Caribbean", "Latin America and the Caribbean", 
"Sub-Saharan Africa", "Latin America and the Caribbean", "Latin America and the Caribbean", 
"Latin America and the Caribbean", "Eastern Europe", "Northern Europe", 
"Southern Asia", "South-eastern Asia", "Southern Asia", "Western Asia", 
"Northern Europe", "Western Asia", "Southern Europe", "Latin America and the Caribbean", 
"Eastern Asia", "Western Asia", "Central Asia", "Sub-Saharan Africa", 
"Western Asia", "Central Asia", "South-eastern Asia", "Northern Europe", 
"Western Asia", "Sub-Saharan Africa", "Sub-Saharan Africa", "Northern Europe", 
"Western Europe", "Sub-Saharan Africa", "Sub-Saharan Africa", 
"South-eastern Asia", "Sub-Saharan Africa", "Southern Europe", 
"Sub-Saharan Africa", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Eastern Asia", "Southern Europe", "Northern Africa", "Sub-Saharan Africa", 
"Sub-Saharan Africa", "Southern Asia", "Melanesia", "Australia and New Zealand", 
"Latin America and the Caribbean", "Sub-Saharan Africa", "Sub-Saharan Africa", 
"Northern Europe", "Western Asia", "Southern Asia", "Latin America and the Caribbean", 
"Melanesia", "Latin America and the Caribbean", "Latin America and the Caribbean", 
"South-eastern Asia", "Eastern Europe", "Southern Europe", "Eastern Europe", 
"Eastern Europe", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Latin America and the Caribbean", "Western Asia", "Sub-Saharan Africa", 
"Southern Europe", "Sub-Saharan Africa", "Sub-Saharan Africa", 
"Eastern Europe", "Southern Europe", "Sub-Saharan Africa", "Southern Europe", 
"Southern Asia", "Northern Africa", "Latin America and the Caribbean", 
"Northern Europe", "Western Europe", "Western Asia", "Central Asia", 
"South-eastern Asia", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Northern Africa", "Central Asia", "Sub-Saharan Africa", "Eastern Europe", 
"Western Asia", "Sub-Saharan Africa", "Latin America and the Caribbean", 
"Central Asia", "Melanesia", "South-eastern Asia", "Western Asia", 
"Sub-Saharan Africa", "Sub-Saharan Africa"), sids = c("", "", 
"", "", "x", "", "", "", "", "", "x", "", "x", "", "", "x", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"x", "", "", "", "", "x", "x", "", "", "", "", "", "x", "", "", 
"", "", "", "", "", "", "x", "", "", "x", "x", "", "", "", "", 
"", "", "", "", "", "", "x", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "x", "", "", "", 
"", "", "", "", "x", "", "", "", "", "", "", "", "", "x", "", 
"", "", "", "", "", "", "", "x", "x", "", "", "", "x", "", "", 
"", "", "", "", "", "x", "", "", "", "", "", "", "x", "", "", 
"", "", "", "", "", "", "x", "", "", "", "")), row.names = c(NA, 
-149L), class = "data.frame")

Solution

  • you can anticipate the condition before plotting:

    plotdata[,"color"] <- "red" 
    plotdata[plotdata$sids == "x","color"] <- "black" 
    

    and then

    ggplot(plotdata, aes(x = Value)) +
      geom_point(aes(y = ssp1, colour = region),alpha = 0.2,size=10) +
      geom_text(aes(y = ssp1, label = NA),size = 10)+
      geom_point(aes(y = ssp3, colour = region),alpha=0.2,size=10) +
      geom_text(aes(y = ssp3, label = NA),size = 10)+
      geom_point(aes(y = ssp3), shape = 21,size = 12, colour = plotdata$color +
      ggnewscale::new_scale_color()  
    

    where colour = plotdata$color.

    If "x" changes you can generalizing using a generic variable.

    If then, you want absolutely a condition inside the geometry you can write:

    ggplot(plotdata, aes(x = Value)) +
      geom_point(aes(y = ssp1, colour = region),alpha = 0.2,size=10) +
      geom_text(aes(y = ssp1, label = NA),size = 10)+
      geom_point(aes(y = ssp3, colour = region),alpha=0.2,size=10) +
      geom_text(aes(y = ssp3, label = NA),size = 10)+
      geom_point(aes(y = ssp3), shape = 21,size = 12, colour = ifelse(plotdata$sids == "x","black","red")) +
      ggnewscale::new_scale_color() 
    

    best

    nic