Reversing y-axis in an individual ggplot facet

Lars Lau Raket

I am plotting a range of different data measured on the same individuals next to each other in facets. For some types of data a positive value is "good" and for some a negative value is "good". The latter types of variables are usually plotted with flipped y-axes. Is it possible to modify axis directions in individual facets with ggplot?

dat <- data.frame(type = rep(c('A', 'B'), each = 10), x = 1:10, y = rnorm(20))

ggplot(dat, aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')

For example, could I do the above plot with the y-axis for B reversed?

bouncyball

I'm not sure if that's possible, so I would opt for a solution using the grid.arrange function from the gridExtra package.

library(gridExtra)
library(ggplot2)

dat <- data.frame(type = rep(c('A', 'B'), each = 10), x = 1:10, y = rnorm(20),
                  stringsAsFactors = FALSE)

p_A <- ggplot(subset(dat, type == 'A'), aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')+
    scale_y_continuous(breaks = c(-1,0,1))
p_B <- ggplot(subset(dat, type == 'B'), aes(x, y)) + geom_point() + facet_wrap( ~ type, scales = 'free_y')+
    scale_y_reverse(breaks = c(-1,0,1))

grid.arrange(p_A, p_B, nrow = 1)

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ggplot: limit axis limits/breaks of individual facet

Reverse Y axis in ggplot and facet

ggplot2 change axis limits for each individual facet panel

Segment/Point ggplot2 is reversing x & y axis

ggplot facet_grid with different y axis scales: reverse axis for a facet panel

Independent y-axis per nested facet in ggplot

Adjusting y axis limits in ggplot2 with facet and "free" scales

Ordering factors in each facet of ggplot by y-axis value

ggplot2 change date axis limits for each individual facet panel using facetscales package

Setting individual axis limits with facet_wrap and scales = "free" in ggplot2

Dual y-axis while using facet_wrap in ggplot with varying y-axis scale

ggplot2 Facet Wrap Reorder by y-axis, Not x-axis

Different x axis for ggplot facet

Annotating text on individual facet in ggplot2

Force individual y axis limits

How to increase the default y-axis limit in ggplot when scales in facet_wrap() are set to "free"

Sorting Y-axis of barplot based on the decresing value of last facet grid in ggplot2

Change y axis limits for each row of a facet plot in ggplot2

How to define common y-axis limits when using facet_grid() in ggplot2

Facet_wrap and scale="free" unexpectedly centers y-axis at zero in ggplot2

ggplot2 facet wrap: y-axis scale on the first row only

ggplot2 facet_grid create panels on the y-axis

R ggplot facet_wrap with different y-axis labels, one values, one percentages

Is there an R function to get the max y-axis when doing ggplot facet?

ggplot facet grid y axis some values are not visible how adjust grid

How to make y-axis scales same size for each facet in ggplot2?

How to set different y-axis scale in a facet_grid with ggplot?

In ggplot/facet_wrap(), how to marke axis Y have different format

ggplot secondry y axis scale based on data with facet_wrap or grid_arrange

TOP Ranking

HotTag

Archive