我正在使用geom_area
绘制堆积面积图,其中有两类:年份和类型(EV和汽油)。
是否可以在此图表上显示类型的标签(而不是年份)?现在我正在使用geom_text
,但是我想使用geom_label
,因为文本的位置针对不同的图形而变化。
ggplot(vy, aes(x = y, y = number/1000, color=Type, fill=as.factor(desc(v)))) +
geom_area(stat="identity", linetype = 1, size =0.5 ) +
geom_text(data=vy, aes(x=2045, y=1250, label="EV"),size=7, color="black") +
geom_text(data=vy, aes(x=2030, y=500, label="Gasoline"),size=7, color="black") +
labs(x="Year", y="") +
scale_x_continuous(breaks = seq(1980, 2050, by = 5)) +
theme(axis.title.y = element_text(angle=0)) +
guides(fill=guide_legend(title="Vintages")) +
guides(colour=FALSE) +
geom_label(aes(label=Type))
不起作用。
以下是数据示例:
structure(list(y = c(2028L, 2029L, 2030L, 2031L, 2032L, 2028L,
2029L, 2030L, 2031L, 2032L, 2028L, 2029L, 2030L, 2031L, 2032L,
2028L, 2029L, 2030L, 2031L, 2032L, 2028L, 2029L, 2030L, 2031L,
2032L, 2028L, 2029L, 2030L, 2031L, 2032L, 2028L, 2029L, 2030L,
2031L, 2032L, 2028L, 2029L, 2030L, 2031L, 2032L, 2028L, 2029L,
2030L, 2031L, 2032L, 2028L, 2029L, 2030L, 2031L, 2032L), v = c(2028,
2028, 2028, 2028, 2028, 2029, 2029, 2029, 2029, 2029, 2030, 2030,
2030, 2030, 2030, 2031, 2031, 2031, 2031, 2031, 2032, 2032, 2032,
2032, 2032, 2028, 2028, 2028, 2028, 2028, 2029, 2029, 2029, 2029,
2029, 2030, 2030, 2030, 2030, 2030, 2031, 2031, 2031, 2031, 2031,
2032, 2032, 2032, 2032, 2032), Type = c("EV", "EV", "EV", "EV",
"EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV",
"EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "EV", "Gasoline",
"Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline",
"Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline",
"Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline",
"Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline", "Gasoline"
), number = c(5662.86208292447, 5662.8580974621, 5662.82422103189,
5662.63225459408, 5661.81639723339, 0, 6382.30437283546, 6382.29988103722,
6382.26170075215, 6382.04534580345, 0, 0, 7136.91197086026, 7136.90694797718,
7136.86425347099, 0, 0, 0, 7917.48836500915, 7917.48279276467,
0, 0, 0, 0, 8713.89605603947, 66590.8856270952, 66590.8387611349,
66590.4404004728, 66588.1830233873, 66578.5891707738, 0, 67068.2729811654,
67068.225779225, 67067.8245627314, 67065.5510026013, 0, 0, 67510.4376156433,
67510.3901025121, 67509.9862408969, 0, 0, 0, 67926.6087712698,
67926.5609652417, 0, 0, 0, 0, 68326.9380182512)), row.names = c(NA,
-50L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "y", drop = TRUE, indices = list(
c(0L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L), c(1L,
6L, 11L, 16L, 21L, 26L, 31L, 36L, 41L, 46L), c(2L, 7L, 12L,
17L, 22L, 27L, 32L, 37L, 42L, 47L), c(3L, 8L, 13L, 18L, 23L,
28L, 33L, 38L, 43L, 48L), c(4L, 9L, 14L, 19L, 24L, 29L, 34L,
39L, 44L, 49L)), group_sizes = c(10L, 10L, 10L, 10L, 10L), biggest_group_size = 10L, labels = structure(list(
y = 2028:2032), row.names = c(NA, -5L), class = "data.frame", vars = "y", drop = TRUE))
对于此较小的数据样本,代码为:
ggplot(vy2, aes(x = y, y = number/1000, color=Type, fill=as.factor(desc(v)))) +
geom_area(stat="identity", linetype = 1, size =0.5 ) +
geom_text(data=vy, aes(x=2031, y=290, label="EV"),size=5, color="black") +
geom_text(data=vy, aes(x=2031, y=100, label="Gasoline"),size=7, color="black") +
labs(x="Year", y="") +
scale_x_continuous(breaks = seq(1980, 2050, by = 5)) +
theme(axis.title.y = element_text(angle=0)) +
guides(fill=guide_legend(title="Vintages")) +
guides(colour=FALSE) +