简体   繁体   English

R ggplot2:在此示例数据集中更改图例标题和标签

[英]R ggplot2: changing the legend title and labels in this example dataset

n   value   name
1   20  1
2   30  1
3   25  1
1   40  2
2   12  2
3   39  2

This is how I plot it now: 这是我现在的绘制方式:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value,color=as.character(name))) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))
dev.off()

在此处输入图片说明 I would like to change "as.character(name)" to "New Title" and the values "1" and "2" to "value1" and "value2". 我想将“ as.character(name)”更改为“ New Title”,并将值“ 1”和“ 2”更改为“ value1”和“ value2”。

I tried the following but it didn't work: 我尝试了以下操作,但没有成功:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value,color=as.character(name))) + geom_point(aes(n,value)) 
+ geom_line(aes(n,value)) +
 scale_fill_manual(name="My title", values=c("value1", "value2"))
dev.off()

You need scale_color_manual not scale_fill_manual . 您需要scale_color_manual而不是scale_fill_manual Then values refers to color values, which you must supply, and the third parameter is labels . 然后, values必须提供的颜色值,第三个参数是labels

+ scale_color_manual(name = "New Title", 
                     labels = c("value1", "value2"), 
                     values = c("red", "green"))

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

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