0

Please help! R homework

I've been trying for hours to find a solution that will help me put "train" and its relevant data before "test" on the x axis.

I've tried to relevel() my factors and order them by decreasing = FALSE, but I am still unable to swap the positions of "test" and "train".

The code I'm running for this ggplot is:

ggplot(all.dat, aes(x = data, y = r2)) + 
  geom_point(colour = "grey") + 
  geom_pointrange(data = final.plot, aes(x = data, y = r2, ymin = se.lower, ymax = se.upper, colour = data), size = 1) + 
  scale_color_discrete(breaks = c("train", "test")) + 
  theme_bw()

This is the last question for my assignment, and I just want to be done with it :( I would really appreciate if anyone could help me solve this issue!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Claire
  • 1
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Where did you put the relevel? What about `scale_x_discrete(breaks = c("train", "test"))`? – MrFlick Feb 23 '22 at 19:06
  • you can `mutate` and use `fct_reorder()` to do that before putting it in ggplot. Check here: https://forcats.tidyverse.org/reference/fct_reorder.html – Ruam Pimentel Feb 23 '22 at 19:08
  • 1
    Hard to say without more infos, like what’s in the data. The results of a str() call would help. Btw just using the level arguments in factor() to define the order should work unless some relevant information is missing. – Bakaburg Feb 23 '22 at 19:16
  • @MrFlick I've included `scale_x_discrete(breaks = c("train", "test"))` before `theme_bw()`, but it didn't work :/ – Claire Feb 24 '22 at 01:20
  • @Bakaburg It's really strange because I have used the `factor()` argument in the code before reaching my ggplot, and the data frame it churns out leads with "train" and not "test", so I'm not sure what it is that's incorrect :/ – Claire Feb 24 '22 at 01:33
  • @RuamPimentel I've done `mutate()` prior to my ggplot but not `fct_reorder()`, which doesn't work out in my code? Here's my code for reference: `final.plot <- final.dat %>% mutate(se.lower = final.dat$r2 - final.dat$sd, se.upper = final.dat$r2 + final.dat$sd) final.plot ggplot(all.dat, aes(x = data, y = r2)) + geom_point(colour = "grey") + geom_pointrange(data = final.plot, aes(x = data, y = r2, ymin = se.lower, ymax = se.upper, colour = data), size = 1) + scale_color_discrete(breaks = c("train", "test")) + theme_bw()` – Claire Feb 24 '22 at 01:38
  • Sorry it should have been `scale_x_discrete(limits = c("train", "test"))`. It's hard to remember that's why its much easier if you provide data we can test with to make sure answers work. – MrFlick Feb 24 '22 at 01:47
  • @Claire, `fct_reorder()`should be inside of `mutate()`. Could you submit an example data as @MrFlick suggested in the first comment? This will help us to help you because then we can post the exact solution for your specific data. – Ruam Pimentel Feb 24 '22 at 04:11
  • that's why seeing the output of str() just before the data is passed to ggplot would help – Bakaburg Feb 24 '22 at 09:17
  • ok, I see something strange here `geom_pointrange(data = final.plot, aes(x = data, y = r2, ymin = se.lower, ymax = se.upper, colour = data), size = 1)` you change the data argument to `final.plot`, but `data` in `final.plot` and in `all.data` are not necessarily the same. if one is a factor and the other character or factor with different level ordering I don't know what would happen. PS: you don't need to repeat `final.dat$` in `mutate()`; `data` as name for a dataframe column is quite a bad idea and will confuse whoever read your code. – Bakaburg Feb 24 '22 at 09:22

0 Answers0