Simple Regression Line is Slightly Jagged

Not a major issue, but my regression line appears slightly jagged, see below

FDI vs Broadband

Is there anything that I can do about this or is this just the way it is in R?

ggplot(dataset, aes(x=`Broadband Subs (%)`, y=`2019 FDI ($m)`)) + 
  geom_point(size=2, shape=16, color="blue") +
  geom_smooth(method='lm',formula=y~x, se=FALSE, color='red', size=1.1) +
  scale_y_continuous(label = comma) +
  theme(panel.background = element_rect(fill="peachpuff"), 
        plot.background = element_rect(fill="peachpuff")) +
  theme(panel.grid.major = element_line(colour = "gray72"), 
        panel.grid.minor = element_line(colour = "gray72")) + 
  theme(text = element_text(family = "Cam"), plot.title = element_text(hjust = 1/2)) +
  labs(x="Fixed Broadband Subscriptions (per 100 People)", y="Foreign Direct Investment ($m)", 
       title="Foreign Direct Investment and Internet Access (2019)") 

Hi there,

I am assuming you're on a windows system. The best way to solve this problem is to change the graphic device you're using. I have found that cairo works really well for this.

See example below of how to force cairo as your graphics device

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red") + 
    labs(title = "Normal") +
    ggsave("normal.png") +
    labs(title = "Cairo") +
    ggsave("cairo.png", type = "cairo")

Here you can see an example too: "cairo" as default in ggsave & RStudio Plots pane in Windows?

When I try to add the line

  ggsave("cairo.png", type = "cairo")

To ggplot, I get the following error output:

+   title="Cairo") +
+   ggsave("cairo.png", type = "cairo")
Saving 6.89 x 4.94 in image
Error: Can't add `ggsave("cairo.png", type = "cairo")` to a ggplot object.

Any idea why this occurs?

NB. Yes I'm running Windows 11

If you can add real/accessible data to your plot I can have a look at your example.

Working now - I just forgot to install the Cairo package. Thanks for helping.

Fantastic :slight_smile: Glad it is working. Feel free to mark the post which helped as the solution.

1 Like

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.