0

I'd like to plot mean affective responses (-5 to 5 were possible) per time point (0, 1, 2, 3). Time point 0 is the minute 0 , time point 1 is minute 1 to 13, time point 2 is minute 14 to 23 and time point 3 is minute 24 to 33. But on the x-axis I still like to have the time (minute 0 to 35) instead of the time point. I just like to have four points in my plot for each time point. I'm only able to have points for each minute:

ggplot(data= FS2809m, aes(x = minute, y = meanFS)) +
   geom_point()+
  scale_x_continuous(expand= c(0, 0), limits = c(0, 35), breaks= c
                     (0, 5, 10, 15, 20, 25, 30, 35), name= "Zeit [min]")+
  scale_y_continuous(breaks = c(0, 1, 2, 3, 4, 5), limits= c(0, 5), name = "Feeling Scale Bewertung")+
  theme_grey()+
  labs(title = "Durchschnittliches affektives Erleben - Jungen",
       subtitle = "28.09.2021")+
  theme(plot.title = element_text(size = 12, color = "black", hjust = 0, face= "bold"),
        plot.subtitle = element_text(size = 10, color = "grey50", hjust = 0))

But I actually only want to have one point at minute 0, one at minute 1, one at minute 14 and one at minute 24. How can I proceed?

Data Frame

Data from picture

df <- data.frame(minute = 0:17,
                 timepoint = c(0, rep(1, 13), 2, 2, 2, 2), 
                 meanFS = c(3.4, rep(3.8, 13), 4, 4, 4, 4))
halfer
  • 19,824
  • 17
  • 99
  • 186
Isabell
  • 11
  • 2
  • 1
    Willkommen, Isabell! Absolutely possible! Please share a sample of your data, preferably by using `dput(head(df))`? – Pax May 26 '22 at 13:17
  • @Pax sure, but I don't know how to use dput(head(df)). I added a picture of my df to my question above, does that help? – Isabell May 26 '22 at 13:33
  • 4
    Do not add pictures of data. We can't copy/paste that into R for testing and we don't want to have to retype everything just to help you. See [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). But ggplot is great for visualizing data. If you want to summarize/filter data, you should do that before trying to plot the data. Packages like `dplyr` can help with data manipulation. – MrFlick May 26 '22 at 13:37

1 Answers1

0

I actually only want to have one point at minute 0, one at minute 1, one at minute 14 and one at minute 24.

You just need to filter the data before calling ggplot so that only the rows where minute is 0, 1, 14 or 24 are passed to ggplot:

library(dplyr)
FS2809m |> filter(minute %in% c(0, 1, 14, 24)) |>
  ggplot(aes(x = minute, y = meanFS)) +
  geom_point() +
  scale_x_continuous(expand= c(0, 0), limits = c(0, 35), breaks= c
                     (0, 5, 10, 15, 20, 25, 30, 35), name= "Zeit [min]")+
  scale_y_continuous(breaks = c(0, 1, 2, 3, 4, 5), limits= c(0, 5), name = "Feeling Scale Bewertung")+
  theme_grey()+
  labs(title = "Durchschnittliches affektives Erleben - Jungen",
       subtitle = "28.09.2021")+
  theme(plot.title = element_text(size = 12, color = "black", hjust = 0, face= "bold"),
        plot.subtitle = element_text(size = 10, color = "grey50", hjust = 0))
halfer
  • 19,824
  • 17
  • 99
  • 186
Robert Long
  • 5,722
  • 5
  • 29
  • 50
  • just curious, Robert, if there's any reason why you're using |> when you're loading dplyr anyways? – tjebo May 27 '22 at 12:11
  • 1
    @tjebo If you mean why am I using it instead of `%>%`, that's because `|>` is now part of base-R and in my opinion it is easier to read. If you mean why am I using it at all, that's because it avoids having to save the filtered data as an intermediate variable, and in my opinion it is easier to read. – Robert Long May 27 '22 at 12:48
  • 1
    I was indeed referring to "why not %>%". Thanks! I just wondered if there was any other reason rather than personal preference :) – tjebo May 27 '22 at 17:58