How does R decide the x and y axis of mosaicplot - r

I am curious how R decides x and y axis of mosaic plot?
data(HairEyeColor)
mosaicplot(HairEyeColor)
It puts the "Hair" at x-axis and "Eye" at the y-axis and break the Sex.
Is it predetermined?
What will happen if we have more than 5 variables and do the mosaic plot?

See: Mosaic Plot vignette
xlab, ylab: x- and y-axis labels used for the plot; by default, the
first and second element of names(dimnames(X)) (i.e., the name of the
first and second variable in X).

Related

How to align y axis by 0 between plots with plot_grid function in R?

I made several weekley plots of different gaseous compounds (BVOCs) with ggplot and I put them together with plot_grid function. Obviously compounds have different scales and Y axis are not aligned.
I wish align them by zero on Y axis.
I think that I can avoid to share the dataset and the single plots code, because the point is on plot_grid function that put them together.
Here the plot_grid function that I used:
plot_grid(metmax,acetalmax,formicmax,acetmax,nrow = 4,align = "hv",rel_widths= c(1,1,1,1),rel_heights = c(1.2,1.2,1.2,1.2))
Here an example of how appear my final plot with Y axis out of phase.
If I understand it correctly, you want the line at y=0 to appear at the same height in all plots. That would require them all to have the same range and, by consequence, the same scale.
You can add to your ggplot() call the following:
+ ylim(min_value, max_value)
min_value and max_value can be calculated by the script by looking at the range of values occurring in your plot data.
https://ggplot2.tidyverse.org/reference/lims.html

Beeswarm with logarithmic X axis

I am trying to plot a "beeswarm" which is essentially a single-dimensional scatterplot
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
What I want to achieve is logarithmic transformation of the X axis.
Obviously, I can do beeswarm(log(breast$time_survival),horizontal=TRUE,method="hex") but it plots logarithmically transformed data and X axis no longer reperesents survival time numerically. Is there a way to directly affect the X axis? In regular scatterplot, I would do plot(breast$time_survival,log="x") but not sure how to behave with beeswarm
option for beeswarm is log=TRUE, not log="x"
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE, log=T)

R: why is boxplot(x,log="y") different from boxplot(log(x))?

delme <- exp(rnorm(1000,1.5,0.3))
boxplot(delme,log="y")
boxplot(log10(delme))
Why are the whiskers different in this 2 plots?
Thanks
Agus
I would say that in your first plot you just changed the y axis to log, so the values you plot still range between 1 and 10. In this plot the y axis is a log scale. The whiskers on this axis look different because the space between each "tick" (ie axis break) is not constant (there is more space between 2 and 4 than between 10 and 8)
In the second plot, you take the log of the values then plot them, so they range from .2 to 1, and are plotted with a linear y axis.
Look at the summary for both of the normal and log transformed dataframes

Set ticks margin on one axis (ggplot2)

When plotting graphs with categorical variables (such as boxplots) with long names, the names have to be shifted using the theme command in ggplot2, then the distance between the axis ticks and the text can be set as well yet this distance is reflected on both axis when it is some time only necessary on one axis. Below some sample code:
df<-data.frame(X=rnorm(50,0,10),Y=c(rep("Some Text",25),rep("Some Larger Text That Takes Space",25)))
#classical boxplots
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45))
#the x axis labels need to be shifted downwards
ggplot(df,aes(x=Y,y=X))+geom_boxplot()+theme(axis.text=element_text(size=20),axis.text.x=element_text(angle=45),axis.ticks.margin=unit(4,"cm"))
#now they are shifted but there is unnecessary space on the y-axis
How can we set axis.ticks.margin to act on only one axis?
Try this for example :
library(grid)
axis.ticks.margin=unit(c(4,-4),'cm'))
So, the ggplot2 call becomes:
ggplot(df,aes(x=Y,y=X))+
geom_boxplot()+
theme(axis.text=element_text(size=20),
axis.text.x=element_text(angle=45),
axis.ticks.margin=unit(c(4,-4),'cm'))

How to plot density of two datasets on same scale in one figure?

How to plot the density of a single column dataset as dots? For example
x <- c(1:40)
On the same plot using the same scale of the x-axis and y-axis, how to add another data set as line format which represent the density of another data that represents the equation of
y = exp(-x)
to the plot?
The equation is corrected to be y = exp(-x).
So, by doing plot(density(x)) or plot(density(y)), I got two separated figures. How to add them in the same axis and using dots for x, smoothed line for y?
You can add a line to a plot with the lines() function. Your code, modified to do what you asked for, is the following:
x <- 1:40
y <- exp(-x)
plot(density(x), type = "p")
lines(density(y))
Note that we specified the plot to give us points with the type parameter and then added the density curve for y with lines. The help pages for ?plot, ?par, ?lines would be some insightful reading. Also, check out the R Graph Gallery to view some more sophisticated graphs that generally have the source code attached to them.

Resources