gpt4 book ai didi

r - 如何在 R 中为 2x2x2 设计创建 ggplot 2 意大利面条图?

转载 作者:行者123 更新时间:2023-12-01 23:39:36 26 4
gpt4 key购买 nike

我有这样的数据

climate <- c("Dry","Wet","Dry","Wet","Dry","Wet","Dry","Wet")
place <- c("Urban", "Urban","Urban","Urban","Rural","Rural","Rural","Rural")
control <- c(4,5,1,0,0,5,6,7)
treatment <- c(1,2,3,0,1,9,9,1)
dat01 <- data.frame(climate, place, control, treatment)

我想在一帧中创建 2 个不同的意大利面条图,以便按地点分类,但两个气候区域都出现在同一个图中(不同颜色)

这是我的尝试

library(ggvis)
library(gridExtra)
library(reshape)
library(ggplot2)

dat01 <- melt(dat01, id = c("climate","place"))

mytheme <- theme_classic() %+replace%
theme(axis.title.x = element_blank(),
axis.title.y = element_text(face = "bold", angle = 90))

ggplot(data = dat01, aes(x = variable, y = value, group = place, colour = climate)) +
mytheme +
labs(list(x = paste("Plots"), y = paste("Number of Seedlings"))) +
geom_line(size = 1)

这会产生一个输出,但这些图似乎并没有明显地将控制加入到相应的处理中。一些对照和治疗似乎相互关联。

此外,我无法按要求生成两个不同的图。

此外,如果有一种简单的方法可以分别为每个图添加标题,那就太好了。

最佳答案

我想这就是你想要的:

library(reshape)
library(ggplot2)

climate <- c("Dry","Wet","Dry","Wet","Dry","Wet","Dry","Wet")
place <- c("Urban", "Urban","Urban","Urban","Rural","Rural","Rural","Rural")
control <- c(4,5,1,0,0,5,6,7)
treatment <- c(1,2,3,0,1,9,9,1)
pair_id <- 1:8
dat01 <- data.frame(climate, place, control, treatment, pair_id)

dat01 <- melt(dat01, id = c("climate","place", "pair_id"))


ggplot(data = dat01, aes(x = variable, y = value, group = pair_id, colour = climate)) +
facet_wrap(~place)+
labs(list(x = paste("Plots"), y = paste("Number of Seedlings"))) +
geom_line(size = 1) + theme_bw()

enter image description here

关于r - 如何在 R 中为 2x2x2 设计创建 ggplot 2 意大利面条图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46395593/

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