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 Answer
Reset to default 0As 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")
)
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:38WORKINGClosedData2023
is included? You can usedput(WORKINGClosedData2023)
. This will make your question reproducable and allow us to see the format of clocktime. – Tim G Commented Mar 11 at 7:45ClockTime
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 convertClockTime
to numeric and then usescale_y_time
. See this Q&A for an example of how to usescale_y_time
. – Limey Commented Mar 11 at 8:20ClockTime
werePOSIXt
ornumeric
class then the default scale behavior inggplot2
would usescale_y_continuous
orscale_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, wherec("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