3

I've been trying to produce a stacked area chart with a discrete x variable (because I want to display financial year, i.e. "2013/14", rather than calendar year). However, turning the x-axis variable into a factor prevents the geom from being rendered in the final chart.

Is there a way around this?

library(ggplot2)

dat <- structure(list(year = c(13, 13, 14, 14, 15, 15), 
                      group_lvl = structure(c(1L, 2L, 1L, 2L, 1L, 2L), 
                                            .Label = c("a", "b"), class = "factor"), 
                      val = c(35, 65, 50, 50, 75, 25)), 
                 .Names = c("year", "group_lvl", "val"), row.names = c(NA, -6L), 
                 class = "data.frame")
dat
  year group_lvl val
1   13         a  35
2   13         b  65
3   14         a  50
4   14         b  50
5   15         a  75
6   15         b  25

ggplot(dat, aes(x = year, y = val)) + 
  geom_area(aes(fill = group_lvl), position = "stack")

enter image description here

dat$year <- factor(dat$year)

ggplot(dat, aes(x = year, y = val)) + 
  geom_area(aes(fill = group_lvl), position = "stack")

enter image description here

sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.1.0

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6 scales_0.4.0     plyr_1.8.3       tools_3.3.0     
 [6] gtable_0.2.0     Rcpp_0.12.4      grid_3.3.0       digest_0.6.9     munsell_0.4.3   
3
  • How does an area chart make sense for categorical data? If you don't like to see decimals, you may be better off controlling axis with scale_x_continuous perhaps? Commented May 5, 2016 at 14:49
  • @Gopola, seems quite reasonable to me. As mentioned above I need to display time on the x-axis as financial year. Another example might be year-quarter (2013q3, 2013q4, 2014q1), where a continuous scale doesn't make much sense. In any case, factors are stored as labelled integers and so I would expect the area chart to plot the x axis without problem. Commented May 5, 2016 at 15:03
  • Area charts show trends on continuous variables. So, you need them in a type that can be represented on a continuous scale. Adjusting scale is a different issue, which is what I suggested and the answer below uses that approach. Commented May 5, 2016 at 15:08

1 Answer 1

7

Just add breaks, no need to make it a factor.

ggplot(dat, aes(x = year, y = val)) + 
  geom_area(aes(fill = group_lvl), position = "stack") + 
  scale_x_continuous(breaks=c(13,14,15),labels=c("2013","2014","2015"))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.