假设我们在单个CSV文件中有2个这样的数据帧:
Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 2 6
#
Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 3 6
我的实际要求是在2个不同的数据帧变量中读取这2个数据帧,我想分析这2个数据帧并相应地绘制,请在这方面帮助我。
答案 0 :(得分:2)
这是一个例子
writeLines(
con = tf <- tempfile(fileext = ".csv"),
text = "Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 2 6
#
Name C1 C2 C3 C4
aa 1 2 3 4
bb 3 4 6 5
cc 10 2 5 6
TT 44 2 3 6")
txt <- readLines(tf)
sep <- grep("^#", txt)[1]
df1 <- read.table(text = txt[1:(sep-1)], header = TRUE)
df1$part <- "1"
df2 <- read.table(text = txt[(sep+1):length(txt)], header = TRUE)
df2$part <- "2"
library(tidyr)
library(dplyr)
library(ggplot2)
bind_rows(df1, df2) %>%
gather(var, val, -part, -Name) %>%
ggplot(aes(x = Name, y = val, fill = var)) +
geom_col() +
facet_wrap(~part)