`geom_smooth()` using formula 'y ~ x' Error: Continuous value supplied to discrete scale

The code you have will produce a plot somewhat like the one on the left, given the proper data. I manipulated the values of sources_Metric and got a plot with separate points and linear fits for each sex. Try running the code below. If your real data are not giving you what you expect, you should start another thread where that can be discussed. It is preferred to keep each thread to one topic and I think your original problem has been solved.

library(ggplot2)
QNR2 <-  data.frame(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))
#Me inventing data
QNR2$sources_Metric <- ifelse(QNR2$SexCD == 1, QNR2$Age * 1.5 + 0.6, 
                              QNR2$Age * -0.5 + 0.2)

QNR2$sources_Metric <- QNR2$sources_Metric + rnorm(20, mean = 0, sd = 4)
###

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(formula = y ~ x, 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))

hey, really can i get a similar left graph? please how do i do it.

what does this mean and how can i apply it to other Variables?

The ggplot code I used with the dput() output you posted produces that plot. The only thing I did was change the values of the sources_Metric column because they are all zero in the 20 rows of data you posted.

These two lines of code

QNR2$sources_Metric <- ifelse(QNR2$SexCD == 1, QNR2$Age * 1.5 + 0.6, 
                              QNR2$Age * -0.5 + 0.2)

QNR2$sources_Metric <- QNR2$sources_Metric + rnorm(20, mean = 0, sd = 4)

calculate new values for sources_Metric but they are just an invention. If your data represent anything real, you should not do that just to make the plot look a certain way.

Yes, the sources_Metric are zero, both hygieneSanitation_Metric and attitudeWASH_Metric.
they are not suppose to be zero. i m so worried i m do a wrong thing.

If you have questions about reading in the data or any processing you are doing that might cause your data to be zeros, please start another topic to discuss that problem. I or someone else can help you with that but the original problem of this thread is solved.

1 Like

Thank you so much.
You are the best.

Hello, please how do i make the following data set into a single data or matric
QS1CD QS2CD QS3CD QS4CD QS5CD
1 3 2 NA 2
3 3 2 NA 2
1 3 2 NA 2
2 3 2 NA 2
2 5 2 NA 2
3 3 1 1 2
6 3 1 1 2
6 5 1 1 5
1 5 1 1 2

Please put this question in a new thread.

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