1

I have a plot with an unreadable x scale:enter image description here my code is:

library(ggplot2)
print(ggplot(dfComb,aes(x=hrmin,y=count,fill = dfComb$word)) +geom_bar(alpha = 0.5,stat='identity')+
        xlab("Minute of Day") +
        ylab("Count") +
        ggtitle(paste("Frequencies of Tweets by minute from tweetsORC1")) +
        scale_fill_hue() +
        scale_colour_hue() +
        # labs(fill="Word"))
scale_fill_discrete("Word", 
                    breaks=c("A","B"), 
                    labels=c("yes","no" ))+
scale_x_continuous(breaks=seq(00:00, 23:59, 60))

)

AS you can see I have tried limiting the labels (as per this) using

scale_x_continuous(breaks=seq(00:00, 23:59, 60))

But this does not work since my data is of the form:

 dput(head(dfComb))
structure(list(hrmin = c("0:00", "0:03", "0:06", "0:08", "0:18", 
"0:20"), count = c(1, 1, 1, 1, 1, 1), word = c("B", "B", "B", 
"B", "B", "B")), .Names = c("hrmin", "count", "word"), row.names = c(NA, 
6L), class = "data.frame")

where hrmin is of the form HH:MM i.e. the time of day from 0:00 to 23:59. I want labels that only designate each hour so 1 AM to Midnight, there being just 24 labels in total. Any pointers on how to handle this kind of data is much appreciated.

schoon
  • 2,858
  • 3
  • 46
  • 78
  • 3
    You have not provided a reproducible sample of your actual data. (Use `dput(head())` & paste the results here). This is particularly problematic in this case as date / time variables can be expressed using numerous different classes, & solutions may not fit every case. Without a sample we are troubleshooting in the dark. – Z.Lin Sep 06 '17 at 13:57
  • I have edited the question to show my data. – schoon Sep 06 '17 at 14:55
  • @Marco Sandri, thanks, but gives: `Error in as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format > `. – schoon Sep 06 '17 at 14:57
  • `hrmin` is of type character which will be plotted on a discrete axis. Try to convert this to `POSIXct` and ggplot will use a continuous axis with nicely placed labels. – Uwe Sep 06 '17 at 16:57
  • Possible duplicate of [Converting date and time with POSIX](https://stackoverflow.com/questions/45962190/converting-date-and-time-with-posix) – Uwe Sep 06 '17 at 17:07

3 Answers3

1

Maybe you could try to convert the "hrmin" variable to an hour within the aes() function?

Using the lubridate package, I suspect it would look something like this:

install.packages("lubridate")
library(lubridate)
print(ggplot(dfComb,aes(x=hour(hrmin),y=count,fill = dfComb$word)) +geom_bar(alpha = 0.5,stat='identity')+
    xlab("Hour of Day") + ....  
  • Thanks. It gives: `Error in as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format` and won't that just plot the hours? I want all the minutes plotted. – schoon Sep 06 '17 at 13:44
  • ***If*** `hrmin` would be in a standard unambiguous format (which it isn't), then you could do something like `hour(hrmin) + minute(hrmin) / 60`. – Uwe Sep 06 '17 at 17:04
1

Not tested, since there's no example dataset, but maybe the following could work.

# Get the hour part, prior to calling 'ggplot'
hr_lab <- as.integer(strftime(paste(Sys.Date(), dfComb$hrmin), format = "%H"))

# then use
scale_x_continuous(breaks = seq(hr_lab[1], hr_lab[length(hr_lab)]))

If this doesn't work, sorry in advance for the noise. Just say so and I'll delete it.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thanks. Gave: `Error: Discrete value supplied to continuous scale`. I changed it to `scale_x_discrete` which ran ok, but did not give any labels. – schoon Sep 07 '17 at 06:01
0

For anyone else who arrives here simply after times from 00:00 to 23:00:

library(dplyr)
0:23 %>% { ifelse(nchar(.) == 1, paste0("0", .), .) } %>% paste0(., ":00")

which gives

"00:00" "01:00" "02:00" "03:00" "04:00" "05:00" "06:00" "07:00" "08:00" "09:00"
"10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "16:00" "17:00" "18:00" "19:00"
"20:00" "21:00" "22:00" "23:00"
stevec
  • 41,291
  • 27
  • 223
  • 311