没有图例显示在1个图形中使用多个geom_point和geom_line函数

时间:2017-03-15 17:40:14

标签: r ggplot2 legend

我正在努力学习R,ggplot2等的来龙去脉 - 更习惯于以A到Z的方式教授整个(固定)编码语言(不习惯开源 - 我学会了在恐龙漫游地球时编码。所以我将以下代码拼凑在一起以创建一个图表。只有......我没有传说中的愚蠢传说 - 我没有传说中的高个子!

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 

我无法弄清楚如何在此文本中放置图片,因为除了我的Dropbox之外我没有任何链接......

我将不胜感激任何帮助。真诚的,斯蒂芬妮

2 个答案:

答案 0 :(得分:2)

好吧,我觉得自己喜欢做一些ggplot,这是一个有趣的任务,与ggplot-beginners(我不是很久以前的人)接近它的方式形成鲜明对比,相比之下你需要这样做才能得到像传说这样的东西

以下是代码:

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)

enter image description here

研究它,特别是gdfpdfgather。如果不使用“长数据”,你就无法获得传说。

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

答案 1 :(得分:1)

如果您正在寻找一个如何获取宽格式系列数据的简短示例,请将其转换为长格式(使用gather),然后使用ggplot绘制(使用图例) ,这是我最近为某人做的一个很好的简短例子:

library(ggplot2)
library(tidyr)

# womp up some fake news (uhh... data)
x <- seq(-pi,pi,by=0.25)
y <- sin(x)
yhat <- sin(x) + 0.4*rnorm(length(x))

# This is the data in wide form 
#      you will never get ggplot to make a legend for it
#     it simply hates wide data
df1 <- data.frame(x=x,y=y,yhat=yhat)

# So we use gather from tidyr to make it into long data
#      creates two new colums, throws y and yhat in them, and replicates x as needed
#       you have to look at the data frame to understand gather,
#       and read the docs a few times
df2 <- gather(df1,series,value,-x)   

# it is now in long form and we can plot it
ggplot(df2) + geom_line(aes(x,value,color=series))

所以这是情节:

enter image description here