1
votes

I have created a geom_point plot with ggplot2 (R v4.0), in which points are classified by two factorial variables - 'Treatment' (colour of points) and 'Time of Year' (shape of points). The order of these two variables is correctly specified (chronologically) in the legend, but the order of individual points for 'Time of Year' in the plot does not match the order in the legend.

I've searched extensively but been unable to find a way to change the order of the points (that works). I'd be very grateful for any insights.

This is the data set:

structure(list(Treatment = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("Dry", 
"Drawdown-natural_inundation", "Drawdown-ewater", "Inundated-ewater"
), class = "factor"), TimeofYear = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L
), .Label = c("Aug.Sept", "Oct.Nov", "Dec.Jan", "Feb.Mar", "Apr.May"
), class = "factor"), predicted = c(9.09349409934063, 8.61564383754041, 
9.5157202467489, 9.60065460966658, 8.74270365273261, 8.24454674073168, 
9.18107640961278, 9.26907768652704, 9.57720737061331, 9.12472382159237, 
9.97898781938199, 10.060011680863, 8.41417418009182, 7.89531575895031, 
8.86880059605401, 8.95986948187825, 9.9788387001064, 9.54541280423986, 
10.3650624061018, 10.4430912953067), LB = c(7.87932611334752, 
6.7702957861824, 8.31338663092318, 8.22576777777575, 7.6828626517689, 
6.38852536865163, 8.02971988411625, 7.71306551965346, 8.54052535220469, 
7.57737204666066, 8.9056928198425, 8.71768315774855, 7.1098374938352, 
5.80048753638683, 7.65014600073047, 7.59589801908951, 8.41885808462554, 
7.56496679159677, 8.79707150140929, 8.70245298328878), UB = c(10.1636356619571, 
10.130238518332, 10.5823185717291, 10.8019388229304, 9.68727803838947, 
9.75355553294865, 10.2033291949699, 10.5990670630617, 10.512146724043, 
10.445314859168, 10.9475582299138, 11.2432055215275, 9.54184809547412, 
9.54077387884648, 9.93914051579127, 10.142034093227, 11.3259556840214, 
11.1803885732443, 11.7252108878204, 11.9324609229559), comb = c("Dry Aug.Sept", 
"Drawdown-natural_inundation Aug.Sept", "Drawdown-ewater Aug.Sept", 
"Inundated-ewater Aug.Sept", "Dry Oct.Nov", "Drawdown-natural_inundation Oct.Nov", 
"Drawdown-ewater Oct.Nov", "Inundated-ewater Oct.Nov", "Dry Dec.Jan", 
"Drawdown-natural_inundation Dec.Jan", "Drawdown-ewater Dec.Jan", 
"Inundated-ewater Dec.Jan", "Dry Feb.Mar", "Drawdown-natural_inundation Feb.Mar", 
"Drawdown-ewater Feb.Mar", "Inundated-ewater Feb.Mar", "Dry Apr.May", 
"Drawdown-natural_inundation Apr.May", "Drawdown-ewater Apr.May", 
"Inundated-ewater Apr.May")), row.names = c(NA, 20L), class = "data.frame")

And this is the code I'm using for plotting (points with error bars):

ggplot(TreatPredLignum, aes(x = Treatment, y = predicted)) +
  geom_point(aes(colour = Treatment, shape = TimeofYear, group = comb), 
             size = 5, position = position_dodge(0.3)) +      
  geom_errorbar(aes(ymin = LB, ymax = UB, colour = Treatment, group = comb, width = 0.2), 
                position = position_dodge(0.3)) + 
  scale_shape_manual(values = c(15, 16, 17, 18, 21), name = "Time of Year", 
                     labels = c("Aug/Sept", "Oct/Nov", "Dec/Jan", "Feb/Mar", "Apr/May")) + 
  scale_x_discrete(labels = c("Dry" = "Dry", "Drawdown-natural_inundation" = "Ddown natural", 
                              "Drawdown-ewater"= "Ddown eWater", "Inundated-ewater"= "Inund eWater")) +
  theme(legend.position = "bottom", 
        legend.box      = "vertical",
        axis.text.x     = element_text(angle = 90, vjust=0.4, hjust=1)) +
  guides(shape = guide_legend(order = 1), 
         color = FALSE) +
  ylab("Lignum Condition Score") +
  ggtitle("Lignum Condition - predicted values") +
  xlab("Treatment")

Thanks.

1

1 Answers

1
votes

You were very close. Just some bad grouping...

library(ggplot2)


ggplot(TreatPredLignum, aes(x=Treatment, 
                            y=predicted))+
   geom_point(aes(colour=Treatment, 
                  shape=TimeofYear),
              size=5,
              position=position_dodge(0.3))+
   geom_errorbar(aes(ymin=LB, 
                     ymax=UB, 
                     colour=Treatment, 
                     group=TimeofYear, 
                     width=0.2),
                 position=position_dodge(0.3) )+
   scale_shape_manual(values=c(15,16,17,18,21), 
                      name = "Time of Year", 
                      labels = c("Aug/Sept", "Oct/Nov", "Dec/Jan", "Feb/Mar", "Apr/May"))+
   scale_x_discrete(labels=c("Dry" = "Dry", 
                             "Drawdown-natural_inundation" = "Ddown natural",
                             "Drawdown-ewater"= "Ddown eWater", 
                             "Inundated-ewater"= "Inund eWater"))+
   theme(legend.position = "bottom", legend.box = "vertical")+
   theme(axis.text.x = element_text(angle = 90, vjust=0.4, hjust=1))+
   guides(shape = guide_legend(order = 0), color = FALSE)+
   ylab("Lignum Condition Score")+
   ggtitle("Lignum Condition - predicted values")+
   xlab("Treatment")