How to plot linear model of a named numeric vector with ggplot2

I would like to plot a linear model by ggplot 2 from a vector. I want slope values on y-axis but it doesn't come out

I tried these but it's get an error messages:

ggplot(coefs, aes(x=log(area),y=log(x))) + geom_point()
Error: data must be a data frame, or other object coercible by fortify(), not a numeric vector
Run rlang::last_error() to see where the error occurred.

ggplot(mapping = aes(coefs)) + geom_point()
Error: geom_point requires the following missing aesthetics: y
Run rlang::last_error() to see where the error occurred.

Here is my data frame:

coefs
X1.log(area) X2.log(area) X3.log(area) X4.log(area) X5.log(area) X6.log(area) X7.log(area)
0.016756194 0.023173977 0.026348215 0.099933066 -0.001718118 0.054166769 0.102037502
X8.log(area) X9.log(area) X10.log(area) X11.log(area) X12.log(area) X13.log(area) X14.log(area)
0.009437868 0.003971373 0.107614826 0.053666809 0.111657974 0.024613063 0.042970449
X15.log(area) X16.log(area) X17.log(area) X18.log(area) X19.log(area) X20.log(area)
0.021714419 0.045949784 0.019950681 0.068792533 0.018291901 0.071565651

Thanks so much for your help!

To use ggplot, you have to store coefs in a data.frame. I am not sure what you want to plot on the x axis. One possible plot is shown below.

library(tibble)
library(ggplot2)
library(stringr)
library(dplyr, warn.conflicts = FALSE)
#recreate the coefs Vector
coefs <- c(0.016756194, 0.023173977, 0.026348215, 0.099933066, -0.001718118, 0.054166769, 0.102037502,
           0.009437868, 0.003971373, 0.107614826, 0.053666809, 0.111657974, 0.024613063, 0.042970449,
           0.021714419, 0.045949784, 0.019950681, 0.068792533, 0.018291901, 0.071565651)
names(coefs) <- c("X1.log(area)", "X2.log(area)", "X3.log(area)", "X4.log(area)", "X5.log(area)", "X6.log(area)", "X7.log(area)",
                  "X8.log(area)", "X9.log(area)", "X10.log(area)", "X11.log(area)", "X12.log(area)", "X13.log(area)", "X14.log(area)",
                  "X15.log(area)", "X16.log(area)", "X17.log(area)", "X18.log(area)", "X19.log(area)", "X20.log(area)")

DF <- tibble(AREA = names(coefs), COEFS = coefs)
head(DF)
#> # A tibble: 6 x 2
#>   AREA            COEFS
#>   <chr>           <dbl>
#> 1 X1.log(area)  0.0168 
#> 2 X2.log(area)  0.0232 
#> 3 X3.log(area)  0.0263 
#> 4 X4.log(area)  0.0999 
#> 5 X5.log(area) -0.00172
#> 6 X6.log(area)  0.0542
#mutate the AREA column so the x axis text is not too long
DF %>% mutate(ShortArea = str_extract(AREA, "X\\d{1,2}"),
              ShortArea = factor(ShortArea, levels = str_c("X", 1:20))) %>% 
ggplot(aes(ShortArea, COEFS)) + geom_point()

Created on 2020-08-24 by the reprex package (v0.3.0)

1 Like

Thank you for your guidance @

May I bother you again? @FJCC I have another data frame that calculated from OTUs data. I calculated OTUs removed and slope but they are in different data frames.
And I would like to plot them in one graph by represented OTUs removed on the x-axis (I think it will show them from mean values) and slope on the y-axis. Can I define (xlim = c(0, 400), ylim = c(-0.1, 0.8)) ?

Here is my OTUs removed data frame in dput format:

> dput(tar.un_sap.out10)
structure(list(X1 = c(0, 425, 157, 348, 391, 301, 395, 319, 299, 
316, 305, 283, 291, 338, 233, 350, 233, 214, 384, 334, 358, 296, 
315, 372, 300, 381, 353, 235, 270, 321, 275, 338, 349, 274, 261, 
315, 344, 318, 333, 280, 311, 319, 274, 311, 313, 300, 304, 302, 
296, 320), X2 = c(0, 376, 60, 286, 218, 252, 276, 194, 367, 348, 
281, 386, 276, 324, 324, 398, 215, 287, 239, 285, 355, 328, 221, 
339, 338, 314, 322, 301, 344, 367, 318, 336, 262, 274, 300, 314, 
318, 342, 328, 323, 281, 292, 261, 311, 314, 303, 304, 302, 297, 
320), X3 = c(0, 282, 361, 314, 179, 325, 326, 315, 339, 402, 
170, 257, 396, 318, 348, 366, 325, 366, 285, 256, 392, 296, 330, 
339, 346, 307, 303, 238, 268, 256, 275, 309, 262, 291, 267, 310, 
301, 346, 280, 283, 303, 286, 311, 309, 305, 300, 304, 302, 299, 
320), X4 = c(0, 327, 238, 383, 200, 202, 413, 387, 376, 376, 
316, 281, 352, 363, 360, 226, 346, 359, 384, 307, 356, 356, 278, 
331, 271, 234, 370, 380, 377, 298, 280, 321, 307, 285, 350, 261, 
295, 330, 294, 334, 319, 312, 315, 311, 307, 306, 300, 308, 300, 
320), X5 = c(0, 267, 397, 172, 306, 258, 334, 88, 229, 267, 350, 
230, 389, 367, 325, 367, 212, 396, 323, 350, 313, 349, 250, 332, 
313, 339, 306, 351, 309, 250, 319, 284, 284, 301, 284, 308, 329, 
298, 326, 280, 268, 258, 314, 312, 309, 302, 302, 302, 295, 320
), X6 = c(0, 35, 286, 85, 156, 236, 321, 397, 299, 357, 294, 
257, 414, 249, 386, 243, 360, 309, 351, 326, 230, 302, 316, 330, 
271, 343, 334, 305, 290, 308, 296, 266, 277, 274, 350, 326, 302, 
328, 337, 328, 303, 296, 311, 318, 308, 302, 304, 303, 298, 320
), X7 = c(0, 352, 305, 377, 376, 305, 360, 232, 271, 325, 331, 
403, 314, 342, 353, 336, 285, 371, 324, 375, 297, 305, 340, 385, 
382, 339, 319, 298, 301, 296, 269, 254, 309, 285, 296, 310, 348, 
330, 282, 332, 318, 267, 319, 311, 307, 298, 303, 304, 296, 320
), X8 = c(0, 331, 407, 109, 365, 384, 252, 258, 244, 339, 379, 
292, 338, 364, 361, 274, 269, 333, 338, 305, 319, 313, 280, 239, 
323, 341, 305, 305, 260, 359, 349, 274, 316, 304, 340, 279, 291, 
282, 336, 292, 315, 309, 296, 306, 297, 298, 312, 302, 296, 320
), X9 = c(0, 301, 253, 169, 390, 316, 144, 222, 299, 322, 319, 
358, 262, 303, 359, 364, 339, 279, 272, 328, 345, 229, 292, 356, 
328, 336, 282, 280, 324, 312, 313, 265, 274, 272, 300, 348, 304, 
324, 325, 322, 303, 308, 267, 312, 308, 304, 304, 299, 296, 320
), X10 = c(0, 419, 262, 339, 172, 122, 335, 406, 284, 324, 389, 
352, 257, 325, 308, 292, 341, 322, 373, 341, 307, 286, 353, 304, 
310, 283, 347, 260, 296, 301, 305, 278, 336, 294, 352, 305, 283, 
300, 325, 332, 303, 309, 309, 306, 307, 301, 302, 302, 296, 320
), X11 = c(0, 289, 76, 66, 363, 293, 314, 314, 403, 323, 374, 
387, 254, 371, 408, 322, 398, 333, 345, 242, 281, 385, 370, 393, 
322, 280, 315, 315, 227, 308, 298, 297, 303, 321, 336, 299, 254, 
287, 294, 293, 303, 303, 309, 306, 311, 302, 302, 304, 296, 320
), X12 = c(0, 307, 234, 180, 144, 274, 414, 315, 321, 358, 295, 
319, 303, 353, 291, 260, 379, 327, 366, 353, 335, 309, 289, 353, 
332, 282, 300, 313, 277, 341, 275, 321, 336, 254, 316, 297, 345, 
328, 325, 280, 305, 309, 310, 295, 305, 312, 302, 308, 300, 320
), X13 = c(0, 54, 306, 408, 77, 341, 341, 193, 340, 230, 355, 
279, 323, 346, 314, 295, 252, 332, 254, 308, 294, 308, 313, 322, 
335, 310, 273, 311, 273, 362, 342, 317, 297, 300, 298, 320, 310, 
264, 325, 289, 315, 296, 296, 312, 310, 302, 300, 308, 300, 320
), X14 = c(0, 318, 382, 243, 109, 297, 165, 404, 373, 284, 372, 
393, 399, 224, 379, 356, 334, 331, 332, 289, 286, 294, 332, 352, 
332, 316, 334, 369, 341, 309, 331, 266, 290, 284, 338, 278, 297, 
328, 333, 292, 290, 311, 308, 307, 314, 303, 301, 301, 295, 320
), X15 = c(0, 203, 291, 359, 132, 350, 351, 229, 300, 268, 323, 
319, 326, 242, 283, 303, 314, 171, 276, 341, 351, 327, 353, 345, 
306, 347, 262, 343, 310, 309, 274, 349, 309, 254, 338, 277, 308, 
300, 319, 323, 314, 296, 298, 307, 306, 300, 303, 302, 296, 320
), X16 = c(0, 366, 195, 359, 51, 242, 114, 187, 242, 326, 259, 
274, 178, 388, 373, 334, 374, 297, 338, 297, 314, 317, 313, 330, 
339, 324, 273, 259, 327, 348, 285, 285, 268, 274, 338, 283, 297, 
285, 336, 323, 304, 313, 320, 311, 307, 306, 304, 306, 295, 320
), X17 = c(0, 276, 390, 66, 293, 341, 423, 107, 384, 153, 344, 
348, 367, 297, 348, 339, 330, 267, 330, 276, 317, 296, 241, 318, 
299, 261, 372, 304, 311, 268, 292, 247, 316, 316, 289, 303, 278, 
343, 293, 331, 277, 308, 311, 307, 306, 302, 302, 298, 295, 320
), X18 = c(0, 98, 94, 178, 356, 341, 289, 323, 292, 353, 352, 
364, 282, 402, 300, 243, 185, 371, 343, 329, 313, 251, 349, 313, 
384, 296, 319, 316, 378, 278, 305, 272, 274, 301, 317, 302, 297, 
264, 335, 323, 291, 311, 304, 305, 309, 298, 302, 303, 295, 320
), X19 = c(0, 377, 291, 356, 195, 264, 218, 356, 268, 378, 389, 
336, 159, 269, 292, 329, 371, 366, 314, 322, 383, 336, 311, 361, 
337, 321, 367, 252, 299, 307, 285, 349, 338, 268, 316, 338, 335, 
277, 294, 328, 318, 261, 311, 306, 305, 300, 302, 299, 296, 320
), X20 = c(0, 55, 266, 298, 326, 288, 388, 179, 292, 369, 405, 
311, 410, 333, 201, 308, 392, 278, 323, 369, 356, 362, 323, 285, 
299, 311, 341, 260, 276, 284, 343, 296, 330, 321, 286, 286, 335, 
321, 287, 280, 318, 308, 318, 306, 307, 304, 304, 300, 295, 320
), mean = c(0, 272.35, 262.55, 254.75, 
239.95, 286.6, 308.65, 271.25, 311.1, 320.9, 330.1, 321.45, 314.5, 
325.9, 327.3, 315.25, 312.7, 315.45, 324.7, 316.65, 325.1, 312.25, 
308.45, 334.95, 323.35, 313.25, 319.85, 299.75, 302.9, 309.1, 
301.45, 296.2, 301.85, 287.35, 313.6, 302.95, 308.55, 309.75, 
315.35, 308.4, 302.95, 298.6, 303.1, 308.45, 307.75, 302.15, 
303.05, 302.75, 296.6, 320), sd = c(0, 122.053601253496, 104.243376667045, 
116.109238491853, 112.95293617022, 59.3405869802271, 90.2116079588781, 
96.5106348107759, 50.3408382925831, 57.8381589573782, 54.7471700782693, 
51.3281240236722, 72.195276124805, 49.0937877943839, 51.3646407150821, 
47.7050973774237, 64.7132134884368, 55.4422174311617, 41.1250979842058, 
35.4092985111627, 38.3884576951266, 36.1049274957544, 39.5866471373738, 
34.6326207650047, 29.3657068166613, 33.850872646763, 32.7916468824479, 
41.1068889091533, 38.3019856569233, 33.4725339093979, 25.4257950245726, 
31.9680761813538, 27.2981105958867, 19.8978311433816, 28.3184894036611, 
21.5735583771577, 24.735389095064, 26.321644085589, 20.5484664557774, 
21.8954453326666, 14.7022196677201, 17.7746122912783, 16.9547385085921, 
4.53611240926343, 3.78188201547158, 3.29712793679271, 2.48098028164166, 
2.91773163514687, 1.78885438199983, 0)), row.names = c(NA, -50L
), class = "data.frame")

And my new slope data frame after OTUs removed:

> DF
# A tibble: 20 x 2
   AREA              COEFS
   <chr>             <dbl>
 1 X1.log(area)  -0.00302 
 2 X2.log(area)   0.0571  
 3 X3.log(area)  -0.000757
 4 X4.log(area)   0.00441 
 5 X5.log(area)   0.0286  
 6 X6.log(area)   0.132   
 7 X7.log(area)  -0.0171  
 8 X8.log(area)   0.00870 
 9 X9.log(area)   0.0278  
10 X10.log(area)  0.0110  
11 X11.log(area)  0.0659  
12 X12.log(area)  0.0315  
13 X13.log(area)  0.0921  
14 X14.log(area)  0.0199  
15 X15.log(area)  0.0333  
16 X16.log(area)  0.0698  
17 X17.log(area)  0.0395  
18 X18.log(area)  0.0886  
19 X19.log(area)  0.00601 
20 X20.log(area)  0.0705 

Could you please give me some advice?
Thank you in advance

Sorry, I do not understand what you want to plot. I take it that the COEFS column in the second data frame is the slope you want to plot on the y axis. What values do you want to use for the x axis? Keep in mind that I do not know what an OTU is. Please refer to the actual column names from your tables to explain what the x axis values should be.

I'm sorry for not clearly explained. OTUs are operational taxonomic units. I calculated the mean value from OTUs data so I want to have mean values in the x-axis.

In the first data frame, X1-X20 is in colnames but in the second data frame, X1-X20 is in rownames. And they are from different data frames Hence it's not obvious how to merge them into one data frame. but I want to represent mean and slope in one graph

Starting with you two data frame tar.un_sap.out10 and DF, I think this code will give you a data frame you can plot. Note that I had to change the name of the mean column in tar.un_sap.out10 to Avg so that the summarise() function would work correctly. You can use the Xmeans column of DF for your x axis values and the COEFS column as the y values.

library(tidyr)
library(dplyr)
STATS <- tar.un_sap.out10 %>% summarise(across(starts_with("X"), mean)) %>% 
  pivot_longer(cols = everything())


DF$Xmeans <- STATS$value

I run the code but it gives me an error message

tar.un_sap.out10 %>% rename(Avg = mean)
STATS <- tar.un_sap.out10 %>% summarise(across(starts_with("X"), mean)) %>% 
+   pivot_longer(cols = everything())
Error: Problem with `summarise()` input `..1`.
x Problem with `across()` input `.fns`.
i Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
i Input `..1` is `across(starts_with("X"), mean)`.
Run `rlang::last_error()` to see where the error occurred.

I stored the data for the original data frame tar.un_sap.out10 in a csv file just to make the code more readable. The following works for me.

library(dplyr, warn.conflicts = FALSE)
library(tidyr)
tar.un_sap.out10 <- read.csv('~/R/Play/Dummy.csv')
tar.un_sap.out10 <- tar.un_sap.out10 %>% rename(Avg = mean)

STATS <- tar.un_sap.out10 %>% summarise(across(starts_with("X"), mean)) %>% 
  pivot_longer(cols = everything())
STATS
#> # A tibble: 20 x 2
#>    name  value
#>    <chr> <dbl>
#>  1 X1     305.
#>  2 X2     294.
#>  3 X3     299.
#>  4 X4     312.
#>  5 X5     295.
#>  6 X6     291.
#>  7 X7     313.
#>  8 X8     302 
#>  9 X9     295.
#> 10 X10    304.
#> 11 X11    302.
#> 12 X12    301.
#> 13 X13    291.
#> 14 X14    307.
#> 15 X15    295.
#> 16 X16    288.
#> 17 X17    294 
#> 18 X18    294.
#> 19 X19    305.
#> 20 X20    301.

Did you forget to save the result of the rename() function in tar.un_sap.out10?
Created on 2020-09-01 by the reprex package (v0.3.0)

yes, it works
Thank you very much. I really appreciate that you took the time to help me

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.