ggplot Adding Tracking Colors Below X-Axis - r

I'd like to add a line below the x-axis where its color is dependant on a factor that is not plotted.
In this example, I'm creating a box plot and would like to add a line that indicates another variable.
Using the cars data set as an example and then physically dawing in what I'm trying to do:
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot()
My thought was to create a bar, column, or geom_tile plot and then arrange it below the boxplot. This is how I would do it in base R. Is there a way to add in these kinds of color labels in ggplot2?

The natural way in ggplot2 to do this sort of thing would to be facet on the categorical variable to create subplots. However if you want to keep everything on the same graph you could try using a geom_tile() layer something like this:
df <-data.frame(x = factor(c(4,6,8)), colour = factor(c(1,2,1)))
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 8, fill = colour))
Alternatively as you suggest you could align an additional plot underneath it. You could use ggarrange() in the ggpubr package for this:
plot1 <- ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 10, fill = colour))
theme(legend.position = 'none')
plot2 <- ggplot(df, aes(x=x, y=1, fill = colour)) +
geom_tile() +
theme_void() +
scale_fill_manual(values=c('orange', 'green', 'orange')) +
theme(legend.position = 'none')
library(ggpubr)
ggarrange(plot1, plot2, nrow = 2, heights = c(10, 1), align = 'h')

Related

Is there a way to combine ggplots using ggpubr that preseves/does not squish the x-axis and y-axis?

I am trying to combine several graphs i made using ggplot in an rmarkdown document into one figure using ggpubr, but the x-axis keeps getting compressed/squished. Is there a way to keep the original scale of the graphs ( just make them smaller) so that the will sit side by side without compressing the x-axis?
I don't want the x-axis compressed like this:
This is the original graph:
I hope I have explained my problem clear enough! Thank you
I expected when i used ggpubr that three mini versions of the original graph would be displayed in a figure side by side. I have had trouble finding a solution
Depending on your data, you might be able to use facet_wrap to facet by a variable, or you can use cowplot to plot three separate plots next to each other.
library(tidyverse)
library(cowplot)
data(mtcars)
ggplot(mtcars, aes(x = mpg, y = cyl, color = factor(gear))) +
geom_point() + geom_smooth() +
facet_wrap(~gear)
p1 <- ggplot(data = subset(mtcars, gear %in% 3), aes(x = mpg, y = cyl)) +
geom_point() + geom_smooth(color = "red")
p2 <- ggplot(data = subset(mtcars, gear %in% 4), aes(x = mpg, y = cyl)) +
geom_point() + geom_smooth(color = "blue") + ylab("")
p3 <- ggplot(data = subset(mtcars, gear %in% 5), aes(x = mpg, y = cyl)) +
geom_point() + geom_smooth(color = "green") + ylab("")
cowplot::plot_grid(p1, p2, p3, nrow = 1)

Can I manually change boxplot color with ggMarginal?

I'm using ggMarginal to make marginal boxplots. Is there a way to manually change the color and/or fill of the boxplots without a grouping variable? I'd like to have different colors on the x boxplot and the y boxplot.
library(tidyverse)
library(ggExtra)
foo <- data.frame(x=rnorm(100,mean=1,sd=1),
y=rnorm(100,mean=2,sd=2))
p1 <- ggplot(data = foo,aes(x=x,y=y)) +
geom_point() + coord_equal()
ggMarginal(p1, type="boxplot", size=12)
Provided I have understood you correctly, you can do the following
p1 <- ggplot(data = foo, aes(x = x, y = y)) +
geom_point() +
coord_equal()
ggMarginal(
p1,
type = "boxplot",
size = 12,
xparams = list(colour = "blue"),
yparams = list(colour = "red"))

How to minimize the white space created by the guide_area() function of the patchwork package in plots made with ggplot2?

I made 3 plots with the ggplot2 package. To arrange the plots in a single figure I used the patchwork package. In the arrangement, I put 2 plots at the top, the common legend below these plots and below the common legend the third plot. I created the common legend space with the guide_area() function, but a big unused blank area is created along with it.
How can I keep this unused blank space to a minimum?
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = mpg,
aes(x = fl,
y = displ)) +
geom_col(aes(fill = cty))
p2 <- ggplot(data = mpg,
aes(x = year,
y = hwy)) +
geom_point(aes(color = drv))
p3 <- ggplot(data = mpg,
aes(x = class,
y = displ)) +
geom_col() +
facet_grid(~year)
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
White space remains in different sizes and proportions of the figure (the white space is marked with red).
Use heights = ... inside plot_layout.
For example,
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect", heights = c(3,1,3)) &
theme(legend.position = "bottom")

Draw border around certain rows using cowplot and ggplot2

I want to somehow indicate that certain rows in a multipanel figure should be compared together. For example, I want to make this plot:
Look like this plot (with boxes around panels made with PowerPoint):
Here's the code I made to use the first plot. I used ggplot and cowplot:
require(cowplot)
theme_set(theme_cowplot(font_size=12)) # reduce default font size
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() +
theme(axis.text.x = element_text(angle=70, vjust=0.5))
plot.mpg2 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
plot.diamonds2 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() +
theme(axis.text.x = element_text(angle=70, vjust=0.5))
plot_grid(plot.mpg, plot.diamonds,plot.mpg2, plot.diamonds2, nrow=2,labels = c('A', 'B','C','D'))
Is there a change I can make to this code to get the borders that I want? Or maybe can I even make the panels A and B have a slightly different color than the background for panels C and D? That might be even better.
Since the result of plot_grid() is a ggplot object, one way to do this is to use nested plot grids: one plot_grid() for each row, with the appropriate border added via theme().
plot_grid(
# row 1
plot_grid(plot.mpg, plot.diamonds, nrow = 1, labels = c('A', 'B')) +
theme(plot.background = element_rect(color = "black")),
# row 2
plot_grid(plot.mpg2, plot.diamonds2, nrow = 1, labels = c('C', 'D')) +
theme(plot.background = element_rect(color = "black")),
nrow = 2)

Plot multiple group histogram with overlaid line ggplot

I'm trying to plot a multiple group histogram with overlaid line, but I cannot get the right scaling for the histogram.
For example:
ggplot() + geom_histogram(data=df8,aes(x=log(Y),y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
produces the right scale. But when I try to perform fill according to groups, each group is scaled separately.
ggplot() + geom_histogram(data=df8,aes(x=log(Y),fill=vec8,y=..density..),binwidth=0.15,colour='black') +
geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw()
How would I scale it so that a black line is overlaid over the histogram and on the y axis is density?
It is going to be difficult for others to help you without a reproducible example, but perhaps something like this is what you're after:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +
geom_histogram(aes(y = ..density..)) +
geom_line(stat = "density")
If you would rather the density line pertain to the entire dataset, you need to move the fill aesthetic into the geom_histogram function:
ggplot(data = mtcars, aes(x = mpg)) +
geom_histogram(aes(y = ..density.., fill = factor(cyl))) +
geom_line(data = mtcars, stat = "density")

Resources