Title: | Define Segments in y-Axis for 'ggplot2' |
---|---|
Description: | It is not very easy to define segments for y-axis in a 'ggplot2' plot. The gggap() function in this package can carry it out. |
Authors: | Jiacheng Lou [ctb], Jing Zhang [ctb], Yizhu Lvy [ctb], Zhi Jin [ctb], Carlos Morales [aut, cre] |
Maintainer: | Carlos Morales <[email protected]> |
License: | GPL-3 + file LICENSE |
Version: | 1.0 |
Built: | 2025-01-25 03:33:25 UTC |
Source: | https://github.com/cmoralesmx/gggap |
Easy-to-define segments in y-axis for 'ggplot2'.
gggap(plot, ylim, segments, tick_width, rel_heights, vjust = 0, margin = c(top = 1, right = 2, bottom = 1, left = 1), ...)
gggap(plot, ylim, segments, tick_width, rel_heights, vjust = 0, margin = c(top = 1, right = 2, bottom = 1, left = 1), ...)
plot |
A 'ggplot2' plot. |
ylim |
The y-axis limits. |
segments |
The interval of a segment. If more than one intervals are given, please use list() to concatenate them. |
tick_width |
One or more numbers for each segmented y-axis. |
rel_heights |
Numerical vector of relative segmented y-axis and segments heights, default is 1 and 0. |
vjust |
Vertical justification. Default = 0 (baseline at y). |
margin |
Margins around the text. |
... |
Arguments will be handed to plot_grid() in 'cowplot'. |
A segmented picture.
data(mtcars) library(ggplot2) p <- ggplot(data = mtcars, aes(x = gear, fill = gear)) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears") # single segments and missing tick_width gggap( plot = p, segments = c(5, 10), ylim = c(0, 50) ) # tick_width can be one or more numbers gggap( plot = p, segments = c(5, 10), tick_width = c(1, 10), ylim = c(0, 50) ) # segments list cantains more than one number vectors gggap( plot = p, segments = list(c(2.5, 4), c(5, 10)), tick_width = c(1, 0.5, 10), ylim = c(0, 50) ) # rel_heights can set the relative height for segments and segmented y-axis gggap( plot = p, segments = list(c(2.5, 4), c(5, 10)), tick_width = c(1, 0.5, 10), rel_heights = c(0.2, 0, 0.2, 0, 1), ylim = c(0, 50) ) # reversed y-axis p <- ggplot( data = mtcars, aes(x = gear, fill = gear)) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears")+ scale_y_continuous(trans = 'reverse') #single segments and missing tick_width gggap( plot = p, segments = c(10, 5), ylim = c(15, 0))
data(mtcars) library(ggplot2) p <- ggplot(data = mtcars, aes(x = gear, fill = gear)) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears") # single segments and missing tick_width gggap( plot = p, segments = c(5, 10), ylim = c(0, 50) ) # tick_width can be one or more numbers gggap( plot = p, segments = c(5, 10), tick_width = c(1, 10), ylim = c(0, 50) ) # segments list cantains more than one number vectors gggap( plot = p, segments = list(c(2.5, 4), c(5, 10)), tick_width = c(1, 0.5, 10), ylim = c(0, 50) ) # rel_heights can set the relative height for segments and segmented y-axis gggap( plot = p, segments = list(c(2.5, 4), c(5, 10)), tick_width = c(1, 0.5, 10), rel_heights = c(0.2, 0, 0.2, 0, 1), ylim = c(0, 50) ) # reversed y-axis p <- ggplot( data = mtcars, aes(x = gear, fill = gear)) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears")+ scale_y_continuous(trans = 'reverse') #single segments and missing tick_width gggap( plot = p, segments = c(10, 5), ylim = c(15, 0))
Add a legend to a 'ggplot2' plot modified by gggap().
gggap_legend(plot, margin = c(top = 200, right = 200, bottom = 200, left = 200))
gggap_legend(plot, margin = c(top = 200, right = 200, bottom = 200, left = 200))
plot |
A 'ggplot2' plot. |
margin |
Margins around the text. |
A legend-added picture
library(ggplot2) mtcars$gear <- factor(mtcars$gear) bp <- ggplot( data = mtcars, aes(x = gear, fill = gear) ) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears") gggap( plot = bp, ylim = c(0, 16), segments = c(6, 8) ) gggap_legend( plot = bp, margin = c(top = 1, right = 1, bottom = 1, left = 460) )
library(ggplot2) mtcars$gear <- factor(mtcars$gear) bp <- ggplot( data = mtcars, aes(x = gear, fill = gear) ) + geom_bar() + ggtitle("Number of Cars by Gears") + xlab("Gears") gggap( plot = bp, ylim = c(0, 16), segments = c(6, 8) ) gggap_legend( plot = bp, margin = c(top = 1, right = 1, bottom = 1, left = 460) )