GGPlot geom_text着色与facets

时间:2013-01-17 06:31:38

标签: r ggplot2

希望有人在这里能够帮助我解决我正在尝试使用ggplot脚本的问题。该脚本将多次使用不同的数据,因此需要相对灵活。我几乎把它拿到了我想要的地方,但我遇到了一个我无法解决的问题。

该脚本用于带有右边距中每一行标签的折线图。有时图表是分面的,有时则不是。

我遇到的问题是,如果没有显着的变化,我想将右边缘的标签颜色编码为黑色,如果有正变化则为绿色,如果有负面变化则为红色。我有一个脚本可以在我只有一个方面时执行此操作,但只要我在图中有多个方面,标签的颜色编码就会出现以下错误

   Error: Incompatible lengths for set aesthetics:

以下是包含多个方面的数据的脚本。问题似乎在于我在geom_text行中指定颜色。如果我删除脚本中geom_text行中的颜色调用,那么我会在正确的位置打印属性,而不是彩色。我真的很茫然。这是我在这里的第一篇文章,所以如果我的帖子出错了,请告诉我。

有多个角色(不工作)

   require(ggplot2)
require(grid)
require(zoo)
require(reshape)
require(reshape2)
require(directlabels)

time.data<-structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
                                                  4L, 4L, 5L, 5L, 6L, 6L), .Label = c("Taste 1", "Taste 2", "Taste 3", 
                                                                                      "Use 1", "Use 2", "Use 3"), class = "factor"), Attribute.Category = structure(c(2L, 
                                                                                                                                                                      2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Nutritional/Usage", 
                                                                                                                                                                                                                              "Taste/Quality"), class = "factor"), Attribute.Order = c(1L, 
                                                                                                                                                                                                                                                                                       1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), Category.Order = c(1L, 
                                                                                                                                                                                                                                                                                                                                                       1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), Color = structure(c(1L, 
                                                                                                                                                                                                                                                                                                                                                                                                                        1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("#084594", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"), 
                          value = c(75L, 78L, 90L, 95L, 82L, 80L, 43L, 40L, 25L, 31L, 
                                    84L, 84L), Date2 = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                   1L, 2L, 1L, 2L, 1L), .Label = c("1/1/2013", "9/1/2012"), class = "factor")), .Names = c("Attribute", 
                                                                                                                                                           "Attribute.Category", "Attribute.Order", "Category.Order", "Color", 
                                                                                                                                                           "value", "Date2"), class = "data.frame", row.names = c(NA, -12L
                                                                                                                                                           ))

label.data<-structure(list(7:12, Attribute = structure(1:6, .Label = c("Taste 1", 
                                                                       "Taste 2", "Taste 3", "Use 1", "Use 2", "Use 3"), class = "factor"), 
                           Attribute.Category = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Nutritional/Usage", 
                                                                                                "Taste/Quality"), class = "factor"), Attribute.Order = 1:6, 
                           Category.Order = c(1L, 1L, 1L, 2L, 2L, 2L), Color = structure(1:6, .Label = c("#084594", 
                                                                                                         "#2171B5", "#4292C6", "#6A51A3", "#807DBA", "#9E9AC8"), class = "factor"), 
                           Significance = structure(c(2L, 3L, 1L, 1L, 3L, 2L), .Label = c("neg", 
                                                                                          "neu", "pos"), class = "factor"), variable = structure(c(1L, 
                                                                                                                                                   1L, 1L, 1L, 1L, 1L), .Label = "1/1/2013", class = "factor"), 
                           value = c(78L, 95L, 80L, 40L, 31L, 84L), Date2 = structure(c(1L, 
                                                                                        1L, 1L, 1L, 1L, 1L), .Label = "2013-01-01", class = "factor"), 
                           label.color = structure(c(1L, 2L, 3L, 3L, 2L, 1L), .Label = c("black", 
                                                                                         "forestgreen", "red"), class = "factor")), .Names = c("", 
                                                                                                                                               "Attribute", "Attribute.Category", "Attribute.Order", "Category.Order", 
                                                                                                                                               "Color", "Significance", "variable", "value", "Date2", "label.color"
                                                                                         ), class = "data.frame", row.names = c(NA, -6L))

color.palette<-as.character(unique(time.data$Color))

time.data$Date2<-as.Date(time.data$Date2,format="%m/%d/%Y")

plot<-ggplot()+
  geom_line(data=time.data,aes(as.numeric(time.data$Date2),time.data$value,group=time.data$Attribute,color=time.data$Color),size=1)+
  geom_text(data=label.data,aes(x=Inf, y=label.data$value, label=paste("  ",label.data$Attribute)),
            color=label.data$label.color,
            size=4,vjust=0, hjust=0,na.rm=T)+
  facet_grid(Attribute.Category~.,space="free")+
  theme_bw()+
  scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),labels=format(unique(time.data$Date2),format = "%b %Y"))+
  theme(strip.background=element_blank(),
        strip.text.y=element_blank(),
        legend.text=element_blank(),
        legend.title=element_blank(),
        plot.margin=unit(c(1,5,1,1),"cm"),
        legend.position="none")+
  scale_colour_manual(values=color.palette)

gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)

1 个答案:

答案 0 :(得分:4)

一些问题:

在审美声明中,您不应将数据列引用为time.data$Date2,而应将Date2引用。 data参数指定查找该信息的位置(对于给定图层,所有这些信息都需要位于相同的data.frame中,但是,正如您所利用的那样,可以在不同层之间进行更改)。

geom_text来电中,color不在aes来电中;如果要将其映射到data.frame中的数据,则必须将其放在aes调用中。这会在修复第一部分后引发不同的错误,因为它无法在任何地方找到label.color,因为它不知道在label.data内部查看。

修复那些,然后scale_colour_manual抱怨有9种颜色而你只提供了6.这是因为行中有6种颜色,文本中有3种颜色。由于您将这些指定为实际颜色名称,因此您只需使用scale_colour_identity

把这一切放在一起:

plot <- ggplot()+
  geom_line(data=time.data, aes(as.numeric(Date2), value, 
                                group=Attribute, color=Color), 
            size=1)+
  geom_text(data=label.data, aes(x=Inf, y=value, 
                                 label=paste("  ",Attribute),
                                 color=label.color),
            size=4,vjust=0, hjust=0)+
  facet_grid(Attribute.Category~.,space="free") +
  scale_x_continuous(breaks=as.numeric(unique(time.data$Date2)),
                     labels=format(unique(time.data$Date2),format = "%b %Y")) +
  scale_colour_identity() +
  theme_bw()+
  theme(strip.background=element_blank(),
        strip.text.y=element_blank(),
        legend.text=element_blank(),
        legend.title=element_blank(),
        plot.margin=unit(c(1,5,1,1),"cm"),
        legend.position="none")
gt3 <- ggplot_gtable(ggplot_build(plot))
gt3$layout$clip[gt3$layout$name == "panel"] <- "off"
grid.draw(gt3)

enter image description here

要了解您可以删除多少示例,这更接近于最小化:

time.data <- 
structure(list(Attribute = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L), .Label = c("Taste 1", "Taste 2", "Use 1", "Use 2"), class = "factor"), 
    Attribute.Category = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"), 
    Color = c("#084594", "#084594", "#2171B5", "#2171B5", "#6A51A3", 
    "#6A51A3", "#807DBA", "#807DBA"), value = c(75L, 78L, 90L, 
    95L, 43L, 40L, 25L, 31L), Date2 = structure(c(15584, 15706, 
    15584, 15706, 15584, 15706, 15584, 15706), class = "Date")), .Names = c("Attribute", 
"Attribute.Category", "Color", "value", "Date2"), row.names = c(NA, 
-8L), class = "data.frame")

label.data <- 
structure(list(value = c(78L, 95L, 40L, 31L), Attribute = structure(1:4, .Label = c("Taste 1", 
"Taste 2", "Use 1", "Use 2"), class = "factor"), label.color = c("black", 
"forestgreen", "red", "forestgreen"), Attribute.Category = structure(c(2L, 
2L, 1L, 1L), .Label = c("Nutritional/Usage", "Taste/Quality"), class = "factor"), 
    Date2 = structure(c(15706, 15706, 15706, 15706), class = "Date")), .Names = c("value", 
"Attribute", "label.color", "Attribute.Category", "Date2"), row.names = c(NA, 
-4L), class = "data.frame")

ggplot() +
  geom_line(data = time.data, 
            aes(x=Date2, y=value, group=Attribute, colour=Color)) +
  geom_text(data = label.data,
            aes(x=Date2, y=value, label=Attribute, colour=label.color),
            hjust = 1) +
  facet_grid(Attribute.Category~.) +
  scale_colour_identity()

enter image description here

主题内容(以及使标签在图表外可见)与问题无关,x轴转换从日期到数字也不能处理Inf。我还将数据修剪为仅需要的列,并将分类变量减少到仅两个类别。

相关问题