gpt4 book ai didi

r - 如何在ggplot中使用interaction()创建两个子组的图(几个图的组合)

转载 作者:行者123 更新时间:2023-12-03 23:05:48 24 4
gpt4 key购买 nike

我正在尝试按两个子组创建图,如下图所示( ggplot2 multiple sub groups of a bar chart )。但是,我想为情节的组合这样做。
enter image description here
当我尝试这样做时,我的变量“growth”和“year”被折叠在一起,而不是将类别清楚地分开(就像在 Irrigated/Dry 和 Variety1/Variety 2 的示例中一样)。
我在将这个微小的修改合并到我的代码中时遇到了麻烦。我希望我的可变年份就像“灌溉/干燥”一样,而可变增长为“Variety1/Variety2”现在,这就是情节的样子:
enter image description here
这是我的代码:

library(ggplot2)

#Creates the data

d <- expand.grid(c(.3, .8), c(.3, 0.8), c(0, 0.5), c(1, 2), c("Oregon"," California"), c("2010","2011"))
colnames(d) <- c("gamma1", "gamma2", "growth", "store", "state", "year")
d$sells <- rnorm(64)

d$gamma_plot <- as.factor(paste(d$gamma1, d$gamma2, sep = "_"))
d$store <- as.factor(d$store)

d$growth <- factor(d$growth)
d$gamma_plot = factor(d$gamma_plot,
labels=c(expression(paste(gamma[1],"=", gamma[2]," = 0.3")),
expression(paste(gamma[1], " = 0.3 ", gamma[2], " = 0.8")),
expression(paste(gamma[1], " = 0.8 ", gamma[2], " = 0.3")),
expression(paste(gamma[1],"=", gamma[2]," = 0.8"))
)
)

d$store = factor(d$store,
labels = c(expression(paste(store[1], " = 1")),
expression(paste(store[2], " = 2"))
)
)

#Creates the plot
p = ggplot(data=d, aes(x=interaction(year, growth), y=sells, fill=state)) +
geom_col(position="dodge") +
theme_bw() +
facet_grid(store ~ gamma_plot, labeller = label_parsed) +
theme(legend.title = element_blank(), legend.position="bottom",
panel.grid.major = element_blank(),
legend.key.size = unit(0.10, "cm"),
legend.key.width = unit(0.15,"cm")) +
guides(fill = guide_legend(nrow = 1)) +
labs(x=expression(growth), y = "Sells")

编辑:
针对我的问题给出的两个解决方案很棒,我非常感谢。
我决定稍微改变一下情节,并在 gamma_plot 和增长之间进行交互。我无法让 R 理解 gamma_plot 是一个表达式。有任何想法吗?
enter image description here
#Creates the plot using teunbrand's code :)

ggplot(data=d, aes(x=interaction(growth, gamma_plot, sep = "&"), y=sells, fill=year)) +
geom_col(position="dodge") +
theme_bw() +
facet_grid(store ~ state, labeller = label_parsed) +
theme(legend.title = element_blank(), legend.position="bottom",
panel.grid.major = element_blank(),
legend.key.size = unit(0.10, "cm"),
legend.key.width = unit(0.15,"cm"),
axis.text.x = element_text(margin = margin(2,2,2,2))) +
scale_x_discrete(guide = guide_axis_nested(delim = "&")) +
guides(fill = guide_legend(nrow = 1)) +
labs(x=expression(growth), y = "Sells")

最佳答案

这个选项怎么样:



library(ggplot2)


ggplot(data=d, aes(x = interaction(year, growth), y=sells, fill = state)) +
geom_col(position="dodge") +
scale_x_discrete(labels = unique(interaction(d$year, factor(d$growth), sep = "\n")))+
theme_bw() +
facet_grid(store ~ gamma_plot, labeller = label_parsed) +
theme(legend.title = element_blank(), legend.position="bottom",
panel.grid.major = element_blank(),
legend.key.size = unit(0.10, "cm"),
legend.key.width = unit(0.15,"cm")) +
guides(fill = guide_legend(nrow = 1)) +
labs(x = expression(Year~growth), y = "Sells")

创建于 2020-07-10 由 reprex package (v0.3.0)

关于r - 如何在ggplot中使用interaction()创建两个子组的图(几个图的组合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62840968/

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