JammingThebBits
JammingThebBits

Reputation: 752

How to show the ticks' labels on an axis while having single data point in R using ggplot?

I have a dataframe with one row, i'd like to show it when the horizontal axis is of type datetime. for some reason when I have a single dot, there are no ticks on the horizontal axis.

table_hr_tags_per_bin <- data.frame(matrix(c("2018-11-21 12:40:35", "25"),nrow = 1,ncol = 2))
colnames(table_hr_tags_per_bin) <-c('StartTimeStamp', 'cars')

plot_conf = ggplot() +
  geom_point(data = table_hr_tags_per_bin, aes_string(x='StartTimeStamp', y= "cars"),colour = "red", size=3) +
  labs(subtitle="plot_name", 
       y="y_axis_name", 
       x="Time", 
       title="my mitle", 
       caption = "") +
  theme(axis.text.x = element_text(angle = 80, hjust = 1)) +
  scale_x_datetime(date_breaks  = paste0(4," sec"), label=function(x) substr(x,12,19))+ 
  scale_y_continuous(breaks=waiver())

plot(plot_conf)

The problematic output is shown below:

enter image description here

Any suggestion would be helpful!

Upvotes: 0

Views: 63

Answers (1)

MarBlo
MarBlo

Reputation: 4514

Maybe I am wrong in anticipating what you mean, if not, I think your datetime and scale_x_datetime use is not right.

If you use lubridate package and the right format for dates, it probably is much easier to get what you want. I have added a second date with a second value for coming nearer to what you wanted with just showing one single point.

library(lubridate)
df <- tibble(dt=c("2018-11-21T12:40:35", 
                  "2018-11-22T12:41:35"),
             value=c("25", "26"))

ggplot(df %>% filter(dt < "2018-11-22T12:41:35"), aes(dt, value)) + geom_point()

Upvotes: 1

Related Questions