堆积面积图在上一个元素的顶部

时间:2018-09-08 17:19:39

标签: r ggplot2

以下代码确实显示了堆积面积图,其中所示的两个元素似乎彼此重叠,在y中不重叠。

library(ggplot2)
library(lubridate)
theme_set(theme_bw())

df <- economics[, c("date", "psavert", "uempmed")]
df <- df[lubridate::year(df$date) %in% c(1967:1981), ]

# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]
lbls <- lubridate::year(brks)

# plot
ggplot(df, aes(x=date)) + 
geom_area(aes(y=psavert+uempmed, fill="psavert")) + 
geom_area(aes(y=uempmed, fill="uempmed")) + 
labs(title="Area Chart of Returns Percentage", 
   subtitle="From Wide Data format", 
   caption="Source: Economics", 
   y="Returns %") +  # title and caption

scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
scale_fill_manual(name="", 
                values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
theme(panel.grid.minor = element_blank())  # turn off minor grid

enter image description here

但是,此代码需要事先知道用于填充的不同元素,在这种情况下为psavert和uempmed。有没有办法显示在y上没有碰撞的情况下动态堆叠的动态元素组?

1 个答案:

答案 0 :(得分:1)

ggplot适用于长格式数据。我们可以使用tidyr::gather将数据转换为长格式,然后简化绘图,并且可以在不更改绘图代码的情况下任意选择多个变量:

df_long = tidyr::gather(df, "var", "value", -date)
head(df_long)
# # A tibble: 6 x 3
#   date       var     value
#   <date>     <chr>   <dbl>
# 1 1967-07-01 psavert  12.5
# 2 1967-08-01 psavert  12.5
# 3 1967-09-01 psavert  11.7
# 4 1967-10-01 psavert  12.5
# 5 1967-11-01 psavert  12.5
# 6 1967-12-01 psavert  12.1

请注意,我们现在有一个用于y值的列和一个单独的键列var,用于区分不同的变量。这意味着当我们在下面绘制时,我们只需要一个geom_area层。如果var列中有两个以上的变量,则也可以很好地绘制它们。结果图是相同的。

# plot
ggplot(df_long, aes(x = date)) +
  geom_area(aes(y = value, fill = var)) +
  labs(
    title = "Area Chart of Returns Percentage",
    subtitle = "From Wide Data format",
    caption = "Source: Economics",
    y = "Returns %"
  ) + 
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_fill_manual(name="", 
                values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
  theme(panel.grid.minor = element_blank())  # turn off minor grid

作为旁注,我建议使用代码注释来(a)解释为什么做事,以及(b)记录您做事的任何不寻常或棘手的方式。我认为您会发现更明显的评论,例如# title and caption 都没用代码行title = "Area Chart of Returns Percentage"本身很清晰。