Thank you! Using that data I got you original ggplot code to run with only a a few changes. Here is the original code
ggplot(QNR, aes(x=N2Age, y=sources_Metric, fill=N3Sex, colour=N3Sex))+
labs(title = "sources of water by sex", x=" ", y="sources (%)")+
scale_color_discrete(name="Sex") + theme(legend.title = element_text(size = 7))+
geom_point(aes(size=hygieneSanitation_Metric)) + geom_smooth(method=lm) + theme_bw()+
theme(legend.position="bottom", plot.title = element_text(hjust = 0.5))+
guides(fill = "none", color= "none", size = "none") + scale_size(range = c(1,3))
It plots N2Age on the x axis and N3Sex is used for fill and color. The data you posted does not have those columns but it has Age and SexCD, so I substituted those. The ggplot code would then throw the error "Error: Continuous value supplied to discrete scale". This happens because scale_color_discrete needs discrete values but SexCD is numbers. I fixed that by making a factor out of SexCD. The example below shows both the failed version and the corrected one. The statement geom_smooth()
using formula 'y ~ x' still appears. You can change that by making the call to geom_smooth
geom_smooth(formula = y ~ x, method=lm)
The graph looks strange but remember that it only uses the first few rows of your data.
library(ggplot2)
QNR2 <- structure(list(Age = c(27L, 45L, 19L, 17L, 32L, 35L, 27L, 23L,
20L, 30L, 51L, 31L, 31L, 64L, 48L, 22L,
16L, 20L, 19L, 35L),
SexCD = c(1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L,1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L),
sources_Metric = c(0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0),
hygieneSanitation_Metric = c(0,0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
attitudeWASH_Metric = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)),
row.names = c("KI1", "KI2","KI3", "KI4", "KI5", "KI6", "KI7",
"KI8", "KI9","KI10", "KI11","KI12", "KI13",
"KI14", "KI15", "KI16", "KI17", "KI18", "KI19","KI20"),
class = "data.frame")
ggplot(QNR2, aes(x=Age, y=sources_Metric, fill=SexCD, colour=SexCD))+
labs(title = "sources of water by sex", x=" ", y="sources (%)")+
scale_color_discrete(name="Sex") +
theme(legend.title = element_text(size = 7))+
geom_point(aes(size=hygieneSanitation_Metric)) +
geom_smooth(method=lm) +
theme_bw()+
theme(legend.position="bottom", plot.title = element_text(hjust = 0.5))+
guides(fill = "none", color= "none", size = "none") + scale_size(range = c(1,3))
#> `geom_smooth()` using formula 'y ~ x'
#> Error: Continuous value supplied to discrete scale
QNR2$SexCD <- factor(QNR2$SexCD)
ggplot(QNR2, aes(x=Age, y=sources_Metric, fill=SexCD, colour=SexCD))+
labs(title = "sources of water by sex", x=" ", y="sources (%)")+
scale_color_discrete(name="Sex") +
theme(legend.title = element_text(size = 7))+
geom_point(aes(size=hygieneSanitation_Metric)) +
geom_smooth(method=lm) +
theme_bw()+
theme(legend.position="bottom", plot.title = element_text(hjust = 0.5))+
guides(fill = "none", color= "none", size = "none") + scale_size(range = c(1,3))
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-06-15 by the reprex package (v0.3.0)