gpt4 book ai didi

r - 我如何让 ggplot 每天打破 x 轴?

转载 作者:行者123 更新时间:2023-12-04 03:04:25 25 4
gpt4 key购买 nike

我想得到一个显示每天一定数量的条形图。但是,ggplot 没有单独显示天数,而是每月汇总。我如何让 ggplot 每天打破 x 轴?

这是我正在使用的代码:

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))

运行上面的代码后出现以下错误: Error in strsplit(unitspec, " ") : non-character argument
以下数据用作输入:
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")

最佳答案

使用您的数据,您首先需要确保您的日期列来自类 POSIXct ,那么你可以使用下面的 ggplot 代码:

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))

这产生了以下情节:

enter image description here

关于r - 我如何让 ggplot 每天打破 x 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45881778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com