ggplot2 plot data: Error in xy.coords 'x' and 'y' lengths differ

Viewed 69

I currently have to learn about Rstudio and I am a bloody beginner in it. My plan was to create multiple plots and merge them together, but I already get an error code I cant solve right at the beginning.

I started like this:

 Data_v_S<- Data_Kinetik[1:4,2]
 Data_v_S$"[NAD+]  0,1867 mmol/L"<- Data_Kinetik[1:4,6]
 Data_v_S$"[NAD+]  0,2667 mmol/L"<- Data_Kinetik[5:8,6]
 Data_v_S$"[NAD+]  0,4667 mmol/L"<- Data_Kinetik[9:12,6]
 Data_v_S$"[NAD+]  1,8667 mmol/L"<- Data_Kinetik[13:16,6]

x <- Data_v_S$`[EtOH] mmol/L`
y1 <- Data_v_S$`[NAD+]  0,1867 mmol/L`

plot(x,y1,col="red",type = "b",ylab = "[DE/min]",xlab = "[EtOH] mmol/L")

but I always get the error:

    Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

My data:

# A tibble: 4 x 5
  `[EtOH] mmol/L` `[NAD+]  0,1867 mmol~ `[NAD+]  0,2667 mmol~ `[NAD+]  0,4667 mmol~ `[NAD+]  1,8667 mmol~
            <dbl>                 <dbl>                 <dbl>                 <dbl>                 <dbl>
1            2                    0.225                 0.242                 0.337                 0.473
2            1                    0.180                 0.210                 0.284                 0.409
3            0.5                  0.138                 0.167                 0.232                 0.347
4            0.25                 0.1                   0.113                 0.162                 0.255

Can anyone tell me what I am doing wrong?

has to be like this excel

1 Answers

I don't get that error with plot(x, y1, ...), but I can reproduce that style of plot using ggplot2. The reason I recommend ggplot2 over base plots here is that while plot(.); lines(.); lines(.); lines(.); will get you what you're trying to do, it can at times be prone to user-error (typos, different glitches in the data, etc), and ggplot2 tends to protect against many of these mistakes.

The first step is to reshape the data, since ggplot2 really benefits from having it in a "long" format. For instance, your data is currently:

Data_v_S
#   [EtOH] mmol/L [NAD+]  0,1867 mmol/L [NAD+]  0,2667 mmol/L [NAD+]  0,4667 mmol/L [NAD+]  1,8667 mmol/L
# 1          2.00                 0.225                 0.242                 0.337                 0.473
# 2          1.00                 0.180                 0.210                 0.284                 0.409
# 3          0.50                 0.138                 0.167                 0.232                 0.347
# 4          0.25                 0.100                 0.113                 0.162                 0.255

and a "long" format of this is something like:

long <- tidyr::pivot_longer(Data_v_S, -`[EtOH] mmol/L`)
# # A tibble: 16 x 3
#    `[EtOH] mmol/L` name                  value
#              <dbl> <chr>                 <dbl>
#  1            2    [NAD+]  0,1867 mmol/L 0.225
#  2            2    [NAD+]  0,2667 mmol/L 0.242
#  3            2    [NAD+]  0,4667 mmol/L 0.337
#  4            2    [NAD+]  1,8667 mmol/L 0.473
#  5            1    [NAD+]  0,1867 mmol/L 0.18 
#  6            1    [NAD+]  0,2667 mmol/L 0.21 
#  7            1    [NAD+]  0,4667 mmol/L 0.284
#  8            1    [NAD+]  1,8667 mmol/L 0.409
#  9            0.5  [NAD+]  0,1867 mmol/L 0.138
# 10            0.5  [NAD+]  0,2667 mmol/L 0.167
# 11            0.5  [NAD+]  0,4667 mmol/L 0.232
# 12            0.5  [NAD+]  1,8667 mmol/L 0.347
# 13            0.25 [NAD+]  0,1867 mmol/L 0.1  
# 14            0.25 [NAD+]  0,2667 mmol/L 0.113
# 15            0.25 [NAD+]  0,4667 mmol/L 0.162
# 16            0.25 [NAD+]  1,8667 mmol/L 0.255

With this, we can do:

library(ggplot2)
ggplot(long, aes(`[EtOH] mmol/L`, value, group = name, color = name)) +
  geom_line() +
  scale_color_discrete(name = NULL) +
  labs(y = expression("Steigung [" * Delta * "E/min]"))

basic ggplot with updated labels and grouped lined


Data:

Data_v_S <- structure(list(`[EtOH] mmol/L` = c(2, 1, 0.5, 0.25), `[NAD+]  0,1867 mmol/L` = c(0.225, 0.18, 0.138, 0.1), `[NAD+]  0,2667 mmol/L` = c(0.242, 0.21, 0.167, 0.113), `[NAD+]  0,4667 mmol/L` = c(0.337, 0.284, 0.232, 0.162), `[NAD+]  1,8667 mmol/L` = c(0.473, 0.409, 0.347, 0.255)), class = "data.frame", row.names = c(NA, -4L))
Related