用ggplot绘制多个geom_point组的轮廓 [英] Drawing outlines around multiple geom_point groups with ggplot

查看:168
本文介绍了用ggplot绘制多个geom_point组的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当然有以下代码来绘制:

p>

我试图做的是让每个组的大纲遵循每个组中的所有要点 - 而不是像目前那样跳过其中的一些。另外,我希望每个大纲都有一个半透明的填充。谢谢你的帮助。

 图书馆(ggplot2)
图书馆(重塑)
图书馆(汽车)

G1< - 1:10
G2< - 11:20
G3< - 21:30
G4< - 31:35
G5< - 36:41

sdata< - read.csv(http://dl.dropbox.com/u/58164604/sdata.csv,stringsAsFactors = FALSE)
pdata< - (data)(unique(pdata $ Var))
VarFinalMin< -c()
(g in 1:max(VarArea))
{
VarNum <-pdata [which(pdata $ Var == g),1:c(ncol(pdata))]
VarN < - g
VarMin < - min(VarNum $ value)
VarMinN < - cbind(VarN,VarMin)
VarFinalMin < - rbind(VarFinalMin,VarMinN)
}
VFinalMin < - data.frame(VarFinalMin)
colnames(VFinalMin)< -c(Variable,Value)

VarFinalMax< -c )
for(g in 1:max(VarArea))
{
VarNum <-pdata [which(pdata $ Var == g),1:c(ncol(pdata))]
VarN < - g
VarMax < - max(VarNum $ value)
VarMaxN < - cbind(VarN,VarMax)
VarFinalM (VarFinalMax,VarMaxN)
}
VFinalMax < - data.frame(VarFinalMax)
colnames(VFinalMax)< -c(Variable,Value)

VFinal <-rbind(VFinalMin,VFinalMax)
VFinal $ Group < - 重新编码(VFinal $ Variable,G1 ='A'; G2 ='B'; G3 ='C'; G4 ='D'; G5 ='E')

ggplot(VFinal,aes(Variable,Value,color = Group))+ geom_point()
find_hull < - function(VFinal)VFinal [chull VFinal $ Variable,VFinal $ Value),]
船体< - ddply(VFinal,Group,find_hull)
ggplot(VFinal,aes(Variable,Value,color = Group))+ geom_point )+ geom_polygon(data = hulls,fill = NA)


解决方案

如果您分别对图形的底部和顶部进行升序和降序排序,您可以在原始的 data.frame 上使用 geom_polygon $ c $>并跳过所有的凸包:

  VLarge < -  VFinal [which(VFinal $ Value> 25000) ,] 
VLarge < - VLarge [order(-VLarge $ Variable,VLarge $ Group),]
VSmall <-VFinal [其中(VFinal $ Value <= 25000),]
VSmall <-VSmall [order(VSmall $ Variable,VSmall $ Group),]
VFinal <-rbind(VSmall,VLarge)
ggplot(VFinal,aes(Variable,Value,color = Group ))+ geom_point( )+
geom_polygon(aes(fill = Group),alpha = 0.3)


I currrently have the code included below to draw this:

What I am trying to do is get the outline for each of the groups to follow all of the points in each group - rather than skip some of them as it currently does. In addition I would want each outline to have a semi-transparent fill. Thanks for any help.

library(ggplot2)
library(reshape)
library(car)

G1 <- 1:10
G2 <- 11:20
G3 <- 21:30
G4 <- 31:35
G5 <- 36:41

sdata <- read.csv("http://dl.dropbox.com/u/58164604/sdata.csv", stringsAsFactors = FALSE)
pdata<-melt(sdata, id.vars="Var")

VarArea <- data.frame(unique(pdata$Var))
VarFinalMin <-c()
for (g in 1:max(VarArea))
{
VarNum<-pdata[which(pdata$Var==g),1:c(ncol(pdata))]
VarN <- g
VarMin <- min(VarNum$value)
VarMinN <- cbind(VarN, VarMin)
VarFinalMin <- rbind(VarFinalMin,VarMinN) 
}
VFinalMin <- data.frame(VarFinalMin)
colnames(VFinalMin)<-c("Variable", "Value")

VarFinalMax <-c()
for (g in 1:max(VarArea))
{
VarNum<-pdata[which(pdata$Var==g),1:c(ncol(pdata))]
VarN <- g
VarMax <- max(VarNum$value)
VarMaxN <- cbind(VarN, VarMax)
VarFinalMax <- rbind(VarFinalMax,VarMaxN)
}
VFinalMax <- data.frame(VarFinalMax)
colnames(VFinalMax)<-c("Variable", "Value")

VFinal<-rbind(VFinalMin, VFinalMax)
VFinal$Group <- recode(VFinal$Variable, "G1 = 'A'; G2 = 'B'; G3 = 'C'; G4 = 'D'; G5 = 'E'")

ggplot(VFinal, aes(Variable, Value, colour = Group)) + geom_point()
find_hull <- function(VFinal) VFinal[chull(VFinal$Variable, VFinal$Value), ]
hulls <- ddply(VFinal, "Group", find_hull)
ggplot(VFinal, aes(Variable, Value, colour = Group)) + geom_point() + geom_polygon(data = hulls, fill = NA)

解决方案

If you reorder the bottoms and tops of your shapes respectively ascending and descending, you can just use geom_polygon on your original data.frame and skip all the convex hull stuff:

VLarge <- VFinal[which(VFinal$Value > 25000),]
VLarge <- VLarge[order(-VLarge$Variable, VLarge$Group),]
VSmall <- VFinal[which(VFinal$Value <= 25000),]
VSmall <- VSmall[order(VSmall$Variable, VSmall$Group),]
VFinal <- rbind(VSmall, VLarge)
ggplot(VFinal, aes(Variable, Value, colour = Group)) + geom_point() + 
    geom_polygon(aes(fill = Group), alpha = 0.3)

这篇关于用ggplot绘制多个geom_point组的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆