gpt4 book ai didi

r - 如何使用ggplot创建多面线图?

转载 作者:行者123 更新时间:2023-12-03 11:41:32 24 4
gpt4 key购买 nike

我有一个使用此代码创建的数据框:

require(reshape2)
foo <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.8),rnorm(3, mean=.9),rnorm(3, mean=1))))
qux <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.3),rnorm(3, mean=.4),rnorm(1, mean=2))))
bar <- data.frame( abs( cbind(rnorm(3,mean=.4),rnorm(3, mean=.3),rnorm(3, mean=.9),rnorm(3, mean=1))))

colnames(foo) <- c("w","x","y","z")
colnames(qux) <- c("w","x","y","z")
colnames(bar) <- c("w","x","y","z")

rownames(foo) <- c("n","q","r")
rownames(qux) <- c("n","q","r")
rownames(bar) <- c("n","q","r")

foo <- cbind(ID=rownames(foo),foo)
bar <- cbind(ID=rownames(bar),qux)
qux <- cbind(ID=rownames(bar),qux)

foo$fn <- "foo"
qux$fn <- "qux"
bar$fn <- "bar"

alldf<-rbind(foo,qux,bar)
alldf.m <- melt(alldf)

我想做的是创建一个 ggplot分面格式的线曲线,因此它创建了这样的图形:

enter image description here

实际图形不包含向上的线条 - 这只是一个草图,以便线条分隔清晰。

我当前的代码不起作用:
    library(ggplot2)
p <- ggplot(data=alldf.m, aes(x=variable)) +
geom_line(aes(colour=ID),alpha=0.4)
p <- p + facet_wrap( ~ fn)
p

最好的方法是什么?

最佳答案

尝试这个:

ggplot(data=alldf.m, aes(x=variable, y = value, colour = ID, group = ID)) + 
geom_line() + facet_wrap(~fn)

enter image description here

关于r - 如何使用ggplot创建多面线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14640872/

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