0

I'm just trying to highlight certain data points (the red ones in the image) over others for expected years of school vs learning-adjusted years of school. The data points I'm trying to highlight are for primary US export markets in sub-Saharan Africa--there are 5. I need these points to appear over the others since they are the main points I'm trying to emphasize in relation to the rest of world and other SSA countries. I've tried subsetting the data, as well as layering those 5 points over the rest with an additional geom_point code.

If someone could help with this I would really appreciate it.Learning-adjusted vs. expected (ROW vs SSA)

LAYvsEY<-ggplot(HC,aes(expected,learning,label=HC$`Country Name`))+
  geom_point(aes(fill=Comparison),pch=21,size=10)+
  labs(x="Expected Years of School",y="Learning-Adjusted Years of School")+
  ggtitle("Learning-adjusted Education vs. Expected Years of Schooling",subtitle="Sub-Saharan Africa vs. ROW")+
  stat_smooth(method="gam",se=TRUE,color="navy",show.legend=FALSE)+geom_text(check_overlap=TRUE,family="serif")+theme_stata(base_size=12,base_family="serif")+
  guides(color=guide_legend(override.aes=list(size=7)))+scale_y_continuous(breaks=c(2,4,6,8,10,12))+
  theme(legend.title=element_blank(),legend.position=c(0.12,0.88),
        legend.background=element_rect(colour=NA,fill=NA),
        panel.border=element_rect(color="black",fill="NA"),
        plot.subtitle=element_text(face="italic"))+
  scale_fill_manual(values=c("#A6CEE3","#B2DF8A","indianred"),breaks=c("Rest of World","Sub-Saharan Africa","Primary SSA Export Market"))
Adam
  • 1

1 Answers1

0

There are a few options to help you, explained in this answer - controlling order of points in ggplot2 in R?

You could either try to create another geom_point in line with the other answer, or arrange the dataframe beforehand to put the values you would like to highlight last.

As an aside it might be easier to assist if you provide a minimal reproducible example and then others can assist with code that would work (How to make a great R reproducible example). It might be a bit difficult to try and adjust your code to get it to work without the original data.

As per your comment here is an example to see what works: Changing the order of the items in the original data works for this little example.

library(dplyr)
library(ggplot2)

# Carb values of 1 behind others
ggplot(mtcars, aes(mpg, wt, colour = as.character(carb))) +
  geom_point(size = 20)

# Arrange so that 1 is last
mtcars2 <- mtcars %>% arrange(desc(carb))

# Now value of 1 is on top of others
ggplot(mtcars2, aes(mpg, wt, colour = as.character(carb))) +
  geom_point(size = 20)

If you wanted to try in your code you could try change the order of the data in the ggplot call.

# Potential change for your code
LAYvsEY<-ggplot(HC %>% arrange(desc(Comparison)), aes(expected,learning,label=HC$`Country Name`))
Chris
  • 266
  • 1
  • 6
  • Thank you for your help. I tried to use the method in the link you provided before posting and could not get this to work. I also tried adding another geom_point--that also did not work. I would try to provide a minimal reproducible example, but my concern is that these are overly simplistic and when I try to transpose them to my code, they do not work. Much like the example in the link you provided. – Adam Jun 25 '20 at 17:18
  • Ok tried to a small adjustment that might work in your code – Chris Jun 25 '20 at 17:55
  • When I tried using the arrange function the various countries were no longer classified correctly. One thin I probably should have been more clear on is that the Comparison variable is just a field in the data file that I'm using that classifies the countries as sub-saharan Africa, Rest of World, or the primary SSA market, the latter is the one I'm trying to highlight. Is there a way to use the arrange function to put the primary SSA markets ahead of the others? – Adam Jun 25 '20 at 18:06
  • Ah you need "Primary SSA Export Market" last - can try this `HC %>% arrange(desc(Comparison))` it should hopefully re-arrange to put all those items last and on top of the other points. Re-arranging should not change the classifications in any of the other columns. – Chris Jun 25 '20 at 18:12
  • When I do that the countries are no longer classified correctly. For example, Ghana, one of the countries I am trying to highlight is classified as rest of world. I thought this may have to do with the manual legend, but even when I take that away the countries are still wrong. – Adam Jun 25 '20 at 18:19