0

我有一个看起来像这样的数据框:

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 的原因。

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

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

编辑:更改为“日期”类

4

2 回答 2

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))
于 2013-11-06T15:37:12.623 回答
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"))
于 2013-11-06T17:05:54.297 回答