Insert function variable into graph title
You should use paste
instead of c
:
plot(..., main=paste("Jaccard vs. Tsim for depths", min.depth, "to",max.depth,"m", sep=" "))
With c
you create a vector of strings (hence the stacking), with paste
you concatenate them into one single string.
How to name a graph's title using the variable name supplied to as function argument in R?
Just use substitute
labs(title = substitute(system))
How to add a variable to Python plt.title?
You can change a value in a string by using %
. Documentation can be found here.
For example:
num = 2
print "1 + 1 = %i" % num # i represents an integer
This will output:
1 + 1 = 2
You can also do this with floats and you can choose how many decimal place it will print:
num = 2.000
print "1.000 + 1.000 = %1.3f" % num # f represents a float
gives:
1.000 + 1.000 = 2.000
Using this in your example to update t
in the figure title:
plt.figure(1)
plt.ylabel('y')
plt.xlabel('x')
for t in xrange(50,61):
plt.title('f model: T=%i' %t)
for i in xrange(4,10):
plt.plot(1.0/i,i**2,'ro')
plt.legend
plt.show()
Adding input variables to plot title/legend in Python
Use string formatting with the .format()
method:
plt.text(0.1, 2.8, "The gradient is {}, the intercept is {}".format(m, c))
Where m
and c
are the variables you want to substitute in.
You can directly write the variables like this in Python 3.6+ if you prefix the string with an f
whcih denotes a formatted string literal:
f"the gradient is {m}, the intercept is {c}"
R plot title involving a subscript and value of a variable
I don't believe you can paste an expression to a variable (paste/print/cat/bquote/etc). As a workaround, you could use the "Fbelow=" expression as the title and use mtext to insert the value of MyVar, e.g.
MyVar<-0.23
plot(mtcars[2:3], main = expression('F'[below]*'='))
mtext(text = MyVar, side = 3, adj = 0.625, padj = -1.75, cex = 1.5)
Obviously this isn't ideal, but unless someone else has a clever way of solving your issue this will at least give you a potential option
Pasting value into R plot title
Answer by @MarcoSandri is correct. The correct way of using the example codes you have written are,
title(main= paste("My plot \n mean =", mean(x)))
and
avg <- mean(x)
title(paste("My Plot \n Mean = ", avg))
Related Topics
How to Plot Bars and One Line on Two Y-Axes in the Same Chart, with R-Ggplot
Write.Csv() a List of Unequally Sized Data.Frames
Using Grep to Subset Rows from a Data.Table, Comparing Row Content
How to Replace Multiple Strings with the Same in R
Reversing Y-Axis in an Individual Ggplot Facet
Results Transposed with R Apply
Aggregating Unique Values in Columns to Single Dataframe "Cell"
How to Label Histogram Bars with Data Values or Percents in R
Mutate Multiple/Consecutive Columns (With Dplyr or Base R)
Missing Data When Supplying a Dual-Axis--Multiple-Traces to Subplot
Fastest Way to Sort Each Row of a Large Matrix in R
Read CSV with Two Headers into a Data.Frame
How to Read a Text File into Gnu R with a Multiple-Byte Separator
How to Adapt a Latex Beamer Theme to Apply It in an Rmarkdown::Beamer_Presentation