这是一个选项,使用从tidyr 到首先spread 的数据来计算对比度,然后将gather 重新组合在一起以允许绘图:
forPlotting <-
df %>%
spread(scenario, value) %>%
mutate(`a - b` = a - b
, `b - a` = b - a
, `a - a` = 0
, `b - b` = 0) %>%
gather(Comparison, Difference, -(year:b) ) %>%
separate(Comparison, c("First Val", "Second Val"), " - ")
这样会返回一个 data.frame(只是这里的头部):
year a b First Val Second Val Difference
1 2011 0 1 a b -1
2 2012 1 2 a b -1
3 2013 2 3 a b -1
4 2014 3 4 a b -1
5 2015 4 5 a b -1
6 2011 0 1 b a 1
你可以像这样绘制:
ggplot(forPlotting
, aes(x = year, y = Difference)) +
geom_point() + geom_line() +
facet_grid(`First Val` ~ `Second Val`)
更大的问题是为什么你想这样做。我假设您已经知道将这两个集合绘制为不同的颜色线更容易可视化:
ggplot(df, aes(x=year, y=value, col = scenario)) +
geom_point() + geom_line()
所以,我假设您有更复杂的数据 - 具体来说,有更多列要比较。因此,这里有一种方法可以自动化(并简化)多个列的上述许多步骤。方法基本相同,但它使用mutate_ 允许您传入包含您尝试创建的列的向量。
df <-
data.frame(
year = 2011:2015
, a = 0:4
, b = 1:5
, c = 2:6
, d = 3:7
)
allContrasts <-
outer(colnames(df)[-1]
, colnames(df)[-1]
, paste
, sep = " - ") %>%
as.character() %>%
setNames(., .) %>%
as.list()
forPlotting <-
df %>%
mutate_(.dots = allContrasts) %>%
select(-(a:d)) %>%
gather(Comparison, Difference, -year ) %>%
separate(Comparison, c("First Val", "Second Val"), " - ") %>%
filter(`First Val` != `Second Val`)
ggplot(forPlotting
, aes(x = year, y = Difference)) +
geom_point() + geom_line() +
facet_grid(`First Val` ~ `Second Val`) +
theme(axis.text.x = element_text(angle = 90))
给出这个:
为什么我不能不理会这个?我只是太喜欢玩标准评估了。如果您有非解析列名(例如,带有空格的东西),上述将失败。因此,这里有一个这样的列名示例,显示了添加反引号以确保正确解析列。
df <-
data.frame(
year = 2011:2015
, value = c(0:4, 1:5, 2:6, 3:7)
, scenario = rep(c("Unit 1", "Exam 2"
, "Homework", "Final Exam")
, each = 5)
) %>%
spread(scenario, value)
allContrasts <-
outer(paste0("`", colnames(df)[-1], "`")
, paste0("`", colnames(df)[-1], "`")
, paste
, sep = " - ") %>%
as.character() %>%
setNames(., .) %>%
as.list()
forPlotting <-
df %>%
mutate_(.dots = allContrasts) %>%
select_(.dots = paste0("-`", colnames(df)[-1], "`")) %>%
gather(Comparison, Difference, -year ) %>%
separate(Comparison, c("First Val", "Second Val"), " - ") %>%
filter(`First Val` != `Second Val`) %>%
mutate_each(funs(gsub("`", "", .)), `First Val`, `Second Val`)
ggplot(forPlotting
, aes(x = year, y = Difference)) +
geom_point() + geom_line() +
facet_grid(`First Val` ~ `Second Val`) +
theme(axis.text.x = element_text(angle = 90))