'How to adjust the labels on the X axis so they are not overlapping and i still get to keep all the labels at 15 minute intervals?
dput(data_compressed)
structure(list(TIME = structure(c(1479884400, 1479884400,
1479884400,
1479884400, 1479884400, 1479884400, 1479884400, 1479884400,
1479884400,
1479884400, 1479885300, 1479885300, 1479885300, 1479885300,
1479885300
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), turn = c("AB",
"AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB",
"AB", "AB", "AB"), vehicle = c("PCL", "MCL", "CAR", "TAXI",
"LGV",
"OGV1", "OGV2", "CDB", "BEB", "OB", "PCL", "MCL", "CAR", "TAXI",
"LGV"), count = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), TIME1 = c("07:00:00", "07:00:00", "07:00:00", "07:00:00",
"07:00:00", "07:00:00", "07:00:00", "07:00:00", "07:00:00",
"07:00:00",
"07:15:00", "07:15:00", "07:15:00", "07:15:00", "07:15:00")),
row.names = c(NA,
15L), class = "data.frame")
looking to stop the labels from overlapping from eachother.
p <-ggplot(Trafic.df,
aes(x = count,
y = TIME1,
fill=vehicle
)) +
scale_fill_manual(values=
c("#4A24B2","#C99E96","#EED731","#0E0E0E","#F0990D","#E90909",
"#D6CCCC","#87501C","#66c2a5","#FF00AE")) +
geom_col() +
facet_wrap(~vehicle)
p
I've tried the following to improve the overlapping and it stopped the overlapping but in doing so has removed some of the labels so it is in 45 minute intervals. is there anyway i can get the labels on the y axis from not overlapping but keep all the labels at the same time?
scale_y_discrete(guide = guide_axis(check.overlap = TRUE))+
Here is a picture of the graph so you know what im talking about.

Solution 1:[1]
One simple solution is to change the width and height of the image when saving it, which will make your plot easier to read. Another solution would be the use of scale_x_datetime() with breaks that will request TIME1 to de defined as as.POSIXct(TIME1) I provided a solution here
Sample code:
p <-ggplot(Trafic.df,
aes(x = count,
y = TIME1,
fill=vehicle
)) +
scale_fill_manual(values=
c("#4A24B2","#C99E96","#EED731","#0E0E0E","#F0990D","#E90909",
"#D6CCCC","#87501C","#66c2a5","#FF00AE")) +
geom_col() +
facet_wrap(~vehicle)+
theme(panel.spacing.x = unit(1, "cm"))+
theme(axis.text.x = element_text(angle=90, hjust = 1, face="bold", size=12, color="black"),
axis.title.x = element_text(face="bold", size=16, color="black"),
axis.text.y = element_text(face="bold", size=12, color="black"),
axis.title.y = element_blank(),
strip.text = element_text(size=10, face="bold"),
plot.title = element_text(size=20, face="bold"),
legend.position = "right")
p
Plot:
Sample data:
Trafic.df<-structure(
list(
TIME = structure(
c(
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479884400,
1479885300,
1479885300,
1479885300,
1479885300,
1479885300
),
class = c("POSIXct", "POSIXt"),
tzone = "UTC"
),
turn = c(
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB",
"AB"
),
vehicle = c(
"PCL",
"MCL",
"CAR",
"TAXI",
"LGV",
"OGV1",
"OGV2",
"CDB",
"BEB",
"OB",
"PCL",
"MCL",
"CAR",
"TAXI",
"LGV"
),
count = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
TIME1 = c(
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:00:00",
"07:15:00",
"07:15:00",
"07:15:00",
"07:15:00",
"07:15:00"
)
),
row.names = c(NA,
15L),
class = "data.frame"
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

