Color change in legend with ggplot2 in R

I am having two different problems with specifying colors in my legends in ggplot. I tried to make simplified examples that show my problem:

df <- data.frame(x=rep(1:9, 10), y=as.vector(t(aaply(1:10, 1, .fun=function(x){x:(x+8)}))), method=factor(rep(1:9, each=10)), DE=factor(rep(1:9, each=10))) ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) + geom_smooth(stat="identity") 

For some reason, the line types shown in the legend under the heading DE are blue. I would like them to be black, but I have no idea why they are blue in the first place, so I'm not sure how to change them.

For my other problem, I am trying to use both the dot color and the dot to show two different differences in my data. I wish they had legends. Here is what I have:

 classifiers <- c("KNN", "RF", "NB", "LR", "Tree") des <- c("Uniform", "Gaussian", "KDE") withoutDE <- c(.735, .710, .706, .628, .614, .720, .713, .532, .523, .557, .677, .641, .398, .507, .538) withDE <- c(.769, .762, .758, .702, .707, .752, .745, .655, .721, .733, .775, .772, .749, .756, .759) df <- data.frame(WithoutDE=withoutDE, WithDE=withDE, DE=rep(des, each=5), Classifier=rep(classifiers, 3)) df <- cbind(df, Method=paste(df$DE, df$Classifier, sep="")) ggplot() + geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier, fill=DE), size=3) + ylim(0,1) + xlim(0,1) + xlab("AUC without DE") + ylab("AUC with DE") + scale_shape_manual(values=21:25) + scale_fill_manual(values=c("pink", "blue", "white"), labels=c("Uniform", "KDE", "Gaussian")) + theme(legend.position=c(.85,.3)) 

If I change the color that needs to be changed, as well as the fill (by putting color = DE in aes), then they are visible in the legend. However, I like to have a black border around the glasses. I just want the inside of the dots in the legend to reflect the dot filling the plot. (I would also like to place two legends side by side, but I just want the color to work right now)

I spent too much time looking for both of these problems and tried the various solutions without much effort. Does anyone know what I'm doing wrong?

+2
source share
1 answer

In question 1:

Give the legend for the line type and color legend with the same name.

 ggplot(df, aes(x, y, color=method, group=DE, linetype=DE)) + geom_smooth(stat="identity") + scale_color_discrete("Line") + scale_linetype_discrete("Line") 

For question 2:

I don’t think your fill matches your data. You must assign a value name for each color in scale_x_manual calls.

I could not get a black border for glasses. Here is what I was able to get, though:

 ggplot() + geom_point(data=df, aes(x=WithoutDE, y=WithDE, shape=Classifier, fill=DE, colour=DE), size=3) + ylim(0,1) + xlim(0,1) + xlab("AUC without DE") + ylab("AUC with DE") + scale_shape_manual(values=21:25) + scale_fill_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"), guide="none") + scale_colour_manual(values=c("Uniform"="pink", "KDE"="blue", "Gaussian"="white"), labels=c("Uniform", "KDE", "Gaussian")) + theme(legend.position=c(.85,.3)) 

I do not know if you can control the type of point inside the legends. Maybe someone else with a lot of ggplot2 knowledge can figure this out.

+6
source

All Articles