gpt4 book ai didi

r - 动态轴在绘图区域的顶部和底部滴答作响,循环时具有一致的间隔数和一致的宽度(ggplot2、grid.arrange)

转载 作者:行者123 更新时间:2023-12-05 06:08:58 25 4
gpt4 key购买 nike

我有一些数据

set.seed(123)

df <- data.frame(rbind(
a = rnorm(4, 60, 30),
b = rnorm(4, 30, 15),
c = rnorm(4, 10, 5)))

spe_a = rnorm(3, mean=0.05, sd=0.003)
season = factor(c('Jan','Feb','Mar'))

colnames(df) <- c("spe_b","spe_c","spe_d","spec_e")
df <- cbind(season, spe_a, df)
# Yes I am sure there is a better method! Please advise in comments?
# I was determine to recreate my problem

使用以下我可以生成网格图:

plot_list = list()

library(ggplot2)

for(i in colnames(df[, 2:5])){
#
plot <- ggplot(data = df,
aes_string(x = df$season, y = i)) +
geom_bar(stat = 'identity',
fill = 'lightblue') + # For style
ggtitle(i) +
theme(axis.title.y = element_blank()) +
scale_y_continuous(expand = c(0,0))
#
plot_list[[i]] = plot
#
}

library(gridExtra)

grid.arrange(grobs = plot_list, ncol = 2)

这给了我: enter image description here

太棒了!但是它有一些问题(也见图 2)

  1. 我希望刻度(和标签)始终位于绘图区域的顶部和底部。
  2. 我希望刻度具有一致的间隔数(至少是 4/5)
  3. 地 block 需要具有相同的地 block 面积。

enter image description here

我知道这里有几个问题,但我认为它们可能是相关的。

我确实环顾四周

最佳答案

使用 patchwork 尝试这种方法并使用 seq() 设置中断。这里的代码:

library(patchwork)
library(ggplot2)
#List
plot_list = list()
#Loop
for(i in colnames(df[, 2:5])){
#
plot <- ggplot(data = df,
aes_string(x = season, y = i)) +
geom_bar(stat = 'identity',
fill = 'lightblue') + # For style
ggtitle(i) +
theme(axis.title.y = element_blank()) +
scale_y_continuous(expand = c(0,0),
limits = c(0, max(df[,i])),
breaks = seq(0,max(df[,i]),length.out = 5),
labels = function(x) round(x,2))
#
plot_list[[i]] = plot
#
}
#Plot
G <- wrap_plots(plot_list,ncol = 2)

输出:

enter image description here

关于r - 动态轴在绘图区域的顶部和底部滴答作响,循环时具有一致的间隔数和一致的宽度(ggplot2、grid.arrange),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64991743/

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