在ggplot2中处理NA和传说

时间:2013-11-06 15:15:48

标签: r ggplot2 legend

我的数据框看起来像是:

df<-data.frame(Date=as.Date(c("06-08-10","06-09-10","06-10-10","06-11-10","06-13-10")),closed_this_year_cum_gv=c(3,5,6,7,NA),opened_this_year_cum_gv=c(2,5,6,8,10),closed_last_year_cum_gv=c(5,6,7,8,10),opened_last_year_cum_gv=c(5,6,8,10,NA))

并使用ggplot2:

创建了一个用于绘图的框架
ggplot(df, aes(x=Date))+
  geom_line(aes(y=closed_this_year_cum_gv, color="blue"),linetype="dashed")+
  geom_line(aes(y=opened_this_year_cum_gv, color="blue"))+
  geom_line(aes(y=closed_last_year_cum_gv, color="red"),linetype="dashed")+
  geom_line(aes(y=opened_last_year_cum_gv, color="red"))+
  xlab("Date")+
  ylab("Millions of Dollars")+
  ggtitle("Cummulative Sum of TGV for Opened and Closed Cases - 2013 vs. 2012")

我尝试使用示例数据,但由于某种原因线条没有出现(它们显示我的真实数据)。我希望不要绘制NA,这就是为什么它们不是0。

在我的真实数据中,它是图形,但图例标题为“蓝色”,其内容为“蓝色”和“红色”作为标签。我希望它们按年份标记并打开/关闭。我尝试了各种方法,但似乎没有什么能覆盖传说。

如何控制图例标题和标签?

编辑:更改为“日期”类

2 个答案:

答案 0 :(得分:0)

您需要在aes()中指定适当的映射。试试这个:

ggplot(df, aes(x=Date)) +
  geom_line(aes(y=closed_this_year_cum_gv, color="this", linetype="closed")) +
  geom_line(aes(y=opened_this_year_cum_gv, color="this", linetype="opened")) +
  geom_line(aes(y=closed_last_year_cum_gv, color="last", linetype="closed")) +
  geom_line(aes(y=opened_last_year_cum_gv, color="last", linetype="opened")) +
  xlab("Date") +
  ylab("Millions of Dollars") +
  ggtitle("Cummulative Sum of TGV for Opened and Closed Cases - 2013 vs. 2012") +
  scale_colour_manual(name="year", values=c("this"="blue", "last"="red")) +
  scale_linetype_manual(name="type", values=c(2, 1))

答案 1 :(得分:0)

ggplot非常高兴能够以“长”格式提供数据,而不是广泛。除此之外,更容易将不同的aes stetics映射到数据集中的变量。

# some data massage before the plot
# reshape data from wide to long format 
library(reshape2)
df2 <- melt(df)

# convert variable 'Date' to class 'Date'
df2$Date <- as.Date(df2$Date, format = "%m-%d-%y")

# create two variables
# var1: opened vs closed
df2$var1 <- ifelse(grepl(x = df2$variable, pattern = "opened"), "Opened", "Closed")

# set factor levels so that 'opened' comes before 'closed'
df2$var1 <- factor(df2$var1, levels = c("Opened", "Closed"))

# var2: this vs last year 
df2$var2 <- ifelse(grepl(x = df2$variable, pattern = "this"), "This year", "Last year")


# plot
# use default colours, slightly pale 'red' and 'blue'
ggplot(df2, aes(x = Date, y = value, linetype = var1, colour = var2, group = interaction(var1, var2))) +
  geom_line()

# if you want to set colours to red and blue, add this
+ scale_colour_manual(values = c("red", "blue"))

更新以下评论
如果您只想要一个图例,则可以让linetypecolour依赖于“变量”。

# set factor levels so that 'opened' comes before 'closed', and 'last' before 'this'
df2$variable <- factor(df2$variable,
                   levels = c("opened_last_year_cum_gv",
                              "closed_last_year_cum_gv",
                              "opened_this_year_cum_gv",
                              "closed_this_year_cum_gv")
                       )


ggplot(df2, aes(x = Date, y = value, linetype = variable, colour = variable, group = variable)) +
  geom_line() +
  scale_colour_manual(values = rep(c("red", "blue"), each = 2),
                      name = "",
                      labels =  c("Opened last year",
                                  "Closed last year",
                                  "Opened this year",
                                  "Closed this year")) +
  scale_linetype_manual(values = rep(c("solid", "dashed"), 2),
                        name = "",
                        labels =  c("Opened last year",
                                    "Closed last year",
                                    "Opened this year",
                                    "Closed this year"))