在1张图中使用多个geom_point和geom_line函数不会显示图例 [英] No legend shows up using multiple geom_point and geom_line functions in 1 graph

查看:225
本文介绍了在1张图中使用多个geom_point和geom_line函数不会显示图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习R,ggplot2等的内容-习惯于以A到Z的方式学习一种完整的(固定的)编码语言(不用于开源-我学会了编码恐龙在地球上漫游时).因此,我将以下代码合并在一起以创建一个图形.只是...我没有伪造的传奇问题-我没有传奇!

I'm struggling to learn the ins and outs of R, ggplot2, etc - being more used to being taught in an A to Z manner an entire (fixed) coding language (not used to open source - I learned to code when dinosaurs roamed the earth). So I have kluged together the following code to create one graph. Only ... I don't have the dupe legends problem -- I have no legend a'tall!

erc <- ggplot(usedcarval, aes(x = usedcarval$age))   +
  geom_line(aes(y = usedcarval$dealer), colour = "orange", size = .5) +
  geom_point(aes(y = usedcarval$dealer), 
             show.legend = TRUE, colour = "orange", size = 1) +
  geom_line(aes(y = usedcarval$pvtsell), colour = "green", size = .5) +
  geom_point(aes(y = usedcarval$pvtsell), colour = "green", size = 1) +
  geom_line(aes(y = usedcarval$tradein), colour = "blue", size = .5) +
  geom_point(aes(y = usedcarval$tradein), colour = "blue", size = 1) +
  geom_line(aes(y = as.integer(predvalt)), colour = "gray", size = 1) +
  geom_line(aes(y = as.integer(predvalp)), colour = "gray", size = 1) + 
  geom_line(aes(y = as.integer(predvald)), colour = "gray", size = 1) +
  labs(x = "Value of a Used Car as it Ages (Years)", y = "Dollars") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 60, vjust = .6)) 
erc 

我不知道如何在此文本中放置图片,因为除我的保管箱外没有其他链接...

I can't figure out how to put an image in this text since I have no link except to my dropbox...

我将不胜感激.此致,斯蒂芬妮

I would appreciate any help. Sincerely, Stephanie

推荐答案

好吧,我想做一些ggplot,与ggplot-beginners(我不久前是一个)比较它的方式相比,这是一个有趣的任务.与获取传说之类的东西所需的方式相比.

Ok, I felt like doing some ggplot, and it was an interesting task to contrast the way ggplot-beginners (I was one not so long ago) approach it compared to the way you need to do it to get things like legends.

这是代码:

library(ggplot2)
library(gridExtra)
library(tidyr)

# fake up some data
n <- 100
dealer <- 12000 + rnorm(n,0,100)
age <- 10 + rnorm(n,3)
pvtsell <- 10000 + rnorm(n,0,300)
tradein <- 5000 + rnorm(n,0,100)
predvalt <- 6000 + rnorm(n,0,120)
predvalp <- 7000 + rnorm(n,0,100)
predvald <- 8000 + rnorm(n,0,100)
usedcarval <- data.frame(dealer=dealer,age=age,pvtsell=pvtsell,tradein=tradein,
                        predvalt=predvalt,predvalp=predvalp,predvald=predvald)

# The ggplot-naive way
erc <- ggplot(usedcarval, aes(x = usedcarval$age))   +
  geom_line(aes(y = usedcarval$dealer), colour = "orange", size = .5) +
  geom_point(aes(y = usedcarval$dealer), 
             show.legend = TRUE, colour = "orange", size = 1) +
  geom_line(aes(y = usedcarval$pvtsell), colour = "green", size = .5) +
  geom_point(aes(y = usedcarval$pvtsell), colour = "green", size = 1) +
  geom_line(aes(y = usedcarval$tradein), colour = "blue", size = .5) +
  geom_point(aes(y = usedcarval$tradein), colour = "blue", size = 1) +
  geom_line(aes(y = as.integer(predvalt)), colour = "gray", size = 1) +
  geom_line(aes(y = as.integer(predvalp)), colour = "gray", size = 1) + 
  geom_line(aes(y = as.integer(predvald)), colour = "gray", size = 1) +
  labs(x = "ggplot naive way - Value of a Used Car as it Ages (Years)", y = "Dollars") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 60, vjust = .6)) 

# The tidyverse way
#    ggplot needs long data, not wide data. 
#    Also we have two different sets of data for points and lines

gdf <- usedcarval %>% gather(series,value,-age)
pdf <- gdf %>% filter( series %in% c("dealer","pvtsell","tradein"))

# our color and size lookup tables
clrs = c("dealer"="orange","pvtsell"="green","tradein"="blue","predvalt"="gray","predvalp"="gray","predvald"="gray")
szes = c("dealer"=0.5,"pvtsell"=0.0,"tradein"=0.5,"predvalt"=1,"predvalp"=1,"predvald"=1)

trc <- ggplot(gdf,aes(x=age)) + geom_line(aes(y=value,color=series,size=series)) + 
  scale_color_manual(values=clrs) +
  scale_size_manual(values=szes) +
  geom_point(data=pdf,aes(x=age,y=value,color=series),size=1) + 
  labs(x = "tidyverse way - Value of a Used Car as it Ages (Years)", y = "Dollars") +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 60, vjust = .6)) 

grid.arrange(erc, trc, ncol=1)

进行研究,尤其是查看gdfpdfgather.不使用长数据"就无法获得图例.

Study it, espeically look at gdf,pdf and gather. You just can't get legends without using "long data".

如果您想了解有关"tidyverse"的更多信息,请从此处开始: Hadley Wickham的tidyverse

If you want more information on the "tidyverse", start here: Hadley Wickham's tidyverse

这篇关于在1张图中使用多个geom_point和geom_line函数不会显示图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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