0

I hope someone can help me with my problem. I have this code

lot <- ggplot(x,aes(x$Emissions..kt.CO.eq..per.capita, x$brandstof.consumptie.in.ton)) + 
  scale_x_continuous("Emissies kt CO2-eq per capita") + 
  scale_y_continuous("energieconsumptie in transportsector per capita (ton)") + 
  geom_smooth(method = "lm") + 
  geom_text(hjust = 0, nudge_x = 0.5, size = 4, aes(label = x$landen))

But if I generate my plot, I get something very weird: the line won't fit the datapoints (see picture).

enter image description here

If I generate the plot without the geom_smooth() function I get a nice plot.

enter image description here

I did already restart R, did it with other data. But it won't help.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
jiroose
  • 1
  • 1
  • 1
    You will get much faster, better help if you make this question reproducible by providing your sample data. It's not that `geom_smooth` "wont work", it's likely working properly and there is something going on in your data, which we wont be able to provide insight on without at least a snippet. Here is a link for some tips on how to provide your data in R: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – jpsmith Apr 14 '23 at 20:23
  • 2
    Have you tried removing or reducing `nudge_x` in `geom_text`? – Seth Apr 14 '23 at 20:25
  • 3
    First step is to update the aes definition: `aes(Emissions..kt.CO.eq..per.capita, brandstof.consumptie.in.ton)`. ggplot knows to reference the data frame thus the `x$` is unnecessary and problematic. – Dave2e Apr 14 '23 at 20:25
  • 3
    ^ yes it seems to be the `nudge_x`, which says you want the labels shifted 0.5 to the right, from data that otherwise would only live between x range of 0 to 0.01. Try `nudge_x = 0.001` or drop it. – Jon Spring Apr 14 '23 at 21:37
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 15 '23 at 10:14
  • Thanks everybody for the quick responses, it was indeed the nudge_x function! the nudge_x = 0.001 solved the problem for me! – jiroose Apr 15 '23 at 12:16

0 Answers0