[英]ggplot2 one plot with two graphs from CSV in R
I'm trying to read a CSV: 我正在尝试读取CSV:
5.0;72.0;
6.0;72.0;
4.0;72.0;
5.0;72.0;
4.0;72.0;
...and have one ggplot2
with two functions: first column and second column, both colored differently. ...并且具有一个具有两个功能的
ggplot2
:第一列和第二列,两者的颜色不同。
What I've tried so far: 到目前为止,我已经尝试过:
>dt <- fread('C:\\Users\\csvFile.txt')
>print(dt)
V1 V2 V3
1: 5 72 NA
2: 6 72 NA
3: 4 72 NA
4: 5 72 NA
5: 4 72 NA
...and now I'm stuck. ...现在我被困住了。 How do I plot both V1 and V2 in the same plot?
如何在同一图中绘制V1和V2?
I know how to make the plot colored and continuous, but I have no idea how to actually plot the values: 我知道如何使图变色和连续,但是我不知道如何实际绘制值:
>ggplot(dt, aes(x=x, y=y)) + geom_line() +geom_area(fill="blue")
My desired chart would look something like this (except that i have no X-axis ("density") as my values are a simple timeseries): 我想要的图表看起来像这样(除了我没有X轴(“密度”),因为我的值是一个简单的时间序列):
I would do something like this... 我会做这样的事情...
library(tidyr) #for the gather
df$time = seq_along(df$V1) #add your time variable
df2 <- df %>% gather(key=type,value=value,-time) #convert to long format
ggplot(df2,aes(x=time,y=value,fill=type))+
geom_area(alpha=0.2,position="identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.