首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ggplot连接分组点图内的所有点

使用ggplot连接分组点图内的所有点
EN

Stack Overflow用户
提问于 2018-08-02 15:45:18
回答 1查看 101关注 0票数 1

我有这样的数据帧

代码语言:javascript
运行
复制
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。我可以创建成组的点图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-02 16:36:45

您可以尝试使用一个隐藏的facet_grid和一个并行线来分隔Cluster列。这也可以通过ave来完成,比如

代码语言:javascript
运行
复制
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")) 

另一种方法是使用像这样的交互术语:

代码语言:javascript
运行
复制
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)}

必须使用花哨的{}来打断管道和标签值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51648204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档