gpt4 book ai didi

r - 如何将可扩展时间数据作为 r ggplot 包中的一个因素呈现? (即 1 小时、5 小时、10 小时)

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

我有 0 小时、3 小时、12 小时、24 小时、48 小时的数据组……我想绘制这些数据的图表,以便保留时间的比例。

runs <- c(1:25)
hours <- as.factor(c(0, 3, 12, 24, 48, 96))
treatments <- (c("a","b","c","d"))
things <- as.numeric(runif(600, min=1, max=15))
type <- expand.grid(hours,treatments,runs)
data.df <- data.frame(type,things)

names(data.df)[names(data.df)=="Var1"] <- "hour"
names(data.df)[names(data.df)=="Var2"] <- "treatments"

library(ggplot2)

ggplot(data.df, aes(x=as.factor(hour), y=things, fill=treatments)) +
geom_boxplot()
enter image description here
我需要缩放图表中的数据,因此 3 小时刻度大约是 12 小时刻度距离的 1/4,而 12 小时是 24 小时刻度距离的一半。
使用 Stefan 的回答,我们得出了这个图:
enter image description here
使用此代码更改
ggplot(data.df, aes(x = as.numeric(as.character(hours)), y=things, fill=things, group = hours))+
geom_boxplot() +
scale_x_continuous(breaks = as.numeric(as.character(hours)))
但不幸的是,我无法更改组/填充以显示治疗。
谢谢!

最佳答案

为了达到您想要的结果,您可以转换您的 hours变量到数字和映射 hoursgroup审美的:

hours <- as.factor(c(0, 3, 12, 24, 48, 96))
things <- runif(36, min=1, max=10)
data.df <- data.frame(hours,things)

library(ggplot2)

ggplot(data.df, aes(x = as.numeric(as.character(hours)), y=things, fill=things, group = hours))+
geom_boxplot() +
scale_x_continuous(breaks = as.numeric(as.character(hours)))

编辑 对于您更新的问题,您可以通过映射 hour 来获得所需的结果。和 treatmentgroup aes 使用例如 interaction :

library(ggplot2)

ggplot(data.df, aes(x = as.numeric(as.character(hour)), y=things, fill=treatments, group = interaction(hour, treatments)))+
geom_boxplot() +
scale_x_continuous(breaks = as.numeric(as.character(hours)))

关于r - 如何将可扩展时间数据作为 r ggplot 包中的一个因素呈现? (即 1 小时、5 小时、10 小时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65865058/

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