R中线图ggplot2的手册图例标签 [英] Manual legend labels for line graph ggplot2 in R

查看:884
本文介绍了R中线图ggplot2的手册图例标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个相当基本的问题,但我对ggplot2比较陌生,我似乎无法弄清楚这一点。如果对于我在这里误解的语法有一些基本的了解,如果有人能够直接指出我的意思,那将是非常棒的。或者只是告诉我如何改变这些标签会很棒... ...

假设我有这个(假的)数据:

  avgTerms<  -  data.frame(itNum = seq(1,15),
i15 = runif(15,5,7),
i20 = runif(15,5.5,7.5),
i25 = runif(15,4,7),
i30 = runif(15,6,8))


我这样做了一个基本的线条图:

 <$ c##colour palette(colorblind-friendly)
cbb <-c(#000000,#E69F00,#56B4E9,#009E73,#F0E442,#0072B2 ,#D55E00,#CC79A7)
#plot
avgTermsplot <-ggplot(data = avgTerms,aes(itNum,avgTerms [,2])))
avgTermsplot< - avgTermsplot + geom_line(aes(itNum,avgTerms [,2],color = cbb [2]))
avgTermsplot < - avgTermsplot + geom_line(aes(itNum,avgTerms [,3],color = cbb [3 ]))
avgTermsplot< - avgTermsplot + geom_line(aes(itNum,avgTerms [,4],color = cbb [4]))
avgTermsplot< - avgTermspl ot + geom_line(aes(itNum,avgTerms [,5],color = cbb [5]))
avgTermsplot < - avgTermsplot + labs(x =Iteration Number,y = )

print(avgTermsplot)

正如您所看到的,图例是颜色代码。没有用。我希望它们成为data.frame中的列名称。 (如在,他们应该是 i15 i20 等等)我尝试了一堆东西,尝试从矢量名称(avgTerms)[2:5] 分配它们,但这些东西都不起作用,所以我不会在这里列出它们。有没有一种简单的方法来分配字符矢量的图例标签?



非常感谢您的帮助。

解决方案

建议使用长格式的数据作为 ggplot2 ,所以首先要融化数据:

  library(reshape2)
avgTerms.m< - melt(avgTerms,id.vars =itNum)

然后根据变量分配颜色:

  ggplot(data = avgTerms.m,aes(itNum,value,color = variable))+ 
geom_line()+
labs (x =Iteration Number,y =平均应用标签数量)+
scale_colour_manual(values = cbb)


This seems like a fairly basic question, but I'm relatively new to ggplot2 and I can't seem to figure this out. If there is something basic about the "grammar" that I'm misunderstanding here, it would be great if someone could point me in the right direct. Or just telling me how to change these labels would be great...

Say I have this (fake) data:

avgTerms <- data.frame(itNum = seq(1,15),
                   i15 = runif(15,5,7),
                   i20 = runif(15,5.5,7.5),
                   i25 = runif(15,4,7),
                   i30 = runif(15,6,8))

I make a basic line plot with it like so:

#colour palette (colorblind-friendly)
cbb <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#plot
avgTermsplot <- ggplot(data=avgTerms, aes(itNum, avgTerms[,2]))
avgTermsplot <- avgTermsplot + geom_line(aes(itNum, avgTerms[,2], colour=cbb[2]))
avgTermsplot <- avgTermsplot + geom_line(aes(itNum, avgTerms[,3], colour=cbb[3]))
avgTermsplot <- avgTermsplot + geom_line(aes(itNum, avgTerms[,4], colour=cbb[4]))
avgTermsplot <- avgTermsplot + geom_line(aes(itNum, avgTerms[,5], colour=cbb[5]))
avgTermsplot <- avgTermsplot + labs(x="Iteration Number", y="Avg # of Tags Applied")

print(avgTermsplot)

As you can see, the labels in the legend are the color codes. Not useful. I want them to be the column names from the data.frame. (As in, they should be i15, i20, etc.) I've tried a bunch of things, trying to assign them from the vector names(avgTerms)[2:5] but none of those things seem to work, so I won't list them all here. Is there a simple way to assign legend labels from a character vector?

Thank you very much for any help.

解决方案

It's recommended to use data in long format for ggplot2, so melt your data first:

library(reshape2)
avgTerms.m <- melt(avgTerms, id.vars = "itNum")

And then assign the colours based on variable:

ggplot(data = avgTerms.m, aes(itNum, value, colour = variable)) +
  geom_line() +
  labs(x = "Iteration Number", y = "Avg # of Tags Applied") +
  scale_colour_manual(values = cbb)

这篇关于R中线图ggplot2的手册图例标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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