以下是我的样本df和ggplot2的图。如您所见,bms在图例中出现两次。如何将bms仅保留在图例中,并省略带有填充方块的bms(fyi:bms = nda + nfa)?
df<-structure(list(date = structure(c(16450, 16481, 16509, 16540,
16570, 16601, 16631, 16662, 16693, 16723, 16754, 16784, 16450,
16481, 16509, 16540, 16570, 16601, 16631, 16662, 16693, 16723,
16754, 16784, 16450, 16481, 16509, 16540, 16570, 16601, 16631,
16662, 16693, 16723, 16754, 16784), class = "Date"), growth = c(15.6208780039999,
15.1694006447267, 16.195618338681, 16.0691510104003, 19.00531067356,
20.6393420328606, 19.913212909657, 20.1086270676403, 20.9905103619554,
21.6087938989033, 24.1841921678774, 24.9969801670078, 16.7740140352366,
19.0872278878704, 20.6172095631277, 18.3422520752069, 18.0117650156554,
17.0745150898952, 16.9399643028229, 16.4777161388574, 13.6874693641892,
13.5642463172163, 15.1097777196027, 13.395801323272, 13.8035044502667,
9.32224304536525, 9.50933290655784, 12.5344406713178, 20.5191805086465,
26.2642712998039, 24.7100853142004, 25.7997724265002, 32.7241081094385,
34.9437659017955, 39.1086559617412, 43.4779253836789), contribution_growth = c(15.6208780039999,
15.1694006447267, 16.195618338681, 16.0691510104003, 19.00531067356,
20.6393420328606, 19.913212909657, 20.1086270676403, 20.9905103619554,
21.6087938989033, 24.1841921678774, 24.9969801670078, 10.2624309484927,
11.4292066393698, 12.4103419775945, 11.1633356429028, 10.8747304989152,
10.4510867812205, 10.457861232512, 10.0596987226562, 8.43653450851318,
8.46037929729901, 9.39649462921924, 8.22970711412679, 5.35844705550725,
3.7401940053569, 3.78527636108649, 4.90581536749757, 8.13058017457733,
10.1882552516401, 9.45535167720894, 10.0489283449197, 12.5539758534422,
13.1484146016662, 14.7876975386582, 16.7672730529425), components = c("bms",
"bms", "bms", "bms", "bms", "bms", "bms", "bms", "bms", "bms",
"bms", "bms", "nda", "nda", "nda", "nda", "nda", "nda", "nda",
"nda", "nda", "nda", "nda", "nda", "nfa", "nfa", "nfa", "nfa",
"nfa", "nfa", "nfa", "nfa", "nfa", "nfa", "nfa", "nfa")), .Names = c("date",
"growth", "contribution_growth", "components"), row.names = c(NA,
-36L), class = "data.frame")
df<- date growth contribution_growth components
1 2015-01-15 15.620878 15.620878 bms
2 2015-02-15 15.169401 15.169401 bms
3 2015-03-15 16.195618 16.195618 bms
4 2015-04-15 16.069151 16.069151 bms
5 2015-05-15 19.005311 19.005311 bms
6 2015-06-15 20.639342 20.639342 bms
7 2015-07-15 19.913213 19.913213 bms
8 2015-08-15 20.108627 20.108627 bms
9 2015-09-15 20.990510 20.990510 bms
10 2015-10-15 21.608794 21.608794 bms
11 2015-11-15 24.184192 24.184192 bms
12 2015-12-15 24.996980 24.996980 bms
13 2015-01-15 16.774014 10.262431 nda
14 2015-02-15 19.087228 11.429207 nda
15 2015-03-15 20.617210 12.410342 nda
16 2015-04-15 18.342252 11.163336 nda
代码:
library(ggplot2)
test_plot<-ggplot(
df[df$components!="bms",],
aes(x=date,y=contribution_growth,fill=components))+
geom_bar(stat='identity')+
geom_line(data=df[df$components=="bms",],
aes(x=date,y=growth,color=components))+
scale_x_date(date_breaks = "2 month", date_labels = "%Y %b")+
labs(
title = "Contributions to m2 growth"
) +
theme_bw()+ # black and white theme
theme(
plot.title = element_text(hjust = 0.0, size=10, face="bold", color="blue"),
plot.subtitle = element_text(hjust = 0.0, size=8, color="blue"),
plot.caption = element_text(hjust = 0.0, size=8),
axis.title.x=element_blank(),
axis.text.x=element_text(size=8, angle=90),
axis.text.y=element_text(size=8),
axis.title.y=element_blank(),
legend.title=element_blank(),
legend.key = element_blank(),
legend.text=element_text(size=8),
legend.direction="horizontal",
legend.position="top",
legend.background = element_rect(fill = FALSE),
panel.grid.major = element_line(colour = NA, size = 0.0),
panel.grid.minor = element_line(colour = NA, size = 0.0),
panel.border = element_blank(),
axis.line = element_line(color = 'black')
)