独立重新排序每个轴元素,ggplot2 [R]
Reorder each axis element independently, ggplot2 [R]
我正在尝试创建一个类似于 Sankey 图但不是频率而是每个案例的图:我想分别从 x 轴重新排序每个元素并连接同一案例中的每个点。
这就是我正在做的事情...
> data <- source("https://pastebin.com/raw/rPaaMvpb")$value
> ggplot(data, aes(variable, reorder(SubjID, value), color = factor(value), group = SubjID)) +
geom_point() + geom_line()
...但是我想要这样的东西:
有什么帮助吗?非常感谢。
我相信这段代码生成的图形与您期望的图形很接近。
library(tidyverse)
source("https://pastebin.com/raw/rPaaMvpb")$value %>% nest(data=-variable) %>% mutate(data=map(data, function(data){
data %>% arrange(value) %>% mutate(id=row_number()) %>% return()
})) %>% unnest(cols=data) %>%
mutate(value=if_else(value %in% c(1, 4), "1, 4", if_else(value %in% c(2, 5), "2, 5", "3, 6"))) %>%
ggplot(aes(x=variable, y=id, group=SubjID, colour=value, label=SubjID)) +
geom_point() + geom_line(colour="black") + geom_text(aes(hjust=if_else(variable=="g1", 1.5, -0.5)), colour="black") +
labs(y=NULL) + theme(axis.text.y=element_blank(), axis.line.y=element_blank(), axis.ticks.y=element_blank())
由 reprex package (v1.0.0)
于 2021 年 3 月 10 日创建
我正在尝试创建一个类似于 Sankey 图但不是频率而是每个案例的图:我想分别从 x 轴重新排序每个元素并连接同一案例中的每个点。
这就是我正在做的事情...
> data <- source("https://pastebin.com/raw/rPaaMvpb")$value
> ggplot(data, aes(variable, reorder(SubjID, value), color = factor(value), group = SubjID)) +
geom_point() + geom_line()
...但是我想要这样的东西:
有什么帮助吗?非常感谢。
我相信这段代码生成的图形与您期望的图形很接近。
library(tidyverse)
source("https://pastebin.com/raw/rPaaMvpb")$value %>% nest(data=-variable) %>% mutate(data=map(data, function(data){
data %>% arrange(value) %>% mutate(id=row_number()) %>% return()
})) %>% unnest(cols=data) %>%
mutate(value=if_else(value %in% c(1, 4), "1, 4", if_else(value %in% c(2, 5), "2, 5", "3, 6"))) %>%
ggplot(aes(x=variable, y=id, group=SubjID, colour=value, label=SubjID)) +
geom_point() + geom_line(colour="black") + geom_text(aes(hjust=if_else(variable=="g1", 1.5, -0.5)), colour="black") +
labs(y=NULL) + theme(axis.text.y=element_blank(), axis.line.y=element_blank(), axis.ticks.y=element_blank())
由 reprex package (v1.0.0)
于 2021 年 3 月 10 日创建