简体   繁体   中英

Changing the color in the legend with ggplot2 in R

I'm having two different problems with specifying the colors in my legends in ggplot. I've tried to make a simplified examples that shows 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 title DE are all blue. I'd like them to be black, but I have no idea why they're blue in the first place, so I'm not sure how to change them.

For my other problem, I'm trying to use both point color and point shape to show two different distinctions in my data. I'd like to have legends for both of these. Here's 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 to change as well as the fill (by putting color=DE into the aes), then those are visible in the legend. I like having the black border around the points, though. I'd just like to have the inside of the points in the legend reflect the point fill in the plot. (I'd also like to position the two legends side-by-side, but I really just want to get the color to work right now)

I've spent way too long googling about both of these problems and trying various solutions without any success. Does anyone have any idea what I'm doing wrong?

For question 1:

Give the legend for line type and the legend for colour 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 do not think your fills are matching your data. You should assign the name of the value to each colour in the scale_x_manual calls.

I couldn't get the black border for the points. 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 don't know if you can control the point type inside the legends. Maybe someone else with more knowledge of ggplot2 can figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM