[英]how to extract min and max rows of data frame and draw multiple graph in a lay out using R
I want to extract data row associated with min and max from 4 data set.我想从 4 个数据集中提取与最小值和最大值相关的数据行。
I already coded to check result of min and max using data set.我已经编码以使用数据集检查最小值和最大值的结果。
#Question #问题
I want to show result to extract min and max of X1.frac, X1.frac1 and X1.frac2 rows per each time(945, 955, 965, 975)我想显示每次提取 X1.frac、X1.frac1 和 X1.frac2 行的最小值和最大值的结果(945、955、965、975)
##expected result## ##预期结果##
time![]() |
frac![]() |
time![]() |
Value![]() |
---|---|---|---|
945 ![]() |
0.904265 ![]() |
945 ![]() |
Min![]() |
955 ![]() |
0.919962 ![]() |
955 ![]() |
Min![]() |
965 ![]() |
0.920854 ![]() |
965 ![]() |
Min![]() |
975 ![]() |
0.925369 ![]() |
975 ![]() |
Min![]() |
time![]() |
frac![]() |
time![]() |
Value![]() |
---|---|---|---|
945 ![]() |
0.965208 ![]() |
945 ![]() |
Min![]() |
955 ![]() |
0.995463 ![]() |
955 ![]() |
Min![]() |
965 ![]() |
0.982396 ![]() |
965 ![]() |
Min![]() |
975 ![]() |
0.973242 ![]() |
975 ![]() |
Min![]() |
I want show result for df_cv_min, df_cv_mean, df_cv_max using one graph.我想使用一张图显示 df_cv_min、df_cv_mean、df_cv_max 的结果。
### my code library(tidyverse) library(ggplot2) library(reshape2) library(cowplot) df=read.csv("D:/fracdata_test.csv",header = T) ##---------------------------time interval (10) int=df %>% select(X1.time.1) %>% slice(2) %>% as.numeric()-df %>% select(X1.time) %>% slice(1) %>% as.numeric() ##---------------------------max time value (10935) time_max=df %>% select(X1.time.1) %>% as.matrix() %>% max() ##---------------------------min time value (945) time_min=df %>% select(X1.time.1) %>% as.matrix() %>% min() ##---------------------------number_time_point (1000) ntp=(time_max-time_min)/int+1 ##---------------------------number_measurement n_meas=df %>% nrow()/ntp ##---------------------------Measurement_Encoding Meas=gl(n_meas,ntp) ##--------------------------Add Measurement_Encoding into the main dataset df=cbind(df,Meas) %>% mutate(Meas=factor(Meas)) ##---------------------------Plotting y_min=df %>% select(X1.frac.1) %>% as.matrix() %>% min() y_max=df %>% select(X1.frac.1) %>% as.matrix() %>% max() df_cv=df %>% dcast(Meas~time, value.var="X1.frac.1") %>% select(-Meas) ##------------time time=seq(time_min, time_max, int) ##------------Mean data df_cv_mean=df_cv %>% apply(.,2,function(x)mean(x)) %>% data.frame() %>% set_names("frac") %>% mutate(time=time, Value="Mean") ##------------Min data df_cv_min=df_cv %>% apply(.,2,function(x)min(x)) %>% data.frame() %>% set_names("frac") %>% mutate(time=time, Value="Min") ##------------Max data df_cv_max=df_cv %>% apply(.,2,function(x)max(x)) %>% data.frame() %>% set_names("frac") %>% mutate(time=time, Value="Max") ##-----------Merging three data df_3data=rbind(df_cv_mean, df_cv_min, df_cv_max) %>% mutate(Value=factor(Value)) ##-----------Plotting df_3data Plot_B=df_3data %>% ggplot(.,aes(x=time, y=frac))+geom_line(aes(group=Value, color=Value))+ geom_smooth(method="auto",aes(group=Value,color=Value))+ scale_colour_manual(values = c("red","blue","orange"))+ scale_x_continuous(breaks=0:8000*1000,limits=c(time_min,time_max)) + scale_y_continuous(breaks=0:8000*0.1,limits=c(y_min,y_max)) + theme_classic()+labs(subtitle = "B. Original data (mean, min, max values)")
#Data set(test.csv) #数据集(test.csv)
X1.time ![]() |
X1.frac ![]() |
X1.time.1 ![]() |
X1.frac.1 ![]() |
X1.time.2 ![]() |
X1.frac.2 ![]() |
---|---|---|---|---|---|
945 ![]() |
0.937752593 ![]() |
945 ![]() |
0.965208348 ![]() |
945 ![]() |
0.904265228 ![]() |
955 ![]() |
0.959463167 ![]() |
955 ![]() |
0.954415107 ![]() |
955 ![]() |
0.919962471 ![]() |
965 ![]() |
0.982386049 ![]() |
965 ![]() |
0.959723958 ![]() |
965 ![]() |
0.920854173 ![]() |
975 ![]() |
0.973241841 ![]() |
975 ![]() |
0.925369792 ![]() |
975 ![]() |
0.928773106 ![]() |
Get the data in long format for 'frac'
columns and get min
and max
value for every X1.time
value.获取
'frac'
列的长格式数据,并获取每个X1.time
值的min
和max
。
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = contains('frac')) %>%
group_by(X1.time) %>%
summarise(min_value = min(value),
max_value = max(value))
# X1.time min_value max_value
# <dbl> <dbl> <dbl>
#1 945 0.904 0.965
#2 955 0.920 0.959
#3 965 0.921 0.982
#4 975 0.925 0.973
Another option is to use rowwise
:另一种选择是使用
rowwise
:
df %>%
rowwise() %>%
transmute(X1.time,
min_value = min(c_across(contains('frac'))),
max_value = max(c_across(contains('frac')))) %>%
ungroup
data数据
It is helpful if you share data in a reproducble format which is easier to copy.如果您以更易于复制的可复制格式共享数据,这将很有帮助。
df <- structure(list(X1.time = c(945, 955, 965, 975), X1.frac = c(0.937752593,
0.959463167, 0.982386049, 0.973241841), X1.time.1 = c(945, 955,
965, 975), X1.frac.1 = c(0.965208348, 0.954415107, 0.959723958,
0.925369792), X1.time.2 = c(945, 955, 965, 975), X1.frac.2 = c(0.904265228,
0.919962471, 0.920854173, 0.928773106)), row.names = c(NA, -4L),
class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.