ggplot2 increase font size of output labels - r

I am working with ggplot2 to create plots and I want to increase the size of the labels from the output, so not the labels added manually.
I tried to use cex put this works only partially as it only increases the count size in the plot but not the x or y labels
ggplot(dat, aes(x = Gender, fill = gen))+
geom_bar(position = "dodge")+
scale_fill_manual(values = c("#40E0D0","#DE3163"))+
labs(y="All", fill ="Gender:")+
scale_y_continuous(breaks = seq(0,550,50))+
ggtitle("Gender")+
theme(axis.title.x = element_text(size =20, face = "bold"))+
theme(axis.title.y = element_text(size = 20, face = "bold", angle = 90))+
theme(plot.title = element_text(size = 30, face="bold"), legend.position = "bottom")+
geom_text(aes(label=stat(count), cex = 6), stat = "count",
position=position_dodge(width = 1),vjust= -0.2, cex = 6)+
theme(legend.title = element_text(size = 20))+
theme(legend.text = element_text(size = 20))
``

Related

Facet_Wrap title overlapping with y-axis in ggplotly?

I am currently using a facet_wrap to knit data (html) to an Rmarkdown document. I am looking to use facet_wrap and ggplot to show this data.
This is how the data looks just in ggplot (not plotly):
However, when I use plotly the titles of each plot intersect with the y-axis making interpretability difficult
Here is the code to the first plot not using plotly
plot <- ggplot(dataframe, aes(x = week, y = measure))+
geom_line(size = 1.25, aes(color = "orange"))+
geom_point(size = 1.50)+
theme_economist()+
geom_smooth(method = 'auto', se = FALSE, size = .50)+
scale_x_continuous(breaks=seq(0,13,1))+
scale_y_continuous(labels = scales::dollar_format(scale = .0001, suffix = "K"))+
facet_wrap(vars(category), scales = "free", ncol = 3, nrow = 6)+
theme(legend.position = "none")+
ggtitle('Title')+
theme(
panel.grid.major = element_line(linetype = "dotted"),
axis.title.x = element_text( size = 10, margin=margin(30,0,0,0)),
axis.title.y = element_text( size = 10, margin=margin(0,30,0,0)),
axis.text = element_text( size = 10),
plot.title = element_text( size = 14, margin=margin(0,0,35,0), hjust = 0.5),
panel.background = element_rect(fill = NA),
strip.text = element_text(size=10)
)
Then here is what I do to conver it to plotly
ggplotly(plot)
Some random data:
require(stats)
r <- function(x) {abs(as.numeric(arima.sim(n=x, list(order=c(1,0,0),ar=0.95)))+10)}
dataframe = data.frame(week = rep(1:13,6), measure = r(13*6), category = rep(as.character(1:6),each=13))
Although I was not able to reproduce the overlapping y-axis labels, you can set the margins between the faceted plots by setting panel.margin.y and panel.margin.x inside the theme():
plot <- ggplot(dataframe, aes(x = week, y = measure))+
geom_line(size = 1.25, aes(color = "orange"))+
geom_point(size = 1.50)+
theme_economist() +
geom_smooth(method = 'auto', se = FALSE, size = .50)+
scale_x_continuous(breaks=seq(0,13,1))+
scale_y_continuous(labels = scales::dollar_format(scale = .0001, suffix = "K"))+
facet_wrap(vars(category), scales = "free", ncol = 3, nrow = 6)+
theme(legend.position = "none")+
ggtitle('Title')+
theme(
panel.grid.major = element_line(linetype = "dotted"),
axis.title.x = element_text( size = 10, margin=margin(30,0,0,0)),
axis.title.y = element_text( size = 10, margin=margin(0,30,0,0)),
axis.text = element_text( size = 10),
plot.title = element_text( size = 14, margin=margin(0,0,35,0), hjust = 0.5),
panel.background = element_rect(fill = NA),
strip.text = element_text(size=10), panel.margin.x = unit(1,"lines"), panel.margin.y = unit(0,"lines")
)
ggplotly(plot)
You can fine tune the margins to suit your plot.
Before:
> ggplotly(plot)
After:
> ggplotly(plot)

How to rearrange legend position from horizontal (1x4) to 2x2 in ggplot /R?

Please, find My script below.
I have produced this plot
However, for graphic purposes, I would like to rearrange the horizontal legend to be presented as 2x2. I have manually produced an example of what it should look like in Photoshop:
Unfortunately, I have not been able to locate any clear instructions.
My script
ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
geom_line(aes(y = yhat, col = model),size=1) +
ggtitle("Lymph node yield") +
geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +
geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +
scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +
scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +
scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25\nas reference","50", "75", "100")) +
scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +
theme(axis.text.x = element_text(color = "grey20", size =11),
axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
axis.text.y = element_text(color = "grey20", size = 11),
axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
legend.key = element_rect(fill = "white"),
plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")
Using the dataset data(iris),part of your code and guides(col = guide_legend(nrow=2, ncol=2)), I have achieved the following:
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length))+
geom_point(aes(y = Sepal.Width, col = Species)) +
theme(axis.text.x = element_text(color = "grey20", size =11),
axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
axis.text.y = element_text(color = "grey20", size = 11),
axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
legend.key = element_rect(fill = "white"),
plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")+
guides(col = guide_legend(title.position = "top",nrow = 2))
Be aware that if you do fill, then in guides() you should also use fill. I have used col since it is what creates my legend.
You can try guides
Let's say you store your plot in p, you could write:
p + guides(col = guide_legend(nrow=2, ncol=2))
The solution was
p + guides(fill=guide_legend(nrow=2,byrow=TRUE))
When storing my ggplot in p

R: Can not change the font size of title of graph, legend, and axes?

I would like to change the font size of title, legend, and axes of a graph, I changed it using theme but it doesn't have any affect :
ggplot( data = df.m, aes(x=variable, y=value , na.rm = TRUE)) +
geom_boxplot(aes(fill=label), outlier.size = 0.5) +
labs(x="",y="Values",fill="") +
theme(text = element_text(size=100), axis.text.x = element_text(angle = 90, hjust = 1, colour = 'black'),
axis.title=element_text(size=100) , text=element_text(size=100),
legend.text = element_text(size = 100),
axis.text.x = element_text(size = 100), axis.text.y = element_text(size = 100), legend.position = "top") + theme_bw()
Why it does not change the font size at all?

Adjusting white space between titles and the edge of the plot

I want to create space between the titles (the axis title and the plot title) and the edge of the plot. I tried vjust on axis.title and plot.title with no luck. Nothing really changed in the plot when I tried various values for vjust. I also tried plot.margin, but nothing seemed to happen with it either.
Data:
data = data.frame(Category = c(0,1), value = c(40000, 120000))
data$Category = factor(data$Category, levels = c(0,1), labels = c("One-time", "Repeat"))
Plot:
p = ggplot(data, aes(Category, Value)) +
geom_bar(stat = "identity", width = 0.5, position=position_dodge(width=0.9)) +
geom_text(aes(label=Value), position=position_dodge(width=0.9), family = "mono", vjust=-0.5) +
ggtitle("Title") +
scale_y_continuous(expand = c(0,0), limits = c(0,150000)) +
scale_x_discrete(expand = c(0,0), limits = c("One-time", "Repeat")) +
xlab("X axis Title") +
ylab("Y axis Title")
Theme:
p + theme(
panel.grid.major = element_line(linetype = "blank"),
panel.grid.minor = element_line(linetype = "blank"),
axis.title = element_text(family = "sans", size = 15),
axis.text = element_text(family = "mono", size = 12),
plot.title = element_text(family = "sans", size = 18),
panel.background = element_rect(fill = NA)
)
You want to do this using margin
p + theme(
panel.grid.major = element_line(linetype = "blank"),
panel.grid.minor = element_line(linetype = "blank"),
axis.title.x = element_text(family = "sans", size = 15, margin=margin(30,0,0,0)),
axis.title.y = element_text(family = "sans", size = 15, margin=margin(0,30,0,0)),
axis.text = element_text(family = "mono", size = 12),
plot.title = element_text(family = "sans", size = 18, margin=margin(0,0,30,0)),
panel.background = element_rect(fill = NA)
)
Note that margin requires a four inputs and they specify the space in the order top,right,bottom,left. Also note I'm using the developmental ggplot2 version so my title default is left justified. 'hjust' and 'vjust' worked in older versions of ggplot2.
You can also give a "-ve" value to margin argument to pull the title closer to the plot.
gg_1 +
theme(plot.title = element_text(size = 12,
hjust = 0.5,
family = fam_3,
face = "bold",
margin = margin(0,0,-10,0))

Aligning two ggplots with grid.arrange

I would like to have two ggplots aligned and the same x axis length. Somehow it does not work. Here is my code:
#5cm_Four Treatments different color
d1<-ggplot(MyDataMoistWinkler[550:10000,], aes(x=betterDate)) +
geom_line(aes(y=VerticalTillHigh5cm, color="Vertical Till High Disturbance 5cm"))+
geom_line(aes(y=VerticalTillLow5cm, color="Vertical Till Low Disturbance 5cm"))+
geom_line(aes(y=StripST5cm, color="Strip Till in row"))+
geom_line(aes(y=StripNT5cm, color="Strip Till between row"))+
geom_line(aes(y=Disc5cm, color="Double Disc"))+
xlab("Date")+
ylab("Volumetric Water Content [m3/m3]")+
scale_y_continuous(limits = c(0, NA))+
theme(text = element_text(size = 11, colour = "black"))+
theme(legend.title=element_blank())+
#theme(panel.background = element_blank(), panel.grid = element_line( size=.5, colour="black" ) )+
theme(axis.text = element_text(size = 15, colour = "grey"))+# changes axis labels
theme(axis.title = element_text(size = 15))+ # change axis titles
theme(text = element_text(size = 15, colour = "black"))+ # this will change all text size # (except geom_text)
theme(strip.text.x = element_text(size = 15, colour = "black", angle =NULL))+
ggtitle("Moisture in 5cm, in Winkler MB")
d2<-ggplot(MyDataMoistWinkler[550:10000,], aes(x=betterDate, y=AccumulatedRainfall)) +
geom_line(aes(y=AccumulatedRainfall, color="Accumulated Rainfall"))+
xlab("Date ")+
ylab("Accumulated Rainfall [mm]")+
scale_y_continuous(limits = c(0, NA))+
theme(text = element_text(size = 11, colour = "black"))+
theme(legend.title=element_blank())+
#theme(panel.background = element_blank(), panel.grid = element_line( size=.5, colour="black" ) )+
theme(axis.text = element_text(size = 15, colour = "grey"))+# changes axis labels
theme(axis.title = element_text(size = 15))+ # change axis titles
theme(text = element_text(size = 15, colour = "black"))+ # this will change all text size # (except geom_text)
theme(strip.text.x = element_text(size = 15, colour = "black", angle =NULL))+
ggtitle("Precipitation in mm, in Winkler MB")
1st Option: If you want to arrange them next to each other
require(gridExtra)
grid.arrange(d1, d2, ncol=1)
library(grid)
grid.draw(rbind(ggplotGrob(d1), ggplotGrob(d2)))
might work

Resources