ggplot2 stacked area line charts producing odd lines and holes

限于喜欢 提交于 2019-12-24 00:56:49

问题


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?


回答1:


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

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)


来源:https://stackoverflow.com/questions/34916995/ggplot2-stacked-area-line-charts-producing-odd-lines-and-holes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!