我在相同序列中组织的2个不同数据集中有2个变量
DF1:
LoopGW NPV Model
1 200 1
2 300 1
DF2:
LoopGW NPVadjusted Model
1 300 3
2 400 3
我试过这个:
boxplot(NPV ~ loopGW, data = df1)
boxplot(NPVadjusted ~ loopGW, data= df2, add = TRUE)
但我得到的是重叠的箱线图。
我希望所有四个箱形图分开并按模型着色。有人可以帮忙吗?非常感谢
你还没有真正提供一个可重复的例子,所以我只是用我所拥有的。希望它做你想要的。可能有更好的方法到达那里,但这就是我做的:
library(tidyr)
library(ggplot2)
#read the data
df1 <- read.table(text = "
LoopGW NPV Model
1 200 1
2 300 1", stringsAsFactors = FALSE, header = TRUE)
df2 <- read.table(text = "
LoopGW NPVadjusted Model
1 300 3
2 400 3", stringsAsFactors = FALSE, header = TRUE)
#preparing the data.frames for binding so no information gets lost.
d1g <- gather(df1, key = "NPV_flag", value = "NPV", -Model, -LoopGW)
d2g <- gather(df2, key = "NPV_flag", value = "NPVadjusted", -Model, -LoopGW)
#binding the two data.frames
d12g <- rbind(d1g, setNames(d2g, names(d1g)))
#create the groups after which to seperate
d12g$Model_Loop <- paste(d12g$Model, "_", d12g$LoopGW, sep = "")
#Model as factor
d12g$Model <- as.factor(d12g$Model)
#Plot with ggplot
ggplot(d12g, aes(x = Model, y = NPV, group = Model_Loop, color = Model)) + geom_boxplot()
这就是结果。你必须想象那里有4个漂亮的箱形图。 ^^
我希望那就是你想要的。 4个框图以Loop和Model分隔?并且由模型着色。