increase legend font size ggplot2
You can use theme_get()
to display the possible options for theme.
You can control the legend font size using:
+ theme(legend.text=element_text(size=X))
replacing X with the desired size.
How to change the size of legend text in ggplot2?
Remove label.theme = element_text(angle = 0)
from your guides seems to fix this.
guides(color=guide_legend(override.aes=list(shape=c(NA,16, 16, 16, 16),
linetype=c(3,0, 0, 0, 0)),
direction = "vertical", title.position = "top",
label.position="right", label.hjust = 0, label.vjust = 0.5
#,label.theme = element_text(angle = 0)
)) +
theme(legend.text=element_text(size=10))
Change size of axes title and labels in ggplot2
You can change axis text and label size with arguments axis.text=
and axis.title=
in function theme()
. If you need, for example, change only x axis title size, then use axis.title.x=
.
g+theme(axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"))
There is good examples about setting of different theme()
parameters in ggplot2 page.
R: How to control font size of legend map in plotly/ggplotly?
You can change the font size of the legend using legend.text
in your theme
like this:
library(dplyr)
library(ggplot2)
library(plotly)
p <- iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme(legend.text=element_text(size=3))
ggplotly(p)
p <- iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme(legend.text=element_text(size=15))
ggplotly(p)
Created on 2022-07-15 by the reprex package (v2.0.1)
Modify the size of each legend icon in ggplot2
One option to create this kind of legend would be to make it as a second plot and glue it to the main plot using e.g. patchwork
.
Note: Especially with a map as the main plot and the export size if any, this approach requires some fiddling to position the legend, e.g. in my code below a added a helper row to the patchwork design to shift the legend upwards.
UPDATE: Update the code to include the counts in the labels. Added a second approach to make the legend using geom_col
and a separate dataframe.
library(dplyr, warn = FALSE)
library(usmap)
library(ggplot2)
library(patchwork)
# Make example data
set.seed(123)
cat1 <- c(1500, 4001, 6001, 20001)
cat2 <- c(4000, 6000, 2000, 40000)
n = c(7, 12, 6, 25)
funding_cat <- paste0("$", cat1, " - $", cat2, " (n=", n, ")")
funding_cat <- factor(funding_cat, levels = rev(funding_cat))
grant_sh <- utils::read.csv(system.file("extdata", "us_states_centroids.csv", package = "usmapdata"))
grant_sh$funding_cat = sample(funding_cat, 51, replace = TRUE, prob = n / sum(n))
# Make legend plot
grant_sh_legend <- data.frame(
funding_cat = funding_cat,
n = c(7, 12, 6, 25)
)
legend <- ggplot(grant_sh, aes(y = funding_cat, fill = funding_cat)) +
geom_bar(width = .6) +
scale_y_discrete(position = "right") +
scale_fill_manual(
values = c('#D4148C',
'#049CFC',
'#1C8474',
'#7703fC')
) +
theme_void() +
theme(axis.text.y = element_text(hjust = 0),
plot.title = element_text(size = rel(1))) +
guides(fill = "none") +
labs(title = "Map Key")
map <- plot_usmap(regions = "states",
labels = TRUE) +
geom_point(data = grant_sh,
size = 5,
aes(x = x,
y = y,
color = funding_cat)) +
theme(
legend.position = 'none',
plot.title = element_text(size = 22),
plot.subtitle = element_text(size = 16)
) + #+
scale_color_manual(
values = c('#D4148C', # pink muesaum
'#049CFC', #library,blue
'#1C8474',
'#7703fC'),
name = "Map Key",
labels = c(
'$1,500 - $4,000 (n = 7)',
'$4,001 - $6,000 (n = 12)',
'$6,001 - $20,000 (n = 6)',
'$20,001 - $40,000 (n = 25)'
)
) +
guides(colour = guide_legend(override.aes = list(size = 3)))
# Glue together
design <- "
#B
AB
#B
"
legend + map + plot_layout(design = design, heights = c(5, 1, 1), widths = c(1, 10))
Using geom_bar
the counts are computed from your dataset grant_sh
. A second option would be to compute the counts manually or use a manually created dataframe and then use geom_col
for the legend plot:
grant_sh_legend <- data.frame(
funding_cat = funding_cat,
n = c(7, 12, 6, 25)
)
legend <- ggplot(grant_sh, aes(y = funding_cat, n = n, fill = funding_cat)) +
geom_col(width = .6) +
scale_y_discrete(position = "right") +
scale_fill_manual(
values = c('#D4148C',
'#049CFC',
'#1C8474',
'#7703fC')
) +
theme_void() +
theme(axis.text.y = element_text(hjust = 0),
plot.title = element_text(size = rel(1))) +
guides(fill = "none") +
labs(title = "Map Key")
Change font-size in legend
First approach:
Try to set the font size before to plot the legend.
x <- y <- rnorm(100, 0, 1)
plot(x, y, type = "n")
## here you set the font size default to `x`, in this example 0.5
## save defaults in `op`
op <- par(cex = 0.5)
legend("topright", legend = "foo legend", pch = 1, bty = "n")
## here you set cexto 1.5
## save new defaults in `op`
op <- par(cex = 1.5)
legend("topright", legend = "foo legend", pch = 1, bty = "n")
Second approach:
Holding the pt.cex
parameter to 1, while trying different values for cex
inside the legend call. Remember to delete op
.
x <- rnorm(100, 10, 4)
y <- rnorm(100, 10, 4)
plot(x, y, type = "n")
## I tried to feed cex with 1.5 and 0.5. The font size changes while the points remain unchanged.
legend("topleft", "Legend", cex=0.5, pch=1, pt.cex = 1)
Changing font size and direction of axes text in ggplot2
Use theme()
:
d <- data.frame(x=gl(10, 1, 10, labels=paste("long text label ", letters[1:10])), y=rnorm(10))
ggplot(d, aes(x=x, y=y)) + geom_point() +
theme(text = element_text(size=20),
axis.text.x = element_text(angle=90, hjust=1))
#vjust adjust the vertical justification of the labels, which is often useful
There's lots of good information about how to format your ggplots here. You can see a full list of parameters you can modify (basically, all of them) using ?theme
.
Increase font size of inplot mean for ggbetweenstats (R) but not write it over the plots
You can use the nudge_x
or nudge_y
argument to specify where the label should be. Maybe you want something like:
library(ggstatsplot)
ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
title = "Distribution of sepal length across Iris species",
centrality.label.args = list(size = 7, nudge_y = 1.5)
)
Output:
Keep in mind that because of the really big font size, it is hard to place the labels in a clean way. You can manually change the argument if you want.
Adjust position and font size of legend title in ggplot2
Yes, using a feature of the new 0.9.0 version, guides
:
p + guides(colour = guide_legend(title.hjust = 0.5))
You can read about guides
here.
Related Topics
Options for Deploying R Models in Production
Return Row Number(S) for a Particular Value in a Column in a Dataframe
Add Author Affiliation in R Markdown Beamer Presentation
What Is a Neat Command Line Equivalent to Rstudio's Knit HTML
Plotly as Png in Knitr/Rmarkdown
Boxplot Schmoxplot: How to Plot Means and Standard Errors Conditioned by a Factor in R
Removal of Constant Columns in R
Installing Rmysql in Mavericks
Create Top-To-Bottom Fade/Gradient Geom_Density in Ggplot2
Xgboost in R: How Does Xgb.Cv Pass the Optimal Parameters into Xgb.Train
How to Remove "Rows" with a Na Value
Loop Over Rows of Dataframe Applying Function with If-Statement
Label Minimum and Maximum of Scale Fill Gradient Legend with Text: Ggplot2