How do you draw a boxplot without specifying x axis? - r

The base graphics can nicely plot a boxplot using a simple command
data(mtcars)
boxplot(mtcars$mpg)
But qplot requires y axis. How can I achieve with qplot the same like base graphics boxplot and not get this error?
qplot(mtcars$mpg,geom='boxplot')
Error: stat_boxplot requires the following missing aesthetics: y

You have to provide some dummy value to x. theme() elements are used to remove x axis title and ticks.
ggplot(mtcars,aes(x=factor(0),mpg))+geom_boxplot()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
Or using qplot() function:
qplot(factor(0),mpg,data=mtcars,geom='boxplot')

you can set the x aesthetics to factor(0) and tweak the appearance by removing unwanted labels:
ggplot(mtcars, aes(x = factor(0), mpg)) +
geom_boxplot() +
scale_x_discrete(breaks = NULL) +
xlab(NULL)

You can also use latticeExtra, to mix boxplot syntax and ggplot2-like theme:
bwplot(~mpg,data =mtcars,
par.settings = ggplot2like(),axis=axis.grid)

Related

Plot on reciprocity scale in R

I would like to make a plot like the one schematized below with x-axis on a reciprocal scale and y-axis on a profit scale, ideally with ggplot2 but I haven't figured out a way to do that. Is there anyone who could help me?
You can use scales package to transform scales, functions reciprocal_trans() and probit_trans().
library(ggplot2)
library(scales)
ggplot(df, aes(x, y)) +
geom_line() +
scale_x_continuous(trans = reciprocal_trans()) +
scale_y_continuous(trans = probit_trans())

Increase spaces between x values of boxplot (overlapping x labels)

Hello I am very new to using coding language and recently made my first couple of figures in R. I used this code to make the figures and they turned out good except that the labels in the x axis were overlapping.
library(ggplot2)
ggplot(LR_density, aes(x=Plant_Lines, y=`Lateral_Root_Density.(root/cm)`, fill=Expression_Type)) +
geom_boxplot() +
geom_jitter(color="black", size=0.4, alpha=0.9) +
ggtitle("Lateral root density across plant expression types")
The figure produced by the line of code I used
I was wondering if anyone knew how to get the x axis labels to be more spaced out in ggplot2 boxplots. I have been looking around but havent found a clear answer on this. Any help on what to do or where to look would be great!
As per comment, this thread shows another option to deal with overlapping x axis labels, which one can use since ggplot2 3.3.0
In included a second graph which "squeezes" the axis a bit, which kind of also simulates the effect of changing the viewport/ file size.
library(ggplot2)
ggplot(diamonds, aes(x = cut, y = price)) +
geom_boxplot() +
scale_x_discrete(guide = guide_axis(n.dodge = 2))
ggplot(diamonds, aes(x = cut, y = price)) +
geom_boxplot() +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
coord_fixed(1/10^3.4)
Created on 2020-04-30 by the reprex package (v0.3.0)

Label right and left side of y axis with different labels using ggplot2 geom_tile function [duplicate]

I want to duplicate the left-side Y-axis on a ggplot2 plot onto the right side, and then change the tick labels for a discrete (categorical) axis.
I've read the answer to this question, however as can be seen on the package's repo page, the switch_axis_position() function has been removed from the cowplot package (the author cited (forthcoming?) native functionality in ggplot2).
I've seen the reference page on secondary axes in ggplot2, however all the examples in that document use scale_y_continuous rather than scale_y_discrete. And, indeed, when I try to use the discrete function, I get the error:
Error in discrete_scale(c("y", "ymin", "ymax", "yend"), "position_d", :
unused argument (sec.axis = <environment>)
Is there anyway to do this with ggplot2? Even a completely hacked solution will suffice for me. Thanks in advance. (MREs below)
library(ggplot2)
# Working continuous plot with 2 axes
ggplot(mtcars, aes(cyl, mpg)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.+10))
# Working discrete plot with 1 axis
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point()
# Broken discrete plot with 2 axes
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point() +
scale_y_discrete(sec.axis = sec_axis(~.+10))
Take your discrete factor and represent it numerically. Then you can mirror it and relabel the ticks to be the factor levels instead of numbers.
library(ggplot2)
irislabs1 <- levels(iris$Species)
irislabs2 <- c("foo", "bar", "buzz")
ggplot(iris, aes(Sepal.Length, as.numeric(Species))) +
geom_point() +
scale_y_continuous(breaks = 1:length(irislabs1),
labels = irislabs1,
sec.axis = sec_axis(~.,
breaks = 1:length(irislabs2),
labels = irislabs2))
Then fiddle with the expand = argument in the scale as needed to more closely imitate the default discrete scale.

Duplicating (and modifying) discrete axis in ggplot2

I want to duplicate the left-side Y-axis on a ggplot2 plot onto the right side, and then change the tick labels for a discrete (categorical) axis.
I've read the answer to this question, however as can be seen on the package's repo page, the switch_axis_position() function has been removed from the cowplot package (the author cited (forthcoming?) native functionality in ggplot2).
I've seen the reference page on secondary axes in ggplot2, however all the examples in that document use scale_y_continuous rather than scale_y_discrete. And, indeed, when I try to use the discrete function, I get the error:
Error in discrete_scale(c("y", "ymin", "ymax", "yend"), "position_d", :
unused argument (sec.axis = <environment>)
Is there anyway to do this with ggplot2? Even a completely hacked solution will suffice for me. Thanks in advance. (MREs below)
library(ggplot2)
# Working continuous plot with 2 axes
ggplot(mtcars, aes(cyl, mpg)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.+10))
# Working discrete plot with 1 axis
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point()
# Broken discrete plot with 2 axes
ggplot(mtcars, aes(cyl, as.factor(mpg))) +
geom_point() +
scale_y_discrete(sec.axis = sec_axis(~.+10))
Take your discrete factor and represent it numerically. Then you can mirror it and relabel the ticks to be the factor levels instead of numbers.
library(ggplot2)
irislabs1 <- levels(iris$Species)
irislabs2 <- c("foo", "bar", "buzz")
ggplot(iris, aes(Sepal.Length, as.numeric(Species))) +
geom_point() +
scale_y_continuous(breaks = 1:length(irislabs1),
labels = irislabs1,
sec.axis = sec_axis(~.,
breaks = 1:length(irislabs2),
labels = irislabs2))
Then fiddle with the expand = argument in the scale as needed to more closely imitate the default discrete scale.

Adding legend to a single line chart using ggplot

I just try to make a line chart and add a legend to it using ggplot in R. The following is my code.
ggplot(mtcars, aes(x=mpg, y=wt)) + geom_line(stat = "identity") + scale_fill_identity(name = "", guide = "legend", labels = c("myLegend"))
and I got the following:
The legend is not shown in the plot and what I want is the following:
which I plot using Matlab. Could anyone tell me how to do it in R? Thank you so much!!
You plot is not showing a legend, because there are no aesthetics mapped to the line. Basically, ggplot sees no reason to add a legend as there's only one line.
A simple way to get a legend is to map the line type to a character string:
ggplot(mtcars, aes(x=mpg, y=wt, lty = 'MyLegend')) + geom_line()
You can have a look at ?scale_linetype for information on how to modify tthat legend.
For example, use + scale_linetype('MyLegendTitle') to change the legend title.

Resources