Home > Blockchain >  Can I get the nice visuals of R-Markdown plotting using non-markdown R?
Can I get the nice visuals of R-Markdown plotting using non-markdown R?

Time:10-31

I am trying to get my ggplots to look as nice as possible. When using R Markdown, the plot comes out with extremely smooth lines:

enter image description here

However, when using standard R (still in RStudio, if that matters), the same plot comes out like this:

enter image description here

(It may not look that bad here, but the wobbly lines can often get very choppy in practice.)

I have tried to include an MWE for the plot, I'm not sure if I need to include anything about my R-Markdown settings. They are pretty fundamental, nothing crazy, as I am not particularly confident in R-Markdown.

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, dplyr, ggplot2)

x <- 0:10
demand <- function(x) 10 - x
supply <- function(x) 1   x
ggplot(data.frame(x = c(0:10)), aes(x))  
  scale_x_continuous(limits = c(0, 10.5), expand = c(0, 0), breaks = seq(0, 10, 1))  
  scale_y_continuous(limits = c(0, 10.5), expand = c(0, 0), breaks = seq(0, 10, 1))  
  labs(x = "Q", y = "P")  
  stat_function(aes(x), fun = supply, size = 1)   # supply function
  stat_function(aes(x), fun = demand, size = 1)

I don't want to use R-Markdown every time I produce a plot, for reasons I don't want to go into. Is there some way to get the nice visual output in regular R?

Much thanks in advance, extra thanks for politeness.

CodePudding user response:

You can save it with ggsave to have a high-quality image. ggsave will save the last plot you ran. Here is an example:

ggsave("myplot.png", device = "png",
       width = 10, height = 4, units = "in", dpi = 600, type = "cairo-png")

It will still look funny when you see preview in RStudio, but the image will be saved with smooth lines. Here is the image I saved with the code above:

enter image description here

In deviceyou can choose how you want to save the plot. From the function documentation:

Device to use. Can either be a device function (e.g. png()), or one of "eps", "ps", "tex" (pictex), "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only).

  • Related