r - 使用 ggplot 连接分组点图中的所有点
问题描述
我有这样的数据框
AA=
States Clusters ncomp
HR Cluster-1 9
HR Cluster-2 4
HR Cluster-3 9
WB Cluster-1 13
WB Cluster-2 13
WB Cluster-3 13
WB Cluster-4 13
TL Cluster-1 9
TL Cluster-2 11
TL Cluster-3 9
TL Cluster-4 10
TL Cluster-5 11
TL Cluster-6 7
OR Cluster-1 15
OR Cluster-2 15
OR Cluster-3 15
OR Cluster-4 14
OR Cluster-5 15
OR Cluster-6 15
需要绘制看起来像。我不想使用 facet_wrap。我可以创建分组点图
解决方案
您可以尝试使用一条隐藏facet_grid
线和一条 tidyverse 线来分隔Cluster
列。这也可以通过ave
like来完成
d$C2 <- ave(as.numeric(d$Clusters), d$States, FUN = function(x) seq(1,length(x)))
library(tidyverse)
d %>%
separate(Clusters, into=c("C1", "C2"), sep="-") %>%
ggplot(aes(as.numeric(C2), ncomp, color=States)) +
geom_vline(data = . %>% count(States) %>%
mutate(x=n/2+0.5),
aes(xintercept=x),
color="white")+
geom_point() +
geom_line(aes(group=1)) +
facet_grid(~States, switch = "x",scales = "free_x", space = "free_x") +
scale_x_continuous(breaks = NULL, minor_breaks = NULL, expand = c(0.2,0.5))+
xlab("States") +
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
strip.background = element_blank(),
panel.spacing.x= unit(0, "mm"))
另一种方法是使用这样的交互术语:
d %>%
# calculate a numeric interaction between clusters and States
mutate(x=as.numeric(interaction(Clusters,States))) %>%
# calculate breaks
group_by(States) %>%
mutate(xbreaks=mean(x)) %>%
{ggplot(.,aes(x, ncomp, color=States)) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = unique(.$xbreaks), labels = unique(.$States),minor_breaks = NULL)}
{}
必须对管道中断和标签值进行幻想。
推荐阅读
- amazon-web-services - 将 EBS 卷附加到 EKS 集群节点
- linq - .NET Core 中 IQueryable 中的反射
- css - 更改离子日期时间值的颜色
- asp.net-core - 部署后 ASP.Net Core 3.1 应用程序丢失会话变量
- flutter - 将字体从飞镖传递到本机端
- python - 如何使用 Selenium 和 Python 提取内容
- reactjs - 使用 map() 函数后,如何使用语义 ui React 以行格式对齐卡片
- machine-learning - ML 或基于规则
- cmake - CMake 添加系统范围的前缀路径
- javascript - 安排 Javascript 函数在每天的特定时间调用