Multiple ggplots causing legend issues

Currently, the code below is generated for this: enter image description here This is more or less how I want it. However, there are two outstanding things that I would like to change, but I am struggling to understand myself: 1) Delete the legend with the name "Group" and get the legend for a thicker black line on the graph titled "Average (while make sure the row itself stays black) and 2) Ability to change the fill colors of geom_polygons and geom_points, which are mapped to custom color values โ€‹โ€‹(without fading legends) Thanks.

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

      

+3


source to share


1 answer


Thanks to baptiste and user1935457 for helping create this:

enter image description here



Here's how it was done:

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

      

+1


source







All Articles