简体   繁体   中英

ggplot2 one plot with two graphs from CSV in R

I'm trying to read a 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.

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?

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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