【问题标题】:Removing duplicate labeling and adjust labeling colours with dotchart删除重复标签并使用点图调整标签颜色
【发布时间】:2020-01-05 19:00:58
【问题描述】:

我想知道是否可以删除重复标签(通过将重复标签折叠到集中位置)并在不更改标签颜色的情况下调整点着色。

我有 3 层组:类别,分为结果,对于每个结果,我有组“1”和“2”的结果。

为了可视化我的问题,下面的代码:

categories <- rep(c("X", rep("Z", 3), rep("Y", 2), rep("W", 2)), 2)
outcomes <- rep(c("A", "B", "C", "D", 
                  "E", "F", "G", "H"), 2)
treatment <- c(rep("1", 8), rep("2", 8))
coefficients_nap <- rep(0.1, 8)
coefficients_ns <- rep(0.1, 8)

coefficients <- c(coefficients_nap, coefficients_ns)


data = data.frame(categories, outcomes, treatment, coefficients)

data <- data[order(categories, outcomes),]

dotchart(data$coefficients, labels = data$outcomes, groups = data$categories, main = "Overview Table", cex=.7, pch=17, gcolor = "black", color = rep(c("darkgreen", "purple")),
         xlim=c(-0.2, 0.2))

生成以下图表,其中标签重复,我还没有找到一种方法来分离点​​和标签的颜色:

【问题讨论】:

    标签: r plot


    【解决方案1】:

    广告 1.:删除重复标签

    将那些不显示的结果标签替换为NA

    解决方案 1: duplicated() 查找所有重复的标签,然后将其替换为 NA(即不绘制任何内容)。假设 重复项总是在运行中,如您的示例所示(即,在此示例中它还会删除重复的 A、B 和 C:A、B、A、C、B、C)。

    解决方案 2 + 3:也适用于非连续重复项,并且重复值仅在运行时才会从图中删除(连续重复项)。

    # Solution 1:  easy solution assuming duplicates are always in a row:
    # duplicated( , fromLast=T) will find all duplicated elements, starting from last element
    data$outcomes_1 <- data$outcomes
    data$outcomes_1[duplicated(data$outcomes_1, fromLast = T)] <- NA
    dotchart(data$coefficients, labels = data$outcomes_1, groups = data$categories, main = "Overview Table", cex=.7, pch=17, gcolor = "black", color = rep(c("darkgreen", "purple")),
             xlim=c(-0.2, 0.2))
    duplicated(c(1,2,3,3,4,3,2,9)) # BUT: does not work with non-consecutive duplicates (as in this example line)
    
    
    # Solution 2: more generic
    rle(as.character(data$outcomes))$lengths # [1] 2 2 2 2 2 2 2 2   # data$outcomes is factor => convert to character
    # then loop through each list element (vector), replace all but (first or) last vector element by NA (i.e. not displayed in diagram)
    
    
    # Solution 3: works with non-consecutive duplicates, and shorter
    # splits outcomes by occurrences of same character, delete each last character from sub-vector resembling consecutive occurrences from last element.
    data$outcomes_3 <- data$outcomes
    # data$outcomes_3 <- c(1,2,3,2,2,2,4,1,2,3,6,6,6,6,6,4) # data$outcomes # example to show non-consecutive runs
    (s <- split(data$outcomes, cumsum(c(1, diff(as.numeric(data$outcomes)) == 0))))
    if (length(s) > 1) for (i in 1:(length(s)-1))  s[[i]][length(s[[i]])] <- NA; data$outcomes_3 <- unlist(s)
    dotchart(data$coefficients, labels = data$outcomes_3, groups = data$categories, main = "Overview Table", cex=.7, pch=17, gcolor = "black", color = rep(c("darkgreen", "purple")),
             xlim=c(-0.2, 0.2))
    

    广告 2:颜色:

    不知道你为什么需要这个。除非您调整 dotchart() 函数,否则 AFAIK 不可能在结果和符号之间随机使用不同的颜色:

    • 输入dotchart(不带括号)
    • 将显示的代码复制粘贴到 R 编辑器并分配给新的 函数名
    • 随意更改颜色名称

    【讨论】:

      猜你喜欢
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      相关资源
      最近更新 更多