function plot() error

Hello! I'm a beginner of R and I'm trying to learn.
I'm trying to use the plot function to work on a dataframe.

These are the firs lines of the data frame:

head(gapminder)
country year infant_mortality life_expectancy fertility population gdp continent region
1 Albania 1960 115.40 62.87 6.19 1636054 NA Europe Southern Europe
2 Algeria 1960 148.20 47.50 7.65 11124892 13828152297 Africa Northern Africa
3 Angola 1960 208.00 35.98 7.32 5270844 NA Africa Middle Africa
4 Antigua and Barbuda 1960 NA 62.97 4.43 54681 NA Americas Caribbean
5 Argentina 1960 59.87 65.39 3.11 20619075 108322326649 Americas South America
6 Armenia 1960 NA 66.86 4.55 1867396 NA Asia Western Asia

I want to create a plot but when I use the function I get an errore message that says that the object "lif-expectancy" hasn't been found.

gapminder %>% plot(infant_mortality, life_expectancy)
Error in match.fun(panel) : oggetto "life_expectancy" non trovato

Could you possibly help me?
Thank you in advance!!

Here are two ways to use the plot() function with a data.frame. I do not think the %>% operator is helpful with plot(), though maybe others know of a way to use it.

DF <- data.frame(Value1 = 1:10, Value2 = 11:20, Value3 = 21:30)
head(DF)
#>   Value1 Value2 Value3
#> 1      1     11     21
#> 2      2     12     22
#> 3      3     13     23
#> 4      4     14     24
#> 5      5     15     25
#> 6      6     16     26
plot(DF[, c("Value1", "Value2")])

plot(Value2 ~ Value1, data = DF)

Created on 2019-12-24 by the reprex package (v0.3.0.9000)

1 Like

Hi,

Welcome to the RStudio community!

I like to add to @FJCC reply that indeed the plot() function does not work with the pipe operator %>% as this is part of the Tidyverse and is built to be used with compatible tidyverse functions. You can use ggplot with this pipe operator if you like to plot that way.

library(dplyr)
library(ggplot2)

data(iris)

#Use the base-R plot function
plot(iris$Sepal.Length, iris$Sepal.Width)

#Use the Tidyverse piping method with ggplot 
iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point()

For more info on using Tidyverse go to their website:

Hope this helps,
PJ

1 Like

Thank you very much for your help!

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