Add legend for geom_polygon

I am trying to create a scatter plot with geom_pointwhere points are bounded by a smooth polygon, c geom_polygon.

Here is my point data:

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)

I mark each point according to the interval in which it is located df$val. Here is the interval data:

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)

Assign colors and spacing to points:

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))
  }
})))

Preparing colors for leged that will show each interval:

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

Here, where I built the smooth polygons (using the function kindly provided by this 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)

And here is my command 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)

which produces:

enter image description here

: ? , 3- , ?

+4
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

+3

, , :

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

enter image description here

+2

What you are asking for are two color scales. I understand that this is not possible. But you can create the impression that you have two color scales with a small amount of cheat and using filled characters (forms from 21 to 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

Alternatively use a filled low alpha polygon

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

But this can make dot colors difficult to display.

+2
source

Source: https://habr.com/ru/post/1656051/


All Articles