Reputation: 499
I would like to get a bar chart that shows a certain quantity per day. However, ggplot is not showing the days seperatly, but summerizes per month. How do I get ggplot to break x-axis on each day?
This is the code that I am using:
ggplot(aes(x=d.data$a, y= d.data$b), data = d.data) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_x_date(breaks = '1 day') +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
After running the code above the following error occurs:Error in strsplit(unitspec, " ") : non-character argument
The following data is used as input:
d.data <- structure(list(a = structure(c(16771, 16772, 16773, 16774, 16776,
16777, 16780, 16781, 16782, 16784, 16785, 16787, 16788, 16789,
16790, 16791, 16792, 16796, 16797, 16798, 16799, 16800, 16801,
16802, 16803, 16804, 16805, 16806, 16807, 16808, 16809, 16810,
16811, 16812, 16813, 16814, 16815, 16816, 16817, 16818, 16819,
16820, 16821, 16822, 16823, 16824, 16825, 16826, 16827, 16828,
16829, 16830, 16831, 16832, 16833, 16834, 16835, 16836, 16837
), class = "Date"), b = c(5613L, 1374L, 6179L, 2913L, 6628L,
3265L, 1763L, 10678L, 7308L, 8686L, 11805L, 4988L, 6584L, 11847L,
271L, 8125L, 11227L, 6969L, 8407L, 5083L, 8188L, 10500L, 4592L,
8691L, 1121L, 1150L, 3154L, 11724L, 6059L, 2573L, 10244L, 1008L,
10938L, 7356L, 1931L, 5182L, 1541L, 10449L, 9948L, 2418L, 10384L,
11416L, 8994L, 10652L, 6231L, 3777L, 6079L, 10041L, 10922L, 7410L,
1695L, 10890L, 390L, 5635L, 6882L, 6892L, 2807L, 4472L, 5696L
)), .Names = c("a", "b"), row.names = c(NA, -59L), class = "data.frame")
Upvotes: 4
Views: 3245
Reputation: 499
Seems that the command changed *see (ggplot2 scale x date?): it appears the argument has been changed to date_breaks
Upvotes: 0
Reputation: 1688
Although you can specify breaks
in scale_x_date()
, in order to specify daily breaks in the format using, the correct argument is date_breaks
. You can see an example of its use in the help for scale_x_date()
by running ?scale_x_date
.
Thus your code should read:
ggplot(aes(x=a, y= b), data = d.data) +
geom_col(position = 'dodge') +
scale_x_date(date_breaks = '1 day') +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
Upvotes: 0
Reputation: 4283
Using your data, you first need to make sure that your date column is from the class POSIXct
, then you may use the ggplot code below:
d.data$a <- as.POSIXct(d.data$a)
ggplot(aes(x = a, y = b), data = d.data) +
geom_bar(stat = 'identity', position = 'dodge') +
scale_x_datetime(date_breaks = "1 day") + ### changed
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
This yields the following plot:
Upvotes: 7