How to reposition the axis title in ggplot2? - r

I would like to reposition (e.g., move up by 10px) the x-axis label of this chart
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)
I thought I could use vjust but as mentioned in this github issue, the only way to shift the x-label vertically is by using margin:
theme(axis.text.x = element_text(margin=margin(0,0,-10,0)))
However, margin produces something very different than a simple repositioning of the x-title, in that it resizes the chart too. I could I simply move the x-label instead?

You could do it this way
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)+
theme(axis.title.x=element_text(margin=margin(-5,0,0,0)))
Just change the numbers until you get a satisfying result.

Related

How to adjust the horizontal placement of x-axis-labels in ggplot2 (for a continuous x-axis) [duplicate]

I have a ggplot in R that displays three labels on the x-axis. By default, these labels are centered. However, I am searching for a way to left-align the first label, center the second, and right-align the third label. The linked photo shows the exact result that I try to replicate. Any suggestions on how I can do this in R?
The label alignment I try to replicate.
qplot(100*1:3,1) + scale_x_continuous(breaks=100*c(1,2,3)) +
theme(axis.text.x = element_text(hjust=c(0,0.5,1)))

Can I place my ggplot2 legend right beside the horizontal axis label?

In order to save as much as white space as I can, I would like to place the legend entry at the same height as the horizontal axis label. Can this be done and if so, how?
Here's a plot illustrating what I am hoping to achieve, with current and hoped-for legend position illustrated using a (manually added) green box.
I currently place the legend using theme(legend.position=c(0.87,0.1)) (noting that the exact coordinates are not relevant). Ideally, this route would allow for values outside of the [0,1] domain but it appears not to allow for that.
theme(legend.position="bottom") places the legend well outside of the plotting area, thus taking up more white space than I am willing to spare.
You just might have to play around with negative values regarding the y-coordinates of your legend.position-vector.
Here's an example:
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color=Species))+
geom_line()+
facet_wrap(~Species)+
theme(legend.position=c(0.87,-0.01))
Note the -0.01 value. Is this what you're looking for?

How to preserve plot sizes when stacking with common x-axes in ggarrange?

I am trying to stack plots with common x- and y-axes in ggplot. What I want to do is have only the bottom plot show the x-axis labels and titles. But I've never been able to figure out how to do this cleanly in ggplot2 without having the bottom plot be squished by carrying the virtue of the x-axis labels/title. There must be an easy way to do this- everyone wants to stack graphs, right?!
I'm currently trying with ggarrange. Example code below. Note that the bottom plot gets compressed vertically because it has the tick and axis labels. I could just have the top two have white font labels/title, but then there is an unseemly amount of margin space between the three if you use that hack.
I'm definitely open to packages other than gpubr, but I am hoping for something not too elaborate that I can use in subsequent situations, as I'm sure I'll encounter this again...
Help, please!! -Ryan
#
require(ggplot2); require(ggpubr)
X=data.frame(seq(as.Date("2001-01-01"),as.Date("2001-12-31"),by='days')); colnames(X)='date'
X$Y1=sample(80:100,size=nrow(X),replace=T)
X$Y2=sample(100:120,size=nrow(X),replace=T)
X$Y3=sample(50:70,size=nrow(X),replace=T)
plot.Y1= ggplot(X, aes(x=date,y=Y1))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y2= ggplot(X, aes(x=date,y=Y2))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y3= ggplot(X, aes(x=date,y=Y3))+
geom_line()+lims(y=c(50,150))
x11(10,8)
ggarrange(plot.Y1,plot.Y2,plot.Y3,nrow=3,ncol=1)
Bottom plot is squished!
try this,
egg::ggarrange(plot.Y1,plot.Y2,plot.Y3,ncol=1)

ggmosaic plot with asymmetric offset

I have a mosaic plot generated by ggmosaic:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3))) +
geom_mosaic(aes(weight=a3, x=product(a1,a2), fill=a1))
I would like to widen the space between the vertical bars without changing the height of the space between the stacked columns:
I've tried using the offset parameter, but it seems to work on both dimensions, and can't isolate just one. An answer using vanilla ggplot is acceptable, but a ggmosaic-only solution is preferred.
A less-than-ideal workaround using using geom_bar:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3)), aes(width=c(.4,.6,.4,.6)))+
geom_bar(aes(x=a2, y=-a3, fill=a1), position = "fill", stat = "identity")

How to add two legends to bottom corners of plot (ggplot)

I have a plot with no x-label. Instead, I want to just have two text boxes on the bottom left (saying "Negative) and bottom right (saying "Positive").
I have my plot object (p), but have tried different ways to achieve what I want, failing each time. For instance, this does not create the text box in the bottom left of the plot.
p + legend.title("Negative") + legend.position(c("bottom","left"))
Any advice is appreciated!
I think you are looking for the annotate function with which you can add text to a plot:
p + annotate("text", x = x-position-value, y = y-position-value, label = "Negative")
You can also add rectangles, lines and pointranges with this function. For some further details about how to use this function, see the official documentation

Resources