简体   繁体   English

在R中具有2个单独的列值的ggplot线图

[英]Plot ggplot line graph having 2 seperate columns value in R

I have a dataset as follows- 我有一个数据集如下-

    mo  Deposit Withdrawl
1   01  430013.5    363620
2   02  399733.5    364426
3   03  782495.0    897652
4   04  349144.7    395913
5   05  470808.5    410153
6   06  568985.0    720753
7   07  676970.2    355392

Now I want to plot a line graph which will show based on each mo (x-axis) , how is Deposit and Withdrawl working , precisely the graph will have mo as X axis and expecting 2 lines as Deposit and Withdrawl showing a line graph connecting to there points with different color in ggplot or qplot. 现在,我想绘制一个折线图,该折线图将基于每个mo(x轴)显示,存款和取款如何工作,精确地该图将mo作为X轴,并期望有2条线作为Deposit和Withdrawl来显示折线图到ggplot或qplot中具有不同颜色的点。

Tried 试过了

ggplot(data=month.dep,
       aes(x=mo, y=y, colour=Deposit)) +
  geom_line()

But above won't work as my objective is different and my colour type is no some binary value 但是上面的方法不起作用,因为我的目标是不同的,并且我的颜色类型没有任何二进制值

First melt your data into long format then plot it: 首先melt你的数据为长格式,然后绘制它:

library(reshape2)   # For melt function

month.dep.m = melt(month.dep, id.var="mo")

ggplot(month.dep.m, aes(x=mo, y=value, colour=variable)) +
       geom_line()

In general, ggplot2 prefers data in long format. 通常, ggplot2喜欢长格式的数据。 In this case, Deposit and Withdrawal are two categories of currency values. 在这种情况下, DepositWithdrawal是两种货币值。 So we use melt to put those two categories into a single column called variable that ggplot uses for the colour aesthetic, while the currency values likewise go into a new value column, each value going with its corresponding category in the variable column. 因此,我们使用melt将这两个类别放入一个单独的列,该variable称为ggplot用于色彩美学的variable ,而货币值同样进入一个新的value列,每个值都与variable列中的相应类别一起。

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

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