user14050624
user14050624

Reputation:

How Can I create a graph using this samples?

I'm a complete novice in R and I've been trying to create a graph in R without any excel data. I only have these:

SampleA=c(963.25, 960.09, 971.81, 954.87, 967.2, 974.03, 948.34, 928.64, 943.09,
967.35, 928.93, 955.64, 973.28, 964.59, 928.48, 957.1)

SampleB=c(1975.56, 1961.6, 1924.5, 1926.01, 1932.9, 1926.17, 1964.78, 1927.42, 1934.92,
1954.09, 1944.11, 1934.97, 1937.15, 1939.11, 1935.36, 1961.17)

how can I use a pivot_longer to make a columns for it? And What kind of graph should I use in order to see the difference between these two sample.

I tried doing these:

df <- data.frame (
  Sample = "Sample A", "Sample B",
  Days = SampleA, SampleB
)
pivot_longer(df, Sample = "SampleA", "SampleB", Days = Sample)

but I get this error

Error: 2 components of `...` were not used. We detected these problematic arguments: * `Sample` * `Days` Did you misspecify an argument?

Upvotes: 0

Views: 55

Answers (2)

Duck
Duck

Reputation: 39613

Another way to test if values are different reckons to statistical measures. You can use box plots in order to check if the samples are different or equal. Or maybe a t-test as your variable is continuous with two groups. Here a code for box plots:

library(ggplot2)
#Data
ggplot(data=df,aes(x=Sample,y=Days,fill=Sample))+
  geom_point()+
  geom_boxplot()+
  facet_wrap(.~Sample,scales = 'free')

Output:

enter image description here

Based on our box plots and the mean (middle bar in each box) it looks like values across samples are different. We can confirm that with a t-test:

#Test
t.test(df$Days[df$Sample=='Sample A'],df$Days[df$Sample=='Sample B'])$p.value

Output:

[1] 9.124026e-47

As our p-value is extremely low we can support the hypothesis that values across samples are different.

Some data used:

#Data
df <- structure(list(Sample = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Sample A", "Sample B"
), class = "factor"), Days = c(963.25, 960.09, 971.81, 954.87, 
967.2, 974.03, 948.34, 928.64, 943.09, 967.35, 928.93, 955.64, 
973.28, 964.59, 928.48, 957.1, 1975.56, 1961.6, 1924.5, 1926.01, 
1932.9, 1926.17, 1964.78, 1927.42, 1934.92, 1954.09, 1944.11, 
1934.97, 1937.15, 1939.11, 1935.36, 1961.17)), class = "data.frame", row.names = c(NA, 
-32L))

Upvotes: 1

akrun
akrun

Reputation: 887771

After create the 'df', we can use ggplot with 'Days' on the y axis and the row sequence on the x axis with color as 'Sample'

library(ggplot2)
library(dplyr)
df %>% 
   group_by(Sample) %>% 
   mutate(rn = row_number()) %>%
   ggplot(., aes(x = rn, y = Days, color = Sample)) + 
      geom_line()

data

df <- data.frame(Sample = rep(c("Sample A", "Sample B"), 
      c(length(SampleA), length(SampleB))), Days = c(SampleA, SampleB))

Upvotes: 0

Related Questions