简体   繁体   English

构面的x轴间隔标签

[英]x-axis interval labeling for facets

I have a data frame: 我有一个数据框:

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")

I am trying to plot the stacked area, however, I encountered a problem in labeling x-axis. 我试图绘制堆叠区域,但是在标记x轴时遇到问题。 The figure is as shown below: 如下图所示:

在此处输入图片说明

For all the facets: Starting year is 1993 and end is 2016; 对于所有方面:起始年份为1993年,结束年份为2016年; The year is in numeric format. 年份为数字格式。

I want to show all the year range(1993 to 2016) in the x-axis, despite having the blank data in facet C and D. 我想在x轴上显示所有年份范围(1993年至2016年),尽管在方面C和D中都有空白数据。

I used the following as suggested in the forum: 我按照论坛中的建议使用了以下内容:

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

However, this does not seem to work. 但是,这似乎不起作用。 How can this be resolved? 如何解决?

You didn't provide enough data to recreate your graph, but here's my answer 您没有提供足够的数据来重新创建图形,但这是我的答案

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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM