Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create base R plot 'type = b' equivalent in ggplot2?

Tags:

plot

r

ggplot2

Base plot() functionality allows one to set type='b' and get a combined line and point plot in which the points are offset from the line segments

plot(pressure, type = 'b', pch = 19)

enter image description here

I can easily create a ggplot with lines and points as follows.

ggplot(pressure, aes(temperature, pressure)) + 
  geom_line() + 
  geom_point()

enter image description here

The lines, however, run right up to the points. I can envision a way that I might hack together something like type='b' functionality using other geoms (e.g. geom_segment()?), but I am wondering if there is a more direct way to accomplish this with geom_line() and geom_point().

like image 473
joemienko Avatar asked Aug 11 '16 11:08

joemienko


People also ask

Is Ggplot in base R?

Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.

What is Lwd and Lty in R?

In R base plot functions, two options are available lty and lwd, lty stands for line types, and lwd for line width. The type of line you can be specified based on a number or a string. In R the default line type is “solid”.

What is GG plot in R?

We use the function ggplot() to produce the plots when using the package. Therefore, ggplot() is the command, and the whole package is called ggplot2. It is a part of the R tidyverse, an ecosystem of packages designed with common APIs. It is the most widely used alternative to base R graphics.


2 Answers

A slightly hacky way of doing this is to overplot a small black point on a larger white point:

ggplot(pressure, aes(temperature, pressure)) + 
  geom_line() +
  geom_point(size=5, colour="white") + 
  geom_point(size=2) + 
  theme_classic() +
  theme(panel.background = element_rect(colour = "black"))

In addition, following Control point border thickness in ggplot, in version 2.0.0 of ggplot2 it's possible to use the stroke argument of geom_point to control the border thickness, so the two geom_points can be replaced by just (e.g.) geom_point(size=2, shape=21, fill="black", colour="white", stroke=3), eliminating the need to overlay the points.

enter image description here

like image 94
Weihuang Wong Avatar answered Sep 28 '22 23:09

Weihuang Wong


One option which is less hacky than manually matching the stroke color with the panel background is to get the panel background beforehand, either from theme_get for the default theme, or with a specific theme that you'll be using. Using a stroked shape like 21 lets you make the inner circle black and the stroke the same color as the background.

library(ggplot2)

bgnd <- theme_get()$panel.background$fill

ggplot(pressure, aes(x = temperature, y = pressure)) + 
  geom_line() + 
  geom_point(shape = 21, fill = "black", size = 2, stroke = 1, color = bgnd)

A couple SO questions (here's one) deal with the math behind shortening segments between points. It's simple but tedious geometry. But in the time since this question was first posted, the lemon package has come out, which has a geom to do this. It's got arguments for how to calculate the shortening, which probably require just some simple tweaking.

library(lemon)

ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_pointline()

like image 22
camille Avatar answered Sep 28 '22 21:09

camille