0

Here's a subset of data.

structure(list(Transmitter = c(1675L, 1675L, 1675L, 1675L, 1681L, 
1681L, 1681L, 1681L, 1685L, 1685L, 1685L, 1685L, 1685L, 9782L, 
9782L, 9782L, 24166L, 24166L, 24166L, 24166L, 24184L, 24184L, 
24184L, 24184L), Date = structure(c(17392, 17721, 17722, 17393, 
17734, 17729, 17391, 17717, 17392, 17390, 17391, 17381, 17382, 
18079, 18110, 17762, 17751, 18097, 18090, 18091, 18097, 18068, 
18082, 18088), class = "Date"), Year = c(2017L, 2018L, 2018L, 
2017L, 2018L, 2018L, 2017L, 2018L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2019L, 2019L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L), DirectionGroups = structure(c(3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L), .Label = c("Both", "Marine", "River"), class = "factor")), row.names = c(355L, 
356L, 357L, 358L, 475L, 476L, 477L, 478L, 530L, 531L, 532L, 533L, 
534L, 573L, 574L, 575L, 626L, 627L, 628L, 629L, 764L, 765L, 766L, 
767L), class = "data.frame")

I'm trying to create a scatterplot of individually tagged animals through time. Points are colored by the group I have put them in. Currently the scatterplot is sorted by the level of the Transmitter. Instead I would like a way to sort these data by the DirectionGroup.

Here is my current scatterplot.

ggplot(data = AbPlot3, aes(x = Date, y = factor(Transmitter), color = DirectionGroups)) + geom_point()+theme_bw()+ylab("Transmitter")+
  scale_color_manual(values = c('grey40', 'black', 'grey70'), labels = c('Transient', 'External', 'Resident'))+
  theme(axis.text.y = element_blank(), axis.title = element_text(size = 16),
        axis.text.x = element_text(size = 14), legend.text = element_text(size = 14),
        legend.title = element_text(size = 16))

Essentially, I want one plot with all Transient points next to each other, all external points together and all resident points together.

ljh2001
  • 391
  • 3
  • 17
  • 1
    It doesn't look like all of your sample data came through. – Ekholme Jun 10 '20 at 19:21
  • @Ekholme Sorry, should be fixed! – ljh2001 Jun 10 '20 at 19:27
  • I am not completely understanding the question. Do you want 3 charts next to each other, one each for Transient, external & resident? If so, then you will need to add `facet_grid()` or `facet_wrap()` to the ggplot definition. – Dave2e Jun 10 '20 at 19:38
  • @Dave2e Nope, just one chart. I just want the points to line up in order. For example, the 'grey40' transient points would be at the top of the figure followed by the 'black' external, and then the 'grey70' residents would be on the bottom – ljh2001 Jun 10 '20 at 20:07
  • 2
    See the FAQ [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph): "_set the factor levels to be in the desired order_". I.e. sort the data in the desired order according to "DirectionGroup". Then use _this_ order of 'Transmitter' when setting its `factor` `levels`. Also make your question easier to follow by removing the unnessesary (for you actual issue) `scale_color_manual` with `labels =`, and just use the levels found in the data. (`theme` is also redundant). – Henrik Jun 10 '20 at 20:33
  • 1
    Your description seems to indicate you're looking to facet vertically. Does adding `facet_grid(DirectionGroups~.)` kind of get you what you're looking to do? – chemdork123 Jun 10 '20 at 20:42

1 Answers1

1

Try this. As already pointed out in the comments simply sort your data by DirectionsGroup then convert Transmitter to a factor and set the order accordingly e.g. by using forcats::fct_inorder:

library(ggplot2)
library(dplyr)

AbPlot3 <- AbPlot3 %>% 
  # Sort data in the wanted order
  arrange(DirectionGroups, Transmitter) %>%
  # Convert to factor and set order according to the order in the df
  mutate(Transmitter = forcats::fct_inorder(factor(Transmitter)))

ggplot(data = AbPlot3, aes(x = Date, y = Transmitter, color = DirectionGroups)) + geom_point()+theme_bw()+ylab("Transmitter")+
  scale_color_manual(values = c('grey40', 'black', 'grey70'), labels = c('Transient', 'External', 'Resident'))+
  theme(axis.text.y = element_blank(), axis.title = element_text(size = 16),
        axis.text.x = element_text(size = 14), legend.text = element_text(size = 14),
        legend.title = element_text(size = 16))

Created on 2020-06-11 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51