小平面的x轴间隔标记

时间:2018-05-22 15:23:12

标签: r ggplot2

我有一个数据框:

dput(head(df))
structure(list(Year = c(1993, 1994, 1995, 1996, 1997, 1998), 
type = c("PS", "PS", "PS", "PS", "PS", "PS"), measure = c("A", 
"A", "A", "A", "A", "A"), value = c(19034.33, 9550.89, 12934.76, 
10779.04, 7433.43, 7955.47)), .Names = c("Year", "type", 
"measure", "value"), row.names = c(NA, 6L), class = "data.frame")

我正在尝试绘制堆积区域,但是,我在标记x轴时遇到了问题。 该图如下所示:

enter image description here

对于所有方面:开始年份是1993年,结束是2016年;年份是数字格式。

我想在x轴上显示所有年份范围(1993年至2016年),尽管在方面C和D中有空白数据。

我在论坛中建议使用以下内容:

ggplot() + geom_area() + facet_wrap(~ measure, scales = "free_x")

但是,这似乎不起作用。怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

您没有提供足够的数据来重新创建图表,但这是我的答案

df <- structure(
  list(
    Year = c(1993, 1994, 1995, 1996, 1997, 1998),
    type = c("PS", "PS", "PS", "PS", "PS", "PS"),
    measure = c("A",
                "A", "A", "A", "A", "A"),
    value = c(19034.33, 9550.89, 12934.76,
              10779.04, 7433.43, 7955.47)
  ),
  .Names = c("Year", "type",
             "measure", "value"),
  row.names = c(NA, 6L),
  class = "data.frame"
)


library(ggplot2)


ggplot(df, aes(x = Year, y = value, fill = type)) +
  geom_area() +
  facet_wrap(~ measure) + 
  scale_x_continuous(breaks = 1993:2016, limits = c(1993, 2016))

enter image description here