为geom_polygon添加图例

时间:2016-09-28 06:14:27

标签: r ggplot2 legend

我正在尝试使用geom_point生成散点图,其中点由平滑多边形限定,并带有geom_polygon

这是我的观点数据:

set.seed(1)
df <- data.frame(x=c(rnorm(30,-0.1,0.1),rnorm(30,0,0.1),rnorm(30,0.1,0.1)),y=c(rnorm(30,-1,0.1),rnorm(30,0,0.1),rnorm(30,1,0.1)),val=rnorm(90),cluster=c(rep(1,30),rep(2,30),rep(3,30)),stringsAsFactors=F)

我根据df$val所在的间隔为每个点着色。这是间隔数据:

intervals.df <- data.frame(interval=c("(-3,-2]","(-2,-0.999]","(-0.999,0]","(0,1.96]","(1.96,3.91]","(3.91,5.87]","not expressed"),
                           start=c(-3,-2,-0.999,0,1.96,3.91,NA),end=c(-2,-0.999,0,1.96,3.91,5.87,NA),
                           col=c("#2f3b61","#436CE8","#E0E0FF","#7d4343","#C74747","#EBCCD6","#D3D3D3"),stringsAsFactors=F)

为点指定颜色和间隔:

df <- cbind(df,do.call(rbind,lapply(df$val,function(x){
  if(is.na(x)){
    return(data.frame(col=intervals.df$col[nrow(intervals.df)],interval=intervals.df$interval[nrow(intervals.df)],stringsAsFactors=F))
  } else{
    idx <- which(intervals.df$start <= x & intervals.df$end >= x)
    return(data.frame(col=intervals.df$col[idx],interval=intervals.df$interval[idx],stringsAsFactors=F))
  }
})))

准备leged的颜色,以显示每个间隔:

df$interval <- factor(df$interval,levels=intervals.df$interval)
colors <- intervals.df$col
names(colors) <- intervals.df$interval

这是我构建平滑多边形的地方(使用这个link提供的函数):

clusters <- sort(unique(df$cluster))
cluster.cols <- c("#ff00ff","#088163","#ccbfa5")


splinePolygon <- function(xy,vertices,k=3, ...)
{
  # Assert: xy is an n by 2 matrix with n >= k.
  # Wrap k vertices around each end.
  n <- dim(xy)[1]
  if (k >= 1) {
    data <- rbind(xy[(n-k+1):n,], xy, xy[1:k, ])
  } else {
    data <- xy
  }
  # Spline the x and y coordinates.
  data.spline <- spline(1:(n+2*k), data[,1], n=vertices, ...)
  x <- data.spline$x
  x1 <- data.spline$y
  x2 <- spline(1:(n+2*k), data[,2], n=vertices, ...)$y
  # Retain only the middle part.
  cbind(x1, x2)[k < x & x <= n+k, ]
}

library(data.table)
hulls.df <- do.call(rbind,lapply(1:length(clusters),function(l){
  dt <- data.table(df[which(df$cluster==clusters[l]),])
  hull <- dt[, .SD[chull(x,y)]]
  spline.hull <- splinePolygon(cbind(hull$x,hull$y),100)
  return(data.frame(x=spline.hull[,1],y=spline.hull[,2],val=NA,cluster=clusters[l],col=cluster.cols[l],interval=NA,stringsAsFactors=F))
}))
hulls.df$cluster <- factor(hulls.df$cluster,levels=clusters)

这是我的ggplot命令:

library(ggplot2)

p <- ggplot(df,aes(x=x,y=y,colour=interval))+geom_point(cex=2,shape=1,stroke=1)+labs(x="X", y="Y")+theme_bw()+theme(legend.key=element_blank(),panel.border=element_blank(),strip.background=element_blank())+scale_color_manual(drop=FALSE,values=colors,name="DE")
p <- p+geom_polygon(data=hulls.df,aes(x=x,y=y,group=cluster),color=hulls.df$col,fill=NA)

产生:

enter image description here

我的问题是如何在点的图例下为多边形添加图例?我希望它有一个图例,根据簇颜色和每行旁边的相应簇号有3条线?

3 个答案:

答案 0 :(得分:3)

略有不同的输出,只更改代码的最后一行,它可能会解决您的目的:

p+geom_polygon(data=hulls.df,aes(x=x,y=y,group=cluster, fill=cluster),alpha=0.1)

enter image description here

答案 1 :(得分:3)

说,您想添加legend the_factor。我的基本想法是,

(1)使用未使用的the_factor参数将aes置于映射中; aes(xx = the_factor)
(2)如果(1)影响某些事物,请使用scale_xx_manual()
删除效果 (3)使用legend

修改guides(xx = guide_legend(override.aes = list()))

在您的情况下,aes(fill)aes(alpha)未使用。前者因为没有效果而做得更好。所以我使用了aes(fill=as.factor(cluster))

p <- ggplot(df,aes(x=x,y=y,colour=interval, fill=as.factor(cluster))) +   # add aes(fill=...)
  geom_point(cex=2, shape=1, stroke=1) + 
  labs(x="X", y="Y",fill="cluster") +          # add fill="cluster"
  theme_bw() + theme(legend.key=element_blank(),panel.border=element_blank(),strip.background=element_blank()) + scale_color_manual(drop=FALSE,values=colors,name="DE") +
  guides(fill = guide_legend(override.aes = list(colour = cluster.cols, pch=0))) # add

p <- p+geom_polygon(data=hulls.df,aes(x=x,y=y,group=cluster), color=hulls.df$col,fill=NA)


当然,您可以使用aes(alpha = the_factor))制作相同的图表。因为它有影响力,所以您需要使用scale_alpha_manual()来控制它。

g <- ggplot(df, aes(x=x,y=y,colour=interval)) +
  geom_point(cex=2, shape=1, stroke=1, aes(alpha=as.factor(cluster))) +  # add aes(alpha)
  labs(x="X", y="Y",alpha="cluster") +          # add alpha="cluster"
  theme_bw() + theme(legend.key=element_blank(),panel.border=element_blank(),strip.background=element_blank()) + scale_color_manual(drop=FALSE,values=colors,name="DE") +
  scale_alpha_manual(values=c(1,1,1)) +         # add
  guides(alpha = guide_legend(override.aes = list(colour = cluster.cols, pch=0))) # add

g <- p+geom_polygon(data=hulls.df,aes(x=x,y=y,group=cluster), color=hulls.df$col,fill=NA)

enter image description here

答案 2 :(得分:2)

你要的是两种色标。我的理解是这是不可能的。但是你可以给人留下两个颜色标度,一点cheat并使用填充符号(形状21到25)的印象。

p <- ggplot(df, aes(x = x, y = y, fill = interval)) +
  geom_point(cex = 2, shape = 21, stroke = 1, colour = NA)+
  labs(x = "X", y = "Y") +
  theme_bw() +
  theme(legend.key = element_blank(), panel.border = element_blank(), strip.background = element_blank()) +
  scale_fill_manual(drop=FALSE, values=colors, name="DE") + 
  geom_polygon(data = hulls.df, aes(x = x, y = y, colour = cluster), fill = NA) + 
  scale_colour_manual(values = cluster.cols)
p

或者,使用具有低alpha

的填充多边形
p <- ggplot(df,aes(x=x,y=y,colour=interval))+
  geom_point(cex=2,shape=1,stroke=1)+
  labs(x="X", y="Y")+
  theme_bw() +
 theme(legend.key = element_blank(),panel.border=element_blank(), strip.background=element_blank()) +
  scale_color_manual(drop=FALSE,values=colors,name="DE", guide = guide_legend(override.aes = list(fill = NA))) +
  geom_polygon(data=hulls.df,aes(x=x,y=y,group=cluster, fill = cluster),    alpha = 0.2, show.legend = TRUE) + 
      scale_fill_manual(values = cluster.cols) 
    p

但这可能会让点颜色难以看清。