3

私は現在、これを作成するために以下に含まれているコードを持っています: ここに画像の説明を入力してください これは多かれ少なかれ私が望む方法です。ただし、変更できるようにしたいが、自分で整理するのに苦労している2つの優れた点は、次のとおりです。1)「グループ」というタイトルの凡例を削除し、「平均」という見出しの付いたグラフ上の太い黒線の凡例を作成します。 '(線自体が黒のままであることを確認しながら)および2)表示されるgeom_polygonsおよびgeom_pointsの塗りつぶし色をカスタムカラー値に変更する機能(凡例を非表示にすることなく)。ありがとう。

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")
jdata <-pdata

pdata$group <- recode(pdata$Var, "G1 = 'A'; G2 = 'B'; G3 = 'C'; G4 = 'D'; G5 = 'E'")

VarArea <- data.frame(unique(pdata$Var))
VarFinalArea <-c()
for (g in 1:max(VarArea))
{
VarNum<-pdata[which(pdata$Var==g),1:c(ncol(pdata))]
VarMin <- min(VarNum$value)
VarMax <- max(VarNum$value)
VarMinMax <- cbind(VarMin, VarMax)
VarFinalArea <- rbind(VarFinalArea,VarMinMax)
}
VarFinal <- data.frame(cbind(VarArea,VarFinalArea))
colnames(VarFinal)<-c("Variable", "Min", "Max")
VarFinal$group <- recode(VarFinal$Variable, "G1 = 'A'; G2 = 'B'; G3 = 'C'; G4 = 'D'; G5 = 'E'")

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

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

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

VLarge <- VFinal[which(VFinal$Value >= VFinalMax_Min),]
VLarge <- VLarge[order(-VLarge$Variable, VLarge$Group),]
VSmall <- VFinal[which(VFinal$Value <= VFinalMin_Max),]
VSmall <- VSmall[order(VSmall$Variable, VSmall$Group),]
VFinal <- rbind(VSmall, VLarge)

AMin <-min(jdata$value)
AMax <-max(jdata$value)

AMinValue<-round_any(AMin,1000, f =floor)
AMaxValue<-round_any(AMax,1000, f =ceiling)

ggplot(VFinal, aes(Variable, Value, colour = Group)) + geom_polygon(colour=NA, aes(fill=Group), alpha=0.5) +scale_x_discrete(name="Missing Variable Number", limits=c(1:NumV)) + theme(axis.text.x=element_text(angle=270, vjust=0.5, hjust=0.0))+ scale_y_continuous(name="Within Cluster Sum of Squares", limits=c(AMinValue, AMaxValue), breaks = seq(AMinValue, AMaxValue, 1000)) + guides(fill=guide_legend(title="Variable Groups"))

last_plot()+geom_line(data=subset(pdata,variable =='Mean'),size=1.5, alpha=0.5, aes(Var, value), colour="black", inherit.aes = FALSE)

last_plot()+geom_line(data=subset(pdata,variable!='Mean'),size=0.5, alpha=0.5, aes(Var, value, shape=variable), colour='black', inherit.aes = FALSE) 

last_plot()+geom_point(data=subset(pdata,variable!='Mean'), aes(Var, value, shape = variable, col=group),alpha=1.0, inherit.aes = FALSE) + labs (shape = "Number of Clusters") + guides(scale_alpha(guide='none'))

last_plot()+ ggtitle("Clusters with Missing Variables") + theme(plot.title = element_text(size = 14, colour = "black", face = "bold"))
4

1 に答える 1

1

これを作成するのを手伝ってくれた baptiste と user1935457 に感謝します:

ここに画像の説明を入力

これがどのように行われたかです:

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")
jdata <-pdata

pdata$group <- recode(pdata$Var, "G1 = 'A'; G2 = 'B'; G3 = 'C'; G4 = 'D'; G5 = 'E'")

VarArea <- data.frame(unique(pdata$Var))
VarFinalArea <-c()
for (g in 1:max(VarArea))
{
VarNum<-pdata[which(pdata$Var==g),1:c(ncol(pdata))]
VarMin <- min(VarNum$value)
VarMax <- max(VarNum$value)
VarMinMax <- cbind(VarMin, VarMax)
VarFinalArea <- rbind(VarFinalArea,VarMinMax)
}
VarFinal <- data.frame(cbind(VarArea,VarFinalArea))
colnames(VarFinal)<-c("Variable", "Min", "Max")
VarFinal$group <- recode(VarFinal$Variable, "G1 = 'A'; G2 = 'B'; G3 = 'C'; G4 = 'D'; G5 = 'E'")

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

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

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

VLarge <- VFinal[which(VFinal$Value >= VFinalMax_Min),]
VLarge <- VLarge[order(-VLarge$Variable, VLarge$Group),]
VSmall <- VFinal[which(VFinal$Value <= VFinalMin_Max),]
VSmall <- VSmall[order(VSmall$Variable, VSmall$Group),]
VFinal <- rbind(VSmall, VLarge)

AMin <-min(jdata$value)
AMax <-max(jdata$value)

AMinValue<-round_any(AMin,1000, f =floor)
AMaxValue<-round_any(AMax,1000, f =ceiling)

ggplot(VFinal, aes(Variable, Value, colour = Group)) + geom_polygon(colour=NA, aes(fill=Group), alpha=0.5) +scale_x_discrete(name="Missing Variable Number", limits=c(1:NumV)) + theme(axis.text.x=element_text(angle=270, vjust=0.5, hjust=0.0))+ scale_y_continuous(name="Within Cluster Sum of Squares", limits=c(AMinValue, AMaxValue), breaks = seq(AMinValue, AMaxValue, 1000)) + guides(fill=guide_legend(title="Variable Groups"))+ scale_fill_manual("Variable Groups", values = c("A" = "red", "B" = "blue", "C" = "purple", "D" = "salmon", "E" = "orange"))

last_plot()+geom_line(data=subset(pdata,variable =='Mean'), alpha=0.5, aes(Var, value, size= ''), colour="black", inherit.aes = FALSE)+ guides (size = guide_legend(title = "Mean", override.aes = list(size = 1.5, colour = "black")))

last_plot()+geom_line(data=subset(pdata,variable!='Mean'),size=0.5, alpha=0.5, aes(Var, value, shape=variable), colour='black', inherit.aes = FALSE) 

last_plot()+geom_point(data=subset(pdata,variable!='Mean'), aes(Var, value, shape = variable, col=group),alpha=1.0, inherit.aes = FALSE) + labs (shape = "Number of Clusters") + guides(scale_alpha(guide='none')) + guides(colour = "none") + scale_colour_manual("Variable Groups", values = c("A" = "red", "B" = "blue", "C" = "purple", "D" = "salmon", "E" = "orange"))

last_plot()+ ggtitle("Clusters with Missing Variables") + theme(plot.title = element_text(size = 14, colour = "black", face = "bold"))
于 2013-01-21T23:32:40.273 に答える