Order colors on geom_point for ggplot

Viewed 566

I have the following data frame:

dput(df)
 structure(list(convSlope = c(-0.00103951046958614, -0.000107780395196407, 
 1.32997761896617e-05, 2.1532762564503e-05, 6.08499370031235e-05, 
 6.23082414012136e-05, 0.00063868810766659, 0.00137104097264597, 
 0.00138245637682735, 0.00212793023058462, 0, -6.63582853150368e-05, 
 -2.60306132968389e-05, -1.9000297997136e-05, 1.80689291471755e-05, 
 2.41022501949015e-05, 7.68020027582176e-05, 0.000159577537629169, 
 0.000239631350647912, 0.00036196002617637, 0.00210600195850549, 
 0), convLow = c(-0.00864332669999781, -0.0155723308541684, -0.00638165550725293, 
 -0.00647730940467806, -0.00794574690736345, -0.00686551134820207, 
 -0.00727589426769797, -0.00488657681436086, -0.00868933527765364, 
 -0.00412964481046931, -0.0167115021724442, -0.00123221754733313, 
 -0.00373838196002056, -0.00562239375188502, -0.00160896039442434, 
 -0.00237678286364215, -0.0012737092234363, -0.0016402758202216, 
 -0.000878360581920508, -0.000871311121398701, 0.000976025059018948, 
 -0.012527643603177), convHigh = c(0.006565044642425, 0.015400484776708, 
 0.00641124352688413, 0.00651094254026463, 0.00809335314637886, 
 0.00698331294640221, 0.00856088786112379, 0.00763765126215838, 
 0.0114540113784824, 0.00838692108878951, 0.0167115021724442, 
 0.00110057404955679, 0.00368574375564408, 0.00559370035927454, 
 0.00164832488535601, 0.00242147584626002, 0.00142461464553302, 
 0.00196038270799053, 0.00136194408638254, 0.00159190939357683, 
 0.00323693188998354, 0.012527643603177), siteID = c("A", "A", 
 "A", "A", "A", "A", "A", "A", "A", "A", "A", "E", "E", "E", "E", 
 "E", "E", "E", "E", "E", "E", "E"), ID = c("Zyg", "Cho", "Pap", 
 "Bom", "Tor", "Pyr", "Las", "Noc", "Gel", "Geo", "all", "Pap", 
 "Las", "Gel", "Zyg", "Tor", "Bom", "Cho", "Noc", "Pyr", "Geo", 
 "all")), class = "data.frame", row.names = c("13", "12", "11", 
 "10", "9", "8", "6", "3", "2", "1", "14", "15", "141", "131", 
 "91", "81", "71", "51", "41", "31", "17", "16"))

I have created the following plot:

ggplot(df,aes(x= ID, y=convSlope))+
geom_hline(yintercept=0, linetype="solid", color = "grey")+ 
geom_point( col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2", 
 "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
                "#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900", 
 "#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), size= 5)+
geom_pointrange(col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", 
 "#194CB2", "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
                    "#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900", 
 "#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), aes(ymin= convLow, 
 ymax=convHigh))+
geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+ 
geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
facet_grid(. ~ siteID)+
theme_bw() + 
xlab("") + 
ylab("Frequency per year")+
theme(strip.text.x = element_text(size=12,
                                face="bold"),
    axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
    axis.title.x=element_text(size=rel(2)),
    axis.title.y=element_text(size=rel(1.75)),
    axis.text.y=element_text(size=rel(1.75)),
    panel.grid.minor=element_blank(),
    panel.grid.major.x=element_blank())

I wish for the point colors to fade from dark to light (dark= leftmost point, light= rightmost point) such that the point for "all" is the darkest point and the point for "Zyg" is the lightest point. Site A should fade from purple to light blue. Similarly, site B should fade from dark green to light green. I listed these colors in order of dark to light in the col argument in geom_point, however the colors sort differently on the graph output.

enter image description here

I need a solution to sort these colors from dark to light for each site.

2 Answers

Here's how I would achieve this:

  • Sort the dataframe by site, and then ID, so it's in the same order it's going to be in the plot
  • Add the colours to the dataframe so you know they're associated with the right rows
  • Use aes(colour=colour) and scale_colour_identity() in the actual plot
df <- df[order(df$siteID, df$ID), ]
df$colour <- c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2", 
                    "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
                    "#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900", 
                    "#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00")

ggplot(df,aes(x= ID, y=convSlope, colour = colour))+
  geom_hline(yintercept=0, linetype="solid", color = "grey")+ 
  geom_point(size= 5)+
  geom_pointrange(aes(ymin= convLow,  ymax=convHigh))+
  geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+ 
  geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
  facet_grid(. ~ siteID)+
  scale_colour_identity() +
  theme_bw() + 
  xlab("") + 
  ylab("Frequency per year")+
  theme(strip.text.x = element_text(size=12,
                                    face="bold"),
        axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
        axis.title.x=element_text(size=rel(2)),
        axis.title.y=element_text(size=rel(1.75)),
        axis.text.y=element_text(size=rel(1.75)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank())

Result:

enter image description here

A common solution to these kind of problems is to turn the column defining the color into a factor and ordering the levels as desired. In this case a solution that only requires minor changes to your code is possible: Arrange siteID to your required order before turning it into a factor in the arranged order.

df %>% 
  arrange(siteID, ID) %>%
  mutate(ID = fct_inorder(ID)) %>%
ggplot(aes(x= ID, y=convSlope))+
  geom_hline(yintercept=0, linetype="solid", color = "grey")+ 
  geom_point( col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", "#194CB2", 
                    "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
                    "#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900", 
                    "#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), size= 5)+
  geom_pointrange(col=c("#330066", "#2D0F75", "#281E84","#232D93", "#1E3DA3", 
                        "#194CB2", "#145BC1", "#0F6BD1", "#0A7AE0", "#0589EF", "#0099FF",
                        "#006600", "#0A7000", "#147A00", "#1E8400", "#288E00", "#339900", 
                        "#3DA300", "#47AD00", "#51B700", "#5BC100", "#66CC00"), aes(ymin= convLow, 
                                                                                    ymax=convHigh))+
  geom_hline(yintercept=0.01, linetype="dashed", color = "grey")+ 
  geom_hline(yintercept=-0.01, linetype="dashed", color = "grey")+
  facet_grid(. ~ siteID)+
  theme_bw() + 
  xlab("") + 
  ylab("Frequency per year")+
  theme(strip.text.x = element_text(size=12,
                                    face="bold"),
        axis.text.x=element_text(size=rel(1.2), angle=45, hjust = 1),
        axis.title.x=element_text(size=rel(2)),
        axis.title.y=element_text(size=rel(1.75)),
        axis.text.y=element_text(size=rel(1.75)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank())

enter image description here

Related