2

I have a data set that's structured as follows:

year    color   toyota  honda   ford
2011    blue    66      75      13
2011    red     75      91      62
2011    green   65      26      57
2012    blue    64      23      10
2012    red     84      8       62
2012    green   67      21      62
2013    blue    31      74      49
2013    red     48      43      35
2013    green   57      62      74
2014    blue    59     100      32
2014    red     72      47      67
2014    green   97      24      70
2015    blue    31       0      79
2015    red     60      35      74
2015    green   51       2      28

(My actual data, presented in the chart images below, is much larger and has 100s of "colors" but I'm simplifying here so you can merely understand the structure.)

I am trying to make a stacked area line chart that shows how many cars of each color are produced over time for a specific company. (i.e. each company has its own chart in which x axis = years, y axis = cars produced).

I run this code:

qplot(year, toyota, data = dataName, fill = color, group = color, geom= "area", position = "stack") 
+ geom_area() + theme(legend.position = "none")

However, every company's chart has issues. There are seemingly random cut-out holes as well as lines that cut across the top of the layers.

company1_chart

company2_chart

I'm confused why this is happening or even possible (especially the holes... won't the data stack down?) Would it help if I made the companies long rather than wide in the data structure?

Jim
  • 715
  • 2
  • 13
  • 26
  • 1
    Did you check, if your data has gaps for certain years and colors? E.g. no blue for 1983 etc.? – Buggy Jan 21 '16 at 07:27
  • My data does have a lot of gaps. Would that be an issue? If so, I can make sure there are rows for the same set of 200 colors for each year. – Jim Jan 21 '16 at 08:16
  • Not entirely sure, but I would guess that the empty spaces are happening where there's no value... What should ggplot2 plot or display if there's no value? – Buggy Jan 21 '16 at 08:50
  • Giving us data to understand the structure is not enough. The problem could for instance be connected to the data type of the columns (e. g. factor vs. numeric), and we can not see that from the table you posted. Also, you should make sure that the data that you post does actually lead to a plot that has the holes that you want to get rid of. So you should produce a data set that is as small as possible, but still shows the problem that you want to solve. Then, run `dput(dataName)` and post the output. – Stibu Jan 21 '16 at 09:34
  • I think @user3293236 is correct about the gaps. If you remove the first row from the test dataset when plotting you can see the gap. If you set it to 0, you don't have the problem. – aosmith Jan 22 '16 at 17:55

1 Answers1

0

Even with 0 values, you should not have those errors. I took your data and added 0's in the honda column sporadically.

The code (using ggplot2)

library(ggplot2)
df <- read.csv("cartest.csv", header = TRUE)

ggplot(data=df,aes(x=year,y=h,fill=color)) + 
  geom_area() + 
  ggtitle("car test")

cartest.png

If you are importing your data as a CSV or TSV and your data columns are numeric you should not have this issue. If it was imported as .character you can convert using:

df$h <- as.numeric(df$h)
abonn
  • 61
  • 1
  • 6
  • Hmm, so my "toyota" variable is numeric, "year" variable is integer, and "color" variable is factor. I typed in the ggplot code exactly as you entered it (swapping in the actual variables I have in my data set), but still got essentially the same odd hole-filled and random lines across the top chart as I did with the qplot in my initial post. – Jim Jan 21 '16 at 15:38
  • @Jim as aosmith already confirmed above, the issue is that the your dataframe has gaps, i.e. `NA` values after the internal data transformation. You can probably alter that by using `na.omit` in your code. Try this snippet: ggplot(data=na.omit(df),aes(x=year,y=h,fill=color)) + geom_area() + ggtitle("car test") Not entirely sure if that will work but give it a spin... – Buggy Jan 26 '16 at 07:19