Align text inside a plot
While legend() is of course appropriate for legends, there is a general solution for all text. The trick is that the pos option not only sets the position of the text relative to the current location but it also sets justification. Above and Below are center justified. Setting pos to 2 makes the text right justified. When it is set to the right of the position (pos = 4) then it is left justified.
For left justified replace your text code with...
text(1.5, 150, paste("Mean =", round(MyMean, 1), "\nMedian =",
round(MyMedian, 1), "\nStd.Dev =", round(MySd, 1)), pos = 4)
and for right justified...
text(5.0, 150, paste("Mean = ", round(MyMean, 1), "\nMedian = ",
round(MyMedian, 1), "\nStd.Dev = ", round(MySd, 1), sep = ''), pos = 2)
Change alignment of plot text
Alignment (adjustment / justification) of text
is set by the adj
argument. To left-align, set adj = 0
.
From ?text
:
adj
: one or two values in [0, 1] which specify the x (and optionally y) adjustment (‘justification’) of the labels, with 0 for left/bottom, 1 for right/top, and 0.5 for centered. On most devices values outside [0, 1] will also work.
adj
allows adjustment of the text position with respect to(x, y)
. Values of 0, 0.5, and 1 specify that(x, y)
should align with the left/bottom, middle and right/top of the text, respectively. The default is for centered text, i.e.,adj = c(0.5, NA)
. Accurate vertical centering needs character metric information on individual characters which is only available on some devices. Vertical alignment is done slightly differently for character strings and for expressions:adj = c(0,0)
means to left-justify and to align on the baseline for strings but on the bottom of the bounding box for expressions. This also affects vertical centering: for strings the centering excludes any descenders whereas for expressions it includes them. UsingNA
for strings centers them, including descenders.
plot(1:10, 1:10)
text(x = 6, y = 1, "text", adj = 0)
adj = 0
will left-align the text:
text alignment in plot R
I would use expression
only in the expression used, and use the adj
parameter to adjust alignment.
plot(1:10)
text(8,4,c("blah \nblah\n", expression(paste(R^2, "= 0.98"))),col="red", adj = 0)
Additionaly, it seem like you're trying to do some legend. You could use legend
to do it:
legend('bottomright', c('blah', 'blah', expression(paste(R^2, "= 0.98"))))
This would be more readable code, and you can adjust settings according to your needs. Check ?legend
for more information.
How can I align text to bar plots with position_fill in ggplot2?
Using the comments, especially from Axeman, I realised that I can use position_identity()
to get the desired result:
library(tidyverse)
iris %>%
mutate(long_sepal = Sepal.Length > 5.5) %>%
count(Species, long_sepal) %>%
ungroup() %>%
mutate(y_label = if_else(long_sepal, 0.01, 0.99)) %>%
ggplot(aes(x = Species)) +
geom_col(aes(y = n, fill = long_sepal), position = position_fill()) +
geom_text(
mapping = aes(label = n, y = y_label, group = long_sepal),
hjust = "inward",
position = position_identity()
) +
coord_flip()
Created on 2019-03-15 by the reprex package (v0.2.1)
Align text between two vertical lines in plot
Text Alignment With Respect to Centre
Using the 'HorizontalAlignment'
property and setting it to 'center'
may help in achieving the alignment in between the sections divided by the vertical lines. Here I initialize each text annotation to variables Text_1
and Text_2
and set their 'HorizontalAlignment'
properties respectively.
Code Snippet:
Text_1 = text(mean([0 x(1)]),y(2)*.8,'Label_1','FontSize',10);
Text_2 = text(mean([x(1) x(2)]),y(2)*.8,'Label_2','FontSize',10);
set(Text_1,'HorizontalAlignment','center');
set(Text_2,'HorizontalAlignment','center');
Full Script:
%% create figure
x=[2 5]; %spacer var
figure; hold on;
axis([0 10 0 10])
%create lines based on spacer var
line([x(1) x(1)],ylim)
line([x(2) x(2)],ylim)
%silly formatting
grid on
axis square
xticks(0:10); yticks(0:10);
set(findall(gcf,'type','line'),'linewidth',3)
%generate text between vertical lines
y = ylim; %get ylimits
Text_1 = text(mean([0 x(1)]),y(2)*.8,'Label_1','FontSize',10);
Text_2 = text(mean([x(1) x(2)]),y(2)*.8,'Label_2','FontSize',10);
set(Text_1,'HorizontalAlignment','center');
set(Text_2,'HorizontalAlignment','center');
Ran using MATLAB R2019b
Related Topics
Filtering Observations in Dplyr in Combination with Grepl
Three-Way Color Gradient Fill in R
Using Dplyr to Conditionally Replace Values in a Column
Install R Packages from Github Downloading Master.Zip
Colorize Clusters in Dendogram with Ggplot2
Why Are Xs Added to Data Frame Variable Names When Using Read.Csv
Arrange Plots in a Layout Which Cannot Be Achieved by 'Par(Mfrow ='
Adding Total/Subtotal to the Bottom of a Datatable in Shiny
Combining Elements of List of Lists by Index
Correlation Corrplot Configuration
Using Parlapply and Clusterexport Inside a Function
Saving Plot as PDF and Simultaneously Display It in the Window (X11)
How to Remove Multiple Columns in R Dataframe
How to Set Seed for Random Simulations with Foreach and Domc Packages
How to Change Font Size of the Correlation Coefficient in Corrplot
How to Remove Row If It Has a Na Value in One Certain Column