Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R geom_线段图的排序问题_R_Ggplot2 - Fatal编程技术网

R geom_线段图的排序问题

R geom_线段图的排序问题,r,ggplot2,R,Ggplot2,我将感谢任何建议,我的阴谋-我是一个ggplot新手 我正试图创建一个克里夫兰圆点图,它有三个层次。我有3个问题需要解决: 在每个簇中,我希望点按我的连续x-var排序。下面的代码排序不正确 是否可以根据y-var是以0结尾(没有特征)还是以1结尾(有特征)来更改点类型 我的数据集中有一个变量(总体),显示特征的总体百分比。我想看看与总体相比,集群特征是否过度/不足。我想在每个y-var的同一行上添加一个点 这是我的密码: ggplot(cl1, aes(x=Cluster_prop, y=re

我将感谢任何建议,我的阴谋-我是一个ggplot新手

我正试图创建一个克里夫兰圆点图,它有三个层次。我有3个问题需要解决:

  • 在每个簇中,我希望点按我的连续x-var排序。下面的代码排序不正确

  • 是否可以根据y-var是以0结尾(没有特征)还是以1结尾(有特征)来更改点类型

  • 我的数据集中有一个变量(总体),显示特征的总体百分比。我想看看与总体相比,集群特征是否过度/不足。我想在每个y-var的同一行上添加一个点

  • 这是我的密码:

    ggplot(cl1, aes(x=Cluster_prop, y=reorder(Var, Cluster_prop)))+
      geom_segment(aes(yend=Var), xend=0, colour="grey50")+
      geom_point(size=3, aes(colour=Cluster))+
      facet_grid(Cluster~., scales="free_y", space="free_y") +
      ggtitle("Top 10 Cluster Characteristics: % Children Within Cluster With 
    Feature") 
    
    以下是我的数据:

    > dput(cl1)
    structure(list(Var = structure(c(2L, 3L, 5L, 7L, 14L, 16L, 18L, 
    19L, 20L, 22L, 15L, 9L, 7L, 6L, 21L, 13L, 17L, 12L, 4L, 11L, 
    15L, 17L, 21L, 1L, 13L, 4L, 10L, 12L, 6L, 8L), .Label = c("asthdoc_1", 
    "AttacksOnExer_1_0", "AttacksTTT_1_0", "AttacksTTT_1_1", "Breath0rmal_1_0", 
    "Breath0rmal_1_1", "CAsthmaMed_1_0", "CAsthmaMed_1_1", "CCurrentAsthma_1_0", 
    
    "CCurrentAsthma_1_1", "CongColds_1_1", "CoughNight_1_1", 
    "CoughWithColds_1_1", 
    "EverWheeze_1_0", "EverWheeze_1_1", "Wheeze6M_1_0", "Wheeze6M_1_1", 
    "WheezeMostDays_1_0", "WheezeOcc_1_0", "WheezeWithColds_1_0", 
    "WheezeWithColds_1_1", "WheezeWithShort_1_0"), class = "factor"), 
        Cluster_prop = c(100, 100, 100, 100, 100, 100, 100, 100, 
        100, 100, 100, 99.4219653, 98.8439306, 95.3757225, 94.7976879, 
        83.2369942, 79.1907514, 53.7572254, 50.867052, 50.867052, 
        100, 100, 100, 93.103448, 89.655172, 86.206897, 86.206897, 
        82.758621, 79.310345, 79.310345), Population = c(96.131528, 
        78.143133, 63.636364, 95.16441, 60.928433, 67.891683, 97.485493, 
        89.555126, 62.669246, 90.32882, 39.071567, 94.584139, 95.16441, 
        36.363636, 37.330754, 68.665377, 32.108317, 43.520309, 21.856867, 
        42.166344, 39.071567, 32.108317, 37.330754, 9.864603, 68.665377, 
        21.856867, 5.415861, 43.520309, 36.363636, 4.83559), Cluster = 
    structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1", 
    "2", "3"), class = "factor")), .Names = c("Var", "Cluster_prop", 
    "Population", "Cluster"), row.names = c(NA, -30L), vars = "Cluster", drop = 
    TRUE, indices = list(
    0:9, 10:19, 20:29), group_sizes = c(10L, 10L, 10L), biggest_group_size = 
    10L, labels = structure(list(
    Cluster = 1:3), row.names = c(NA, -3L), class = "data.frame", vars = 
    "Cluster", drop = TRUE, .Names = "Cluster"), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"))
    
    非常感谢你的建议

    关于您的第二期(编辑和第三期):

    库(tidyverse)
    图书馆(stringr)
    str_sub(str,start=-1,end=-1)
    cl2%突变(Shape=str_sub(Var,start=-1,end=-1))
    ggplot(cl2,aes(x=簇属性,y=重新排序(Var,簇属性)))+
    geom_段(aes(yend=Var),xend=0,color=“灰色50”)+
    几何点(尺寸=3,aes(颜色=簇,形状=形状))+
    几何点(aes(x=总体),大小=2,颜色=“黑色”)+
    刻面网格(集群),scales=“free\u y”,space=“free\u y”)+
    ggtitle(“前10个群集特征:%s群集中具有
    特征“)
    

    如果这解决了你的问题,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这么做。我不知道为什么我不能按照我的第一点来整理我的数据,但是,你完美地解决了其他问题。再次感谢你!
    library(tidyverse)
    library(stringr)
    str_sub(str, start = -1, end = -1)
    
    cl2 <- cl1 %>% mutate(Shape = str_sub(Var, start = -1, end = -1))
    
    
    ggplot(cl2, aes(x=Cluster_prop, y=reorder(Var, Cluster_prop)))+
      geom_segment(aes(yend=Var), xend=0, colour="grey50")+
      geom_point(size=3, aes(colour=Cluster, shape = Shape))+
      geom_point(aes(x = Population), size = 2, color = "black")+
      facet_grid(Cluster~., scales="free_y", space="free_y") +
      ggtitle("Top 10 Cluster Characteristics: % Children Within Cluster With 
              Feature")