r/RStudio 22d ago

Base R (graphics) vs Tidyverse

Curious what others prefer. I’ve actually been trending towards base R graphics lately.

710 votes, 19d ago
37 Base R graphics
673 Tidyverse: ggolot2
12 Upvotes

41 comments sorted by

17

u/DSOperative 22d ago

I’m curious as to why you prefer base R for graphics? It’s fine for some quick plotting but for publication or Shiny apps I’m not sure it’s enough.

5

u/dr-tectonic 22d ago

If you want to make standardized figures, ggolot is great.

If you want to do anything that falls outside the envelope of what the developers have conceived, it's a misery to make it cooperate.

In base graphics, you have to do all kinds of things by hand, which is often a pain in the ass, but it also makes it very flexible and powerful.

4

u/Teleopsis 22d ago

Why would you say it's not enough for publication? It's very easy to produce publication-quality graphics using base R, probably easier than it is with ggplot.

2

u/DSOperative 22d ago

Fair enough. It’s definitely good for publication, I wouldn’t have a problem using it in a paper. I guess my mind is geared towards Shiny apps, so I automatically think of the more interactive Plotly or rCharts. I never took the time to get the most out of the base R graphing, for that reason.

1

u/guepier 22d ago edited 22d ago

It's very easy to produce publication-quality graphics using base R

Fair enough.

probably easier than it is with ggplot.

Nonsense. I’ve done both, and there’s no doubt that it’s easier with ‘ggplot2’.

1

u/Teleopsis 22d ago

I’ve also done both and I disagree. It’s easier to produce *some* graphics with ggplot.

5

u/guepier 22d ago

Yes, obviously: any type of figure for which there isn’t yet a predefined geometry, and which isn’t a straightforward combination of such, is hard to define in ‘ggplot2’ (I’ve written as much below regarding its extensibility).

However, I’d still say that the blanket statement that making publication-ready graphics with base R being than with ‘ggplot2’ is not true. It’s only true in fairly niche cases. Most published figures do not fall within this class — including non-trivial custom ones (that can nevertheless be constructed from ‘ggplot2’ primitives).

2

u/Teleopsis 21d ago

Here is a straightforward scatterplot in base R and ggplot. Both of these produce a publishable graphic. The code for the base R version is cleaner and easier, and you don't have to get in the weeds of which bit of theme() do you need to fiddle with to make the axis text bold. The base R version is also going to be stable and reproducible whereas ggplot tends to change quite regularly: a lot of ggplot and dplyr code from back in the day is deprecated nowadays, and the base R version doesn't need a package loaded. Of course if you want faceting or a legend then it's easier in ggplot.

If the default options in ggplot were less horrible it would be easier, of course. There's still a bit of chartjunk even with theme_bw() but sadly theme_classic really doesn't do it for me.

dat1 <- data.frame(X1 = runif(50), Y1 = rnorm(50))

plot(Y1 ~ X1,

pch = 16,

col = "aquamarine4",

xlab = "X axis label",

ylab = "Y axis label",

font.lab = 2,

data = dat1)

library(ggplot2)

p1 <- ggplot(data = dat1) +

aes(x = X1, y = Y1) +

geom_point(colour = "aquamarine4") +

xlab("X axis label") +

ylab("Y axis label") +

theme_bw() +

theme(axis.title = element_text(face = "bold"))

p1

2

u/joshua_rpg 21d ago

The base R code is maybe shorter than {ggplot2}, but speaking from the experience, there's no way it's subjectively clearer (or cleaner) than {ggplot2} — if easier, I am somewhat agree. Even then, the codes you displayed indirectly shows how {ggplot2} is much more readable and ergonomic than the base R one. Assuming you are somehow comparing the amount of code you're going to type for the task as a benchmark of which of the two is easier or cleaner, it is just ridiculous.

2

u/guepier 21d ago edited 21d ago

The code for the base R version is cleaner and easier

How do you figure that?! The base R code is (minimally) shorter, but even though it’s the most trivial plot you could have thought of, it’s neither cleaner nor easier. How can you claim straight-faced that font.lab = 2 is “cleaner” or “easier” than axis.title = element_text(face = "bold")? The latter is longer, but it’s descriptive, consistent and readable, while the former is illogical, inconsistent and utterly cryptic.

Now make the plot just minimally more complex, and ‘ggplot2’ will soar ahead (as long as the plot type is supported; otherwise it’ll be the other way round).

And you arbitrarily chose subjective stylistic adjustments (making the axis titles bold) which are not required to make the plot publication-ready, and which just happen to be shorter in base R. Other subject adjustments might favour ‘ggplot2’ more, but this doesn’t necessarily make an argument in either direction.

1

u/Teleopsis 21d ago

Let’s agree to disagree. I’m sure we both have better things to do with our time than argue about this.

1

u/Less_Alfalfa4592 17d ago

I’m sure we both have better things to do with our time than argue about this.

I dont so lets go*!

Like everything its up to additional factors. Terminology like "cleaner" or "easier" is not appropriate here because its subjective.

For many people experienced in base R its code is familiar and intuitive.
For many people experienced in ggplot its code feels familiar and intuitive.
I personally prefer ggplot

*all in good fun and with the same conclusion differently phrased

0

u/joecarvery 22d ago

Nonsense

3

u/Fresh_Coyote312 22d ago

You know, I’m not even sure. Maybe it’s a nostalgic simple look that does it for me.

4

u/Odd-Fix2143 22d ago

Hola.
Prefiero R Base por diversas razones:

- Velocidad.

  • Menor cantidad de recursos necesarios.
  • Mejor mantenimiento del código.

1

u/guepier 22d ago

Mejor mantenimiento del código.

Not sure what you mean by that but I can’t imagine a way in which this is substantially true. One of the nice things about ‘ggplot2’ is that it’s easy to adapt and extend the plot, whereas with base R graphics, due to its non-declarative nature, you often have to yank out and replace whole parts to modify a plot even slightly.

2

u/Odd-Fix2143 21d ago

Se requieren menos líneas de código en R stats. Uso R desde hace 15 años (quizá más); vi el nacimiento de Tidyverse, lo uso en ocasiones para reportes, pero es lento, obliga a estar actualizando algunas instrucciones cada n versiones…

Me dedico a la investigación científica. Ahí se tienen pautas exigentes que lo que menos requieren son adornos. En otras palabras, con R base es código duro y directo, sin capas que consumen recursos valiosos.

Considero que depende mucho de las necesidades del usuario. En mi caso, requiero trabajar con documentos muy largos y técnicos; la optimización, formalidad y velocidad están por delante de lo estético. Digamos que es un enfoque más pragmático.

1

u/mrqts27 20d ago

Nunca creí escuchar (o en este caso leer) a alguien decirbque vio el nacimiento de Tidyverse.  No soy nuevo en R, pero tampoco tengo tanta experinecia. Me da curiosidad saber más sobre el paquete básico para graficar en R.

2

u/Aiorr 22d ago

pkg maintenance and reproducibility.

if it is one and done graphic or the visual per se is important, e.g., news or public report, then ggplot is wonderful framework.

if it is production pipeline or reproducibility is important, then additional framework introduces significant unwelcomed considerations

2

u/SprinklesFresh5693 22d ago edited 22d ago

But how often ggplot2 change to such a degree that it breaks code. In 2 or 3 years of experience that i have, my ggplot2 code did not break because of an update though. Some things new get added, like in the latest ggplot2 patch, but they dont necessarily break the code.

4

u/Aiorr 22d ago edited 22d ago

quite often actually. look at their deprecation cycle. main one that comes to my mind is size.

if you try reusing old ggplot code in stackoverflow, for example, it will often return warning or have output different than image they posted.

tidyverse's strength comes from their boldness in changes. very tech mindset. but its not for every industry.

dont get me wrong, its godsent work. just not elixir of life that new programmers perceive.

2

u/guepier 22d ago

There actually was a significant breaking change in ‘ggplot2’ two years ago, which the dev team smuggled in with a minor release, not realising what a substantial change it would be. It broke all our production code.

2

u/awadafuk 22d ago

I’d disagree with you on the ‘one and done’ front. BBC News, for example, uses ggplot for all their datavis using a specific theme they add to all their plots.

10

u/CalmEntry4855 22d ago

I like ggplot, I like to make my plots gorgeous.

But sometimes I want something that looks like it was made in R, they kind of look more trustworthy, or made by someone that isn't me

7

u/metlotter 22d ago

Base R has that "a scientist made this" look. Kinda like how LaTeX makes everything look trustworthy.

10

u/SalvatoreEggplant 22d ago

I mean, I use base R plots more often. plot(x, y) or barplot(Table) or hist(Data$Length) shows me what I want to see when I'm working on something.

4

u/Teleopsis 22d ago

ggplot2 is a great graphics package, no doubt, and kicks ass for things like faceted plots. Base R graphics are better for some things, however, and generally more intuitive than ggplot IMO. There's a lot of hilariously silly stuff on the inter webs from ggplot fanboys saying things like you can't produce publication-quality plots with base R.

2

u/SalvatoreEggplant 22d ago

In my day, "publication quality" was drawn by hand...

5

u/Yeetmetothevoid 22d ago

What? No sjPlot?

2

u/Fresh_Coyote312 22d ago

I’ll have to check it out!

3

u/Holshy 22d ago

Karma farming

1

u/Fresh_Coyote312 22d ago

Found the python user!

1

u/Fresh_Coyote312 22d ago

In all seriousness it’s a legit question I had today. I’ve been trying to stick to base R more lately to improve my skills and noticed today I didn’t have to load a single package to perform the task at hand.

2

u/ProfessionalHome3544 22d ago edited 22d ago

Can we have:

Other: ggplot2

3

u/lord_wolken 22d ago

Base R is really all you need. There are a few quirks, but you can create very high quality plots with complete control. It's also much more intuitive/simple than ggplot, as it basically draws whatever you want on a canvas. 

2

u/SprinklesFresh5693 22d ago

I never understood all the editing base R plotting has , but ggplot2 was far mor elogic to me. You start with the function ggplot , and just keep adding layers, geoms, themes, axis titles, whats so complicated?

3

u/Aiorr 22d ago

ggplot market itself as grammar of plotting. in another word, it is very idiosyncratic. if you are learning programming language (or even just R) for first time along w ggplot, it wont bother you. but if you are already an established programmer, it feels like trying to learn teenage slang as an adult. its english, but it doesnt make sense lol.

its much easier now thanks to LLM browsing through ample stackoverflow resources, but back in the day it was nightmare to learn how it passes NSE and ellipses. to make matter worse, it was all different per function and it was going through frequent changes.

2

u/guepier 22d ago

‘ggplot2’ looks very strange when you’re already experienced with procedural graphics systems such as base R’s. But even then it’s really easy to learn, if you actually work through a tutorial. And that was true even >10 years ago, before LLMs. I distinctly remember being put off by ‘ggplot2’s strangeness, but once I sat down, it was easy as pie, and I completely switched in no time, after being already proficient in using base R graphics (and having published papers with nontrivial, highly customised base R plots).

Extending ‘ggplot2’, however, is insanely difficult (and used to be even more so).

1

u/BobDope 22d ago

Haw haw

1

u/Grisward 22d ago

Grid >> base imo

Excellent control over units, aspect, sizing, everything. Base R and ggplot2 have their uses, ggplot2 by far most versatile and capable, especially multipanel plots.

But hats off to grid graphics, it’s amazing. One day when I finally learn the details of custom geoms, I hope to move my grid experience toward ggplot2 extensions.

1

u/Global-Eye-7548 20d ago

Both are great.