使用R中的ggplot2更改图例中的颜色 [英] Changing the color in the legend with ggplot2 in R

查看:1237
本文介绍了使用R中的ggplot2更改图例中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的问题,在ggplot的传说中指定颜色。我试图做一个简化的例子来说明我的问题:

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

由于某些原因,标题DE下的传说都是蓝色的。我希望他们是黑人,但我不知道他们为什么是蓝色的,所以我不知道如何改变他们。

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

如果我改变颜色以改变填充值(通过put ting color = DE进入aes),然后那些在图例中可见。不过,我喜欢在点附近有黑色边框。我只想让图例中的点内部反映点填充情节。 (我也想把这两个传说并排放置,但我真的只是想让颜色现在正常工作)

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?

推荐答案

对于问题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")

对于问题2:

我不认为你的填充符合你的数据。您应该将值的名称分配给 scale_x_manual 调用中的每种颜色。

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

我不知道您是否可以控制点在传说中输入。也许有更多关于 ggplot2 的知识的人可以算出来。

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.

这篇关于使用R中的ggplot2更改图例中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆