Problem generating scatterplot with ggplot and tidyverse

I loaded ggplot2, tidyverse. I import the data. These were the codes I ran
data<- read.csv("who_data.csv")
head (data)

ggplot(data, aes(x = Year, y = Life.expectancy)) +
geom_point() + scale_x_log10()+
geom_smooth()

ggplot(data, aes(x = Year, y = BMI)) +
geom_point() + scale_x_log10()+
geom_smooth()

ggplot(data, aes(x = Year, y = Schooling)) +
geom_point() + scale_x_log10()+
geom_smooth()

What did I miss? What did I do wrong?

Please help

Probably contains missing data for the Life.expectancy variable. This can be eliminated by using a reduced data set.

reduced <- data[complete.cases(data),]

Hi, welcome to the forum.
seccessfully
It is not clear to me that you did anything wrong. It looks like you have successfully plotted your first graph, What were you expecting to see?

You seem to be getting a standard warning that you have some missing data (NAs in R parlance). Unless you have good reason to think that there should be no missing data, I do not think you should worry.

Hi, where should I add
reduced <- data[complete.cases(data),]

Hi technocrat. I did what you told me,

library(tidyverse)
data<- read.csv("who_data.csv")
reduced <- data[complete.cases(data),]
head (reduced)
reduced

ggplot(reduced, aes(x = Year, y = Life.expectancy)) +
geom_point() + scale_y_continuous(limits=c(30,100))+
geom_smooth()

ggplot(reduced, aes(x = Year, y = BMI)) +
geom_point() + scale_y_continuous(limits=c(0,100))+
geom_smooth()

ggplot(reduced, aes(x = Year, y = Schooling)) +
geom_point() + scale_y_continuous(limits=c(0,25))+
geom_smooth()

Now, I only has a line saying "

e[38;5;232mgeom_smooth() using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'e[39m

Is this normal? Or am I just alien to R. Which I am. :joy:

1 Like

Hi jrkrideau,

I am so sorry. I am new to R. I don't know this is normal :joy:

I can say for sure, because I don’t have the data to replicate your code, but it reads like an info message, rather than a warning or an error.

Yes, this is normal. geom_smooth() offers several different smoothers. You didn't explicitly choose one, so it chose one for you and told you what it did.

If you want it to stop printing that message, you could write:

geom_smooth(method = 'gam', formula = 'y ~ s(x, bs = "cs")')

But that choice is not always the best. For other data, other smoothers might be better. For more information you might have a look at the description of the method argument in the documentation for geom_smooth(): Smoothed conditional means — geom_smooth • ggplot2

Perfectly normal. It is just reminding you of which smoother you are using. The default is loess but there are probably a dozen or so you can use. This just reminds you of the one you are currently using. You could be using a couple and forget which one you are currently using.

1 Like

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.