r - How can I modify my scale on my graph axis - Stack Overflow

admin2025-04-16  0

I am very new to coding and currently attempting to make a graph (and I'm almost there). The issue is on the Y axis, the scale shows each point but I would just like the scale to read 0:00, 6:00, 12:00, 18:00, 23:00. I know that R doesn't see the points as numbers but rather text, but I'm not sure how to fix this. When I try to look up a way to fix this, I can't seem to find anything beyond adjusting limits but that doesn't work as these don't act as numbers?

This is my code if it's helpful

ggplot(
  data = WORKINGClosedData2023,
  aes(
    x = DaysSinceStart, y = ClockTime,
    colour = Sp1_Species
  )
) +
  geom_point() +
  theme_light() +
  labs(
    title = "2023 Closed Site Scavenging Events",
    x = "Days since start of trial",
    y = "Time of day",
    colour = "Species"
  )

I am very new to coding and currently attempting to make a graph (and I'm almost there). The issue is on the Y axis, the scale shows each point but I would just like the scale to read 0:00, 6:00, 12:00, 18:00, 23:00. I know that R doesn't see the points as numbers but rather text, but I'm not sure how to fix this. When I try to look up a way to fix this, I can't seem to find anything beyond adjusting limits but that doesn't work as these don't act as numbers?

This is my code if it's helpful

ggplot(
  data = WORKINGClosedData2023,
  aes(
    x = DaysSinceStart, y = ClockTime,
    colour = Sp1_Species
  )
) +
  geom_point() +
  theme_light() +
  labs(
    title = "2023 Closed Site Scavenging Events",
    x = "Days since start of trial",
    y = "Time of day",
    colour = "Species"
  )

Share asked Mar 11 at 7:00 Olivia BrannaganOlivia Brannagan 1 4
  • 1 Not sure if this is a dupe. You may be looking for scale_y_time( breaks = lubridate::hm(c("0:00", "6:00", "12:00", "18:00", "23:00")), labels = c("0:00", "6:00", "12:00", "18:00", "23:00") ) – Tim G Commented Mar 11 at 7:38
  • 3 Can you please edit your question, so that WORKINGClosedData2023 is included? You can use dput(WORKINGClosedData2023). This will make your question reproducable and allow us to see the format of clocktime. – Tim G Commented Mar 11 at 7:45
  • 3 Welcome to SO. It looks to me as if ClockTime is a character rather than a numeric or (Date)Time. I can't be sure, because you haven't shown us your data. (That's why doing so is important.). If I'm correct, the first convert ClockTime to numeric and then use scale_y_time. See this Q&A for an example of how to use scale_y_time. – Limey Commented Mar 11 at 8:20
  • I agree with Limey. If ClockTime were POSIXt or numeric class then the default scale behavior in ggplot2 would use scale_y_continuous or scale_y_datetime would produce a much more sane set of breaks/labels. With character values, ggplot2's only recourse it to plot all possible values, which in cases like this renders it unreadable (it also masks any inconsistent gaps between numbers, where c("1", "2", "9") will be displayed with even spacing despite the intuitive assumption the second gap should be much larger.) – r2evans Commented Mar 11 at 12:40
Add a comment  | 

1 Answer 1

Reset to default 0

As per @Limey's comment if we assume that your ClockTime is in character format, plotting my recreation of your data df

library(ggplot2)
set.seed(1)
df <- data.frame(DaysSinceStart = sample(50:150, 100),
                 ClockTime = format(seq(c(ISOdate(2000,3,20,1)), by = "15 min", length.out = 100), "%H:%M"),
                 Sp1_Species = sample(c("A","B"), 100, prob = c(0.8,0.2), replace = TRUE))

ggplot(
  data = df,
  aes(
    x = DaysSinceStart, y = ClockTime,
    colour = Sp1_Species
  )
) +
  geom_point() +
  theme_light() +
  labs(
    title = "2023 Closed Site Scavenging Events",
    x = "Days since start of trial",
    y = "Time of day",
    colour = "Species"
  )

looks similar to your plot:

You can use scale_y_discrete to customize your y-axis breaks

ggplot(
  data = df,
  aes(
    x = DaysSinceStart, y = ClockTime,
    colour = Sp1_Species
  )
) +
  geom_point() +
  theme_light() +
  labs(
    title = "2023 Closed Site Scavenging Events",
    x = "Days since start of trial",
    y = "Time of day",
    colour = "Species"
  ) +
  scale_y_discrete(
    breaks = c("01:00", "06:00", "12:00", "18:00", "23:00"),
    labels = c("1:00", "6:00", "12:00", "18:00", "23:00")
  )

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744810317a268238.html

最新回复(0)