gpt4 book ai didi

r - ggplot2 每周箱线图

转载 作者:行者123 更新时间:2023-12-04 10:09:16 27 4
gpt4 key购买 nike

此代码生成一个箱线图:

df <- data.frame(value = rnorm(62), my.date = seq(as.Date("2013-12-01"), as.Date("2014-01-31"), by="1 day"))

library(ggplot2)
ggplot(df, aes(as.Date(my.date), value)) + geom_boxplot() + scale_x_date(minor_breaks = "1 week", labels = date_format("%W\n%b"))

enter image description here

如何生成一个图,该图在 12 月 1 日至 1 月 31 日之间的每周都有单个箱线图?所以在单个图中,应该有大约 8 个箱线图。更喜欢使用 ggplot()scale_x_date() 的解决方案。

最佳答案

一种选择是在使用 ggplot 之前转换您的日期

library(ggplot2)
df <- data.frame(value = rnorm(62),
my.date = seq(as.Date("2013-12-01"), as.Date("2014-01-31"), by="1 day"))
weeks <- format(df$my.date, "%Y/%W")
weeks <- factor(weeks, levels = unique(weeks))

ggplot(df, aes(weeks, value)) +
geom_boxplot()

关于r - ggplot2 每周箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329882/

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