“for”循环只添加ggplot中的一个层

时间:2016-12-04 18:06:47

标签: r ggplot2

我认识到这是many other instances中提出的一个问题,但所提供的解决方案都没有针对我的特定问题。

在这里,我有以下数据:

library(tidyverse)
library(scales)
mydata <- tibble(Category = c("A", "B", "C", "D"),
                 Result = c(0.442, 0.537, 0.426, 0.387),
                 A = c(NA, "A", NA, NA),
                 B = rep(NA, 4),
                 C = c(NA, "C", NA, NA),
                 D = c("D", "D", NA, NA))
mydata$Category <- factor(mydata$Category)

我有以下颜色的矢量:

colors_vct <- c(A = "#0079c0", B = "#cc9900", C = "#252525", D = "#c5120e")

有了这些信息,我可以创建以下图:

p <- ggplot(data = mydata , aes(x = Category, y = Result, fill = Category)) + 
geom_bar(stat = "identity") + geom_text(aes(label = percent(Result), color = Category), hjust = -.25) +
coord_flip() + scale_y_continuous(limits = c(0,1), labels = percent) + 
scale_colour_manual(values = colors_vct) + scale_fill_manual(values = colors_vct)
p

enter image description here

我想在标签后面显示小三角形,这取决于mydata的最后4列中是否提到某个类别,并按照该类别的颜色进行着色,因此:

p <- p + geom_text(data = filter(mydata, mydata[,3] == "A"), aes(label = sprintf("\u25b2")), colour = colors_vct["A"], hjust = -4)
#p <- p + geom_text(data = filter(mydata, mydata[,4] == "B"), aes(label = sprintf("\u25b2")), colour = colors_vct["B"], hjust = -5) #This is commented out because there are no instances where the layer ends up being applied.
p <- p + geom_text(data = filter(mydata, mydata[,5] == "C"), aes(label = sprintf("\u25b2")), colour = colors_vct["C"], hjust = -6)
p <- p + geom_text(data = filter(mydata, mydata[,6] == "D"), aes(label = sprintf("\u25b2")), colour = colors_vct["D"], hjust = -7)
p

enter image description here

这就是我想要最终图表的样子(或多或少,请参阅下面的红利问题)。现在,我想使用for循环迭代最后一段代码。这就是我遇到麻烦的地方。它最终只会添加一层。我该如何工作?这是我的尝试:

#Set the colors into another table for matching:
colors_tbl <- tibble(Category = levels(mydata$Category),
                                colors = c("#0079c0", "#cc9900", "#252525", "#c5120e"))

for (i in seq_along(mydata$Category)) {
if (is_character(mydata[[i]])) { #This makes the loop skip if there is nothing to be applied, as with category B.
#Filters to just the specific categories I need to have the triangles shown.
    triangles <- filter(mydata, mydata[,(i+2)] == levels(mydata$Category)[i])
#Matches up with the colors_tbl to determine which color to use for that triangle.
    triangles <- mutate(triangles, colors = colors_tbl$colors[match(levels(triangles$Category)[i], colors_tbl$Category)])
#Sets a particular position for that triangle for the hjust argument below.
    pos <- -(i+3)
#Adding the layer to the plot object
 p <- p + geom_text(data = triangles, aes(label = sprintf("\u25b2")), color = triangles$colors, hjust = pos)
  }
}
p

enter image description here

:(

奖金问题:根据第二张图表,有没有办法可以避免三角形之间的间隙?

编辑:根据@baptiste的建议,我重新处理了数据:

mydata2 <- mydata %>% gather(key = comp, value = Present, -Result, -Category) 
mydata2 <- mydata2 %>% mutate(colors = colors_tbl$colors[match(mydata2$Present, colors_tbl$Category)]) %>%
filter(!is.na(mydata2$Present)) %>% select(-comp)
mydata2 <- mydata2 %>% mutate(pos = if_else(Present == "A", -4, if_else(Present == "B", -5, if_else(Present == "C", -6, -7))))
p <- p + geom_text(data = mydata2, aes(x = Category, label = sprintf("\u25b2")), colour = mydata2$colors, hjust = mydata2$pos)
p

好的,我得到了它的工作。我的奖金问题仍然存在。

0 个答案:

没有答案