0

When I plot using ggplot I get grey vertical lines on my plot before data chart. Any ideas on how to remove it would be highly appreciated.

ggplot(fitbit_data, aes(x = Date, y = Steps)) + 
  geom_bar(stat = "identity", fill = "green") + 
  labs(title = "My Steps", subtitle = " June - Dec 2019", 
       x = " Date", y = "Steps") + 
  scale_x_date(
    date_labels = "%b\n%Y",
    date_breaks = "1 month",
    limits = c(as.Date("2019-06-01"), as.Date("2019-12-31"))
  )

bar plot with redundant lines at front

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
MinR
  • 3
  • 3
  • 1
    Can you add an image of the graph you are currently obtaining in order people can visualize your issue ? Also, can you add a small reproducible example of your data ? (see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Dec 25 '19 at 15:34
  • Fastest way would be adding `+ theme_classic()` or `+ theme_minimum()` to your code. Otherwise modify these `panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y` https://ggplot2.tidyverse.org/reference/theme.html – Tung Dec 25 '19 at 16:16
  • 2
    Btw I suspect your `Steps` are not numeric. Run `str(fitbit_data)` or `summary(fitbit_data)` to check – Tung Dec 25 '19 at 16:17
  • 1
    I guess the data imported is somewhere converted to factors (perhaps even when loading the data) which leads to a categorical y-axis, that then appears with overlapping labels that look like those grey columns. – TobiO Dec 25 '19 at 17:42
  • Thank you all, you are correct. Didn't realize steps data type got changed during import. It works now :) – MinR Dec 26 '19 at 07:54

1 Answers1

0

Likely the data is converted to factor, thus ggplot shows a categorical y-axis, that then appears with overlapping labels that look like those grey columns.

When reading the data make sure to use

df= read.table(..., 
               # assign appropriate data types by using
               colClasses = c(...),
               ... ,
               # it can also be adviseable to use
               stringsAsFactors = FALSE)
TobiO
  • 1,335
  • 1
  • 9
  • 24