简体   繁体   English

使用R中的ggplot2更改图例中的颜色

[英]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. 我在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. 出于某种原因,标题DE下的图例中显示的线型都是蓝色。 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. 如果我改变颜色以及填充(通过将颜色= DE放入aes中),那么这些颜色在图例中可见。 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: 问题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: 问题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. 您应该在scale_x_manual调用中为每种颜色指定值的名称。

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. 也许对ggplot2有更多了解的其他人可以搞清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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