Cannot replicate plotting, pls help

Hi,
I've created a plot combining 4 plots using ggarrange. Following is the code, it worked perfectly.

sleep_vs_activity <- ggarrange(
ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_TotalMinAsleep, y=avg_VActminutes), color= 'dark blue', alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_TotalMinAsleep, y=avg_VActminutes)) +
labs(title = "Very active minutes", x= 'Avg_Sleep (mins)', y= 'Avg_Very Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_TotalMinAsleep, y=avg_FActMinutes), color= '#FFA500', alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_TotalMinAsleep, y=avg_FActMinutes)) +
labs(title = "Fairly active minutes", x= 'Avg_Sleep (mins)', y= 'Avg_Fairly Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_TotalMinAsleep, y=avg_LActMinutes), color="#008000", alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_TotalMinAsleep, y=avg_LActMinutes)) +
labs(title = "Lightly active minutes", x= 'Avg_Sleep (mins)', y= 'Avg_Lightly Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_TotalMinAsleep, y=avg_SActMinutes), color="#800000", alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_TotalMinAsleep, y=avg_SActMinutes)) +
labs(title = "Sedentary minutes", x= 'Avg_Sleep (mins)', y= 'Avg_Sedentary Minutes')
)
sleep_vs_activity

Using the same code, I tried to combine 4 different plots:

inactive_vs_activity <- ggarrange(
ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_InactiveMinBed, y=avg_VActminutes), color= 'dark blue', alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_Inactive, y=avg_VActminutes)) +
labs(title = "Very active minutes", x= 'Avg_Inactive (mins)', y= 'Avg_Very Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_InactiveMinBed, y=avg_FActMinutes), color= '#FFA500', alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_Inactive, y=avg_FActMinutes)) +
labs(title = "Fairly active minutes", x= 'Avg_Inactive (mins)', y= 'Avg_Fairly Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_InactiveMinBed, y=avg_LActMinutes), color="#008000", alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_Inactive, y=avg_LActMinutes)) +
labs(title = "Lightly active minutes", x= 'Avg_Inactive (mins)', y= 'Avg_Lightly Active Minutes'),

ggplot(data=activity_sleep) +
geom_point(mapping = aes(x=avg_InactiveMinBed, y=avg_SActMinutes), color="#800000", alpha= 1/4) +
geom_smooth(method = loess,formula =y ~ x,mapping = aes(x=avg_Inactive, y=avg_SActMinutes)) +
labs(title = "Sedentary minutes", x= 'Avg_Inactive (mins)', y= 'Avg_Sedentary Minutes')
)
inactive_vs_activity

This code says

Error in FUN(X[[i]], ...) : object 'avg_Inactive' not found

inactive_vs_activity
Error: object 'inactive_vs_activity' not found

Any clue why? Any help will be much appreciated.

In the aes() of geom_point, you set x = avg_InactiveMinBed but in geom_smooth of the same plot you set x=avg_Inactive. The error message suggests that there is no column named avg_Inactive and it seems strange to use a different x variable for the points and the smoothed line. Are you sure avg_Inactive is the the correct name?

1 Like

OMG, thank you so much! Such a silly mistake, thankfully got resolved.

This topic was automatically closed 7 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.