Geom points change position when changing color

I have a plot with multiple elements,
see below:

enter image description here

The code for this is

df2 <- data %>% group_by(presence,class,Wolf) %>% summarise(roaddensity_mean=mean(roaddens),dens_se_group=standard_error(roaddens))
df3 <- df2 %>% group_by(presence,class) %>% summarise(roaddensity_mean=mean(roaddensity_mean),dens_se_group=mean(dens_se_group))

  p1 <- ggplot(df1,aes(x=class,y=roaddensity_mean,color=presence)) +
  geom_errorbar(data=df3,aes(ymin=roaddensity_mean-1.96*dens_se_group, ymax=roaddensity_mean+1.96*dens_se_group,color=presence),
   width = 0.4,size=1,position=position_dodge(width=0.5))+
  labs(title='',y="Road density/km²",x="") +
  geom_point(data=df2,stat='identity',position=position_dodge(width=0.5),size=1.25,alpha=0.4) + ylim(0,2) + 
  scale_color_manual(values=c("#40B0A6","#E1BE6A"))+  
  theme(axis.text.x=element_blank(),legend.direction = "horizontal",plot.margin = unit(c(0.35, 0.5, 0, 0.5), "cm"),
        axis.title = element_text(size=11),axis.text=element_text(size=10)) + guides(color=guide_legend(title="")) +
  theme(panel.background = element_rect(fill = 'white'),panel.grid.major = element_line(color = 'gray'), panel.grid.minor = element_line(color = 'gray')) 

All I’m trying to do is plot the geom_points in a black color, but when I try to do this it also changes their position to the middle, as it stops separating them by ‘presence’?

enter image description here

So I’m trying to find a slight tweak to the code to just have the geom points in black and keeping everything else the same as in the orginal plot, but haven’t found a solution

Input for DF1

structure(list(presence = structure(c(3L, 3L, 3L, 4L, 4L, 4L), levels = c("0", 
"1", "Used", "Available"), class = "factor"), class = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), levels = c("Resident", "Floater", "Disperser"
), class = "factor"), roaddensity_mean = c(0.644340794888985, 
0.688824654672777, 0.564575889060891, 0.912037176999315, 0.838949052923906, 
0.802653338954428), dens_se = c(0.00844381665550316, 0.0148649612280198, 
0.0248181112925504, 0.00602487059941883, 0.010051939708459, 0.0188940407637902
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(presence = structure(3:4, levels = c("0", 
"1", "Used", "Available"), class = "factor"), .rows = structure(list(
    1:3, 4:6), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

df2

structure(list(presence = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L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    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Resident", 
    "Floater", "Disperser"), class = "factor"), Wolf = structure(c(2L, 
    3L, 5L, 7L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 28L, 29L, 30L, 31L, 
    32L, 33L, 34L, 36L, 37L, 39L, 40L, 41L, 42L, 44L, 45L, 47L, 
    48L, 52L, 53L, 54L, 55L, 56L, 57L, 60L, 62L, 63L, 64L, 65L, 
    66L, 68L, 69L, 70L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
    84L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 95L, 96L, 1L, 
    4L, 8L, 27L, 35L, 38L, 43L, 44L, 46L, 47L, 50L, 51L, 58L, 
    59L, 60L, 61L, 67L, 71L, 72L, 81L, 82L, 83L, 85L, 94L, 2L, 
    3L, 6L, 9L, 15L, 21L, 26L, 37L, 40L, 44L, 47L, 49L, 56L, 
    64L, 65L, 77L, 78L, 88L, 93L, 96L, 2L, 3L, 5L, 7L, 10L, 11L, 
    12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
    24L, 25L, 26L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 36L, 37L, 
    39L, 40L, 41L, 42L, 44L, 45L, 47L, 48L, 52L, 53L, 54L, 55L, 
    56L, 57L, 60L, 62L, 63L, 64L, 65L, 66L, 68L, 69L, 70L, 73L, 
    74L, 75L, 76L, 77L, 78L, 79L, 80L, 84L, 86L, 87L, 88L, 89L, 
    90L, 91L, 92L, 93L, 95L, 96L, 1L, 4L, 8L, 27L, 35L, 38L, 
    43L, 44L, 46L, 47L, 50L, 51L, 58L, 59L, 60L, 61L, 67L, 71L, 
    72L, 81L, 82L, 83L, 85L, 94L, 2L, 3L, 6L, 9L, 15L, 21L, 26L, 
    37L, 40L, 44L, 47L, 49L, 56L, 64L, 65L, 77L, 78L, 88L, 93L, 
    96L), levels = c("1", "3", "4", "5", "7", "8", "10", "11", 
    "14", "15", "16", "17", "42", "46", "50", "51", "52", "54", 
    "55", "56", "60", "62", "63", "64", "65", "66", "71", "74", 
    "76", "86", "88", "90", "91", "94", "95", "98", "100", "102", 
    "103", "104", "134", "135", "146", "147", "149", "150", "153", 
    "158", "160", "162", "163", "164", "165", "166", "169", "174", 
    "175", "176", "178", "179", "180", "181", "183", "190", "193", 
    "196", "197", "198", "199", "200", "202", "204", "206", "211", 
    "213", "214", "215", "218", "219", "220", "222", "224", "225", 
    "228", "263", "326", "330", "333", "334", "344", "347", "349", 
    "376", "377", "378", "380"), class = "factor"), roaddensity_mean = c(1.01589281216281, 
    0.594955804269291, 0.820048833717715, 0.55543689630627, 0.735175877030779, 
    0.703294463723088, 0.14717971660192, 1.15301514573886, 1.26623189978458, 
    0.549081708609647, 0.418795585546052, 0.403465922430855, 
    0.552670369363893, 0.90712539802107, 0.219708511564515, 0.621113503325107, 
    0.289661842043862, 0.684319651528335, 0.897937723875893, 
    0.628718060718654, 0.704773689632962, 0.513728168496973, 
    0.605063942092509, 0.473228704909802, 0.684494478365975, 
    0.454758349651386, 0.610760625350926, 1.40638806315728, 0.680758272259927, 
    0.677653081229325, 0.699041874218408, 0.305608323414585, 
    0.58758979574581, 0.280901220315442, 1.14351465273216, 0.822815118325133, 
    1.26293996527918, 0.313897865894127, 0.325471806914118, 0.438288001966973, 
    0.512769336298429, 0.159304099098402, 0.917618444896924, 
    0.499845431978498, 0.27858449166646, 0.431125027741936, 0.514363695505618, 
    0.37592877, 0.777263062833333, 0.678623857172131, 0.403902668145695, 
    0.615586223930578, 0.32515041596, 1.22008627690865, 0.917855678814433, 
    0.619433429458484, 0.669039640526316, 0.569446113653137, 
    0.641389031019802, 0.584311186444444, 0.799582387179487, 
    0.921402927603306, 0.598648375609756, 0.638776112987013, 
    1.72535020486111, 0.423487118478261, 1.44174572, 1.45103335671429, 
    0.952027997281553, 0.566182278703704, 0.601641233340541, 
    0.4561838955, 0.297159788337017, 0.575043946292997, 0.784421925196698, 
    1.0756901502733, 0.242521787293987, 0.958427589800432, 0.963409657878619, 
    0.330738935942967, 0.470192939148807, 0.305637823709164, 
    0.83217025639387, 0.794780621642058, 0.52595742248153, 0.579742455788221, 
    0.59310353713696, 0.684860984038462, 0.562176052525252, 0.935382558741497, 
    0.274828115645412, 2.56176947173913, 1.4703750421365, 0.867103765254237, 
    0.605866101496881, 0.757209240576923, 0.466572599359278, 
    0.560883359323986, 0.506589530136473, 0.53688361106288, 0.725607777428679, 
    0.484256128060912, 1.1897541538805, 0.53071304289513, 0.585206982563745, 
    0.686393561991724, 0.0986971871707839, 0.325040857634391, 
    0.54632444986142, 1.15860202083333, 0.83763202, 0.53068827127907, 
    0.502528869591837, 1.0580214262069, 0.901614713888889, 1.24116076666667, 
    0.803489099571755, 1.05253806888083, 1.42030557630271, 1.16279895348698, 
    1.26506284609121, 0.836165797338182, 0.374642676177875, 1.43523645983108, 
    1.27373342071722, 0.821745618034996, 1.23245880134607, 0.806206836738491, 
    1.01923115149509, 1.14286271550372, 0.557842426118368, 1.23451094279995, 
    0.643190695812293, 1.08011156449041, 0.767787000552506, 1.36752377218757, 
    1.12935034976995, 0.779484544217097, 0.827342934648422, 0.442502099863712, 
    0.767283284013841, 0.550664964493328, 0.917612235251008, 
    1.75336960875976, 0.964158644548036, 1.24157398007387, 1.02960117665735, 
    0.794035347313041, 0.692222899809873, 0.649439241638017, 
    1.10690712323597, 0.735583967860472, 1.40255056638556, 0.391800241516151, 
    0.603378941412544, 0.718763503232408, 0.278994314091257, 
    0.752677874538609, 1.18614248824571, 0.799841732520478, 0.421251035696545, 
    0.94776770311828, 0.837913398089888, 0.880360207777778, 1.16061023714583, 
    0.876386522486339, 0.69635604207064, 1.11581526020661, 0.504473584586667, 
    1.06860284314845, 0.936200598075601, 0.764524808580024, 0.737221095559211, 
    0.781386104526445, 1.10480912293894, 1.24657842874074, 0.92387933017094, 
    0.986628866198347, 0.955723391829268, 0.838097012008658, 
    1.63504397134722, 0.668303167344203, 1.3680033925669, 1.09550073003061, 
    1.17894855346278, 1.05314801953704, 0.539820512702703, 1.45826308247619, 
    1.18209949158435, 0.864129982277444, 1.17323282656875, 1.24788692091735, 
    0.671439761816646, 1.24308202079898, 1.12835015705314, 0.539495887609586, 
    1.07620662385206, 0.727363152918715, 0.685308526395294, 0.726248872844029, 
    0.885994242446795, 0.868293179888237, 0.866858170202046, 
    0.816519305432692, 1.04323416645791, 1.13800795826531, 0.322286807734578, 
    1.70188114821087, 0.994621354803165, 0.805079977231638, 0.549211849882883, 
    0.801160731602564, 0.865038681796107, 1.22574923104707, 0.715635927837216, 
    0.605409770365431, 1.01340565731717, 1.12639180035336, 1.34325851267476, 
    3.12012260886868, 0.538180722953443, 0.953902615287807, 1.10997799360896, 
    0.312698965003995, 0.828883656105216, 0.778786083611111, 
    1.08046257082667, 0.809625998178295, 1.27607253170068, 1.30289346011494, 
    0.907333581481481, 1.13577995905983), dens_se_group = c(0.121252630289307, 
    0.06307522686269, 0.0420148859360373, 0.0623425234164211, 
    0.0722047815095388, 0.0722190238317927, 0.034165011272962, 
    0.0936790859814306, 0.0714356396741611, 0.0570436810778115, 
    0.0788899930828033, 0.0475608654800484, 0.0598258300666842, 
    0.130259750986686, 0.0334983354274925, 0.0761826236952826, 
    0.0453655791167425, 0.106781997714366, 0.040836244508566, 
    0.0588199042229267, 0.0918996412489127, 0.059357285207517, 
    0.0627450154452284, 0.0931376976250813, 0.084292452116752, 
    0.0411589571700968, 0.0450637305076403, 0.143482758772818, 
    0.0590540839343392, 0.102088012024323, 0.105528090687969, 
    0.0411070052757566, 0.0590357031703664, 0.0413723778927877, 
    0.0939695841562126, 0.113708033475645, 0.158663845039196, 
    0.0301738069049785, 0.0481176785452355, 0.0475410335753984, 
    0.0485730649599259, 0.0337475523712995, 0.0931442960086653, 
    0.0438977172489599, 0.0424171746665752, 0.0738349440137023, 
    0.0913272748385275, 0.0627775731632729, 0.0750793002689392, 
    0.0644990405314962, 0.0681619458121342, 0.0641801830023756, 
    0.0438174706972614, 0.0576257130602113, 0.139342575103628, 
    0.0663789070872911, 0.0863145096716977, 0.0600985558206582, 
    0.0764272563148116, 0.169826259735112, 0.184240678059479, 
    0.114217210628983, 0.077105857002114, 0.0917979995962206, 
    0.130375538952498, 0.0831714673344984, 0.155637910054504, 
    0.182334782866416, 0.121781647284439, 0.0905464615847127, 
    0.0794797909580277, 0.0794262146070196, 0.0690763715519387, 
    0.054177580544246, 0.133886131515446, 0.0975198400831905, 
    0.060948263509353, 0.0592085516548547, 0.0964065218316166, 
    0.0566068972354325, 0.0705188476471073, 0.059090116217133, 
    0.0704901062387426, 0.0772605951309048, 0.079904976311246, 
    0.0341781918365715, 0.0693474430214774, 0.0736837558846294, 
    0.0909932562984428, 0.0770188131260058, 0.0321224404695654, 
    0.426352360863202, 0.0860904764680956, 0.117344075287764, 
    0.047135117351449, 0.087796458703135, 0.195647691770468, 
    0.107645404295871, 0.0638257581164984, 0.0574054873999136, 
    0.130632708387773, 0.15445538200891, 0.420782131758654, 0.53071304289513, 
    0.129449647950927, 0.0966976588064522, 0.0478244103175173, 
    0.0500911026711741, 0.0758923191462454, 0.266146674296548, 
    0.201048417318756, 0.100258152229103, 0.143653740787346, 
    0.242629918781001, 0.243947309121107, 0.209007706870181, 
    0.0678651740892763, 0.0500516714724084, 0.031780642986091, 
    0.0537257936991515, 0.0653501464247892, 0.0493304633238825, 
    0.0336688977463781, 0.0612308683973206, 0.0455720034294281, 
    0.0436259344304239, 0.0943512454837203, 0.0410030689363069, 
    0.0451293352510801, 0.0896445586179648, 0.0307747235605067, 
    0.0734208222538278, 0.0394913939343875, 0.0765409225429585, 
    0.025374441063364, 0.0444649470099386, 0.0730495689397327, 
    0.0459619709615132, 0.0433532922641296, 0.053082585844426, 
    0.0504231803684409, 0.029436186635978, 0.0315384102925965, 
    0.100155129745926, 0.0425515004455708, 0.0809255503046289, 
    0.076741229624734, 0.0404474839627097, 0.0502691651068222, 
    0.0367618437292748, 0.0543715894162467, 0.0644420393285048, 
    0.102853262043496, 0.0209757541932988, 0.0379067741150279, 
    0.0388374571177147, 0.0224026507951449, 0.0488595669578494, 
    0.0607984484981257, 0.0308073909184775, 0.0355765592047844, 
    0.0672714122816897, 0.0678947295042004, 0.0724137869735462, 
    0.0498994605948105, 0.0429375623756504, 0.0508074557193626, 
    0.0503967777899689, 0.0359045343803192, 0.0313416900900793, 
    0.074412171894048, 0.0444861195437179, 0.0524949430539524, 
    0.0433144020179389, 0.0669415347792242, 0.139758451102717, 
    0.126410741700456, 0.0740585372205941, 0.0633466036963939, 
    0.0540614327666959, 0.0846801874393233, 0.0735049005380994, 
    0.0850690151236804, 0.0847073151505744, 0.0734745870049583, 
    0.0729637582416377, 0.0420837212484981, 0.0831133972681497, 
    0.0704433708286561, 0.0407420105367455, 0.0935582039895798, 
    0.0651900689745068, 0.0684379765984847, 0.0432801023022752, 
    0.067455128176762, 0.045395354824592, 0.0713098680424737, 
    0.054211706995267, 0.0410346211598702, 0.0431101126046647, 
    0.0594260657636561, 0.0252432542452958, 0.0515760061224177, 
    0.0523099371769159, 0.0780564045285394, 0.0550242094269161, 
    0.0212900616785109, 0.198414884937002, 0.0460065121485867, 
    0.0680413344006127, 0.0367297232554462, 0.0565383472751982, 
    0.15930206668028, 0.11106227940454, 0.0473257701721107, 0.0363120820477224, 
    0.0959890189790112, 0.132682010408609, 0.180161941009971, 
    0.678985467450568, 0.0890767154343322, 0.0663485795199558, 
    0.145044628112251, 0.0299757777722962, 0.0566222512010285, 
    0.147162248659636, 0.160968063668646, 0.082616400974738, 
    0.154240136781489, 0.170831374701335, 0.118322836278145, 
    0.139302355825633)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -232L), groups = structure(list(
    presence = structure(c(3L, 3L, 3L, 4L, 4L, 4L), levels = c("0", 
    "1", "Used", "Available"), class = "factor"), class = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L), levels = c("Resident", "Floater", "Disperser"
    ), class = "factor"), .rows = structure(list(1:72, 73:96, 
        97:116, 117:188, 189:212, 213:232), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))

>Solution :

You need to add a group = presence to keep the grouping:

library(ggplot2)


p1 <- ggplot(df1,aes(x=class,y=roaddensity_mean, group = presence)) +
  # geom_errorbar(data=df3,aes(ymin=roaddensity_mean-1.96*dens_se_group, ymax=roaddensity_mean+1.96*dens_se_group,color=presence),
  #               width = 0.4,size=1,position=position_dodge(width=0.5))+
  labs(title='',y="Road density/km²",x="") +
  geom_point(data=df2,stat='identity',position=position_dodge(width=0.5),size=1.25,alpha=0.4) + ylim(0,2) + 
  scale_color_manual(values=c("#40B0A6","#E1BE6A"))+  
  theme(axis.text.x=element_blank(),legend.direction = "horizontal",plot.margin = unit(c(0.35, 0.5, 0, 0.5), "cm"),
        axis.title = element_text(size=11),axis.text=element_text(size=10)) + guides(color=guide_legend(title="")) +
  theme(panel.background = element_rect(fill = 'white'),panel.grid.major = element_line(color = 'gray'), panel.grid.minor = element_line(color = 'gray'))

p1
#> Warning: Removed 2 rows containing missing values (geom_point).

Created on 2023-03-17 by the reprex package (v1.0.0)

Leave a Reply