如何在ggplot中创建不同颜色的图例文本元素?

时间:2015-03-20 19:52:13

标签: r ggplot2

我试图让图例中的文字与ggplot中的线条颜色相同。说女性的行为绿色,男性为蓝色,然后文本说女性为绿色,男性为蓝色。

df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42))

ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + 
geom_line()+
scale_color_manual("sex",values=c("green", "blue"))

测试代码取自: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

0 个答案:

没有答案