广告 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 编辑器并分配给新的
函数名
- 随意更改颜色名称