Selamola Tloubatla
Selamola Tloubatla

Reputation: 38

How do i order by Variable time

Hi can someone help me on how to order the plot by Time(realtime) and make the top of the plot higher(Alter my x-axis) thanks

`

ggplot(timessum, aes(timessum$Row.Labels, timessum$realtime))+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0))+
  geom_text(aes(label=timessum$Average.of.total_hours_Worked),hjust=0.5, vjust= -2, size = 2.5)+
  ggtitle("Earliest End Time(P/RA)")+
  xlab("Field Worker") +
  ylab("Time") +
  geom_bar(stat='identity', aes(fill = timessum$Row.Labels))

`

Here's the plot

Upvotes: 0

Views: 30

Answers (1)

teunbrand
teunbrand

Reputation: 38013

The easiest thing is to encode the variable on the x-axis as a factor with the levels attribute in the correct order. Example with some dummy data below:

library(ggplot2)

df <- data.frame(
  x = LETTERS[1:26],
  y = rnorm(26)
)

df$x <- factor(df$x, levels = df$x[order(df$y, decreasing = TRUE)])

ggplot(df, aes(x, y)) +
  geom_col()

Created on 2020-12-11 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions