ggplot2堆积区线图生成奇数线和孔 [英] ggplot2 stacked area line charts producing odd lines and holes

查看:161
本文介绍了ggplot2堆积区线图生成奇数线和孔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,结构如下:

 年彩色丰田本田福特
2011蓝色66 75 13
2011红色75 91 62
2011绿色65 26 57
2012蓝色64 23 10
2012红色84 8 62
2012绿色67 21 62
2013蓝色31 74 49
2013红色48 43 35
2013绿色57 62 74
2014蓝色59 100 32
2014红色72 47 67
2014绿色97 24 70
2015蓝色31 0 79
2015红色60 35 74
2015绿色51 2 28

(我的实际数据显示在下面的图表中,比较大,有100多种颜色,但我在这里进行了简化,因此您只能理解结构。)

我正在尝试制作一个显示的堆积区线图一个特定的公司随着时间的推移生产出每种颜色的汽车数量。 (即每个公司都有自己的图表,其中X轴=年,Y轴=汽车生产)。

我运行这段代码:

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

然而,每家公司的图表都有的问题。有看似随机的切割孔以及切割图层顶部的线条。



如果您要将数据导入为CSV或TSV,你的数据列是数字你不应该有这个问题。如果以.character格式导入,您可以使用以下格式转换:

  df $ h < -  as.numeric(df $ h) 


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?

解决方案

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)

这篇关于ggplot2堆积区线图生成奇数线和孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆