title size for charts.PerformanceSummary - r

I'm not 100% sure if this is just a bug or some other issue but how do you change the font size of the title for outputs of charts.PerformanceSummary() from the PerformanceAnalytics package?
Here is my reproducible example for what i would have hoped would have increased the font size by 6 times what it original was...
require(PerformanceAnalytics)
v <- rnorm(100,0.001,0.003)
charts.PerformanceSummary(xts(v,Sys.Date()-(100:1)),main="random title")
charts.PerformanceSummary(xts(v,Sys.Date()-(100:1)),main="random title", cex.main=6)
The outputs appear to be the same with no change in title size....

Set cex.main before you plot:
par(cex.main = 6)
charts.PerformanceSummary(xts(v,Sys.Date()-(100:1)),main="random title")
Update
Another alternative that you might try (but one which requires a bit of testing first) is to use the title() function.
# Set `main` to `""`
charts.PerformanceSummary(xts(v,Sys.Date()-(100:1)), main = "")
# You'll have to experiment with the best combination of `line` and `cex.main`
title(main="random title", line = -2, outer = TRUE, cex.main=3)

Related

Strange blue subtitle when plotting objects from the RandomFields package

Theplot method for RFspatialGridDataFrame outputs a weird blue "subtitle" when passing main or axis label arguments to the plot function.
install.packages("RandomFields")
library(RandomFields)
model <- RPbernoulli(RMexp(scale = 3), threshold = 1)
set.seed(100)
simulation <- RFsimulate(model, x = 1:138, y = 1:74)
plot(simulation, main = "This is an example title")
The following is a screeshot of the output
Strangely enough, this appears to be a feature, as running other example for the RandomFields documentation shows.
Is there any way of not outputting this blue repeated title? I have tried fiddling with other graphical arguments (such as setting legend = F) but the behavior does not change.
I have contacted the maintainers of the RandomFields package, and it appears to be a setting in RFoptions.
By setting RFoptions(grPrintlevel = 0) before the plots, this behavior can be avoided. More information can be found under ?RFoptions in section 10 regarding graphics. The relevant part regarding grPrintlevel reads
grPrintlevel: integer values 0, 1, 2; only relevant when simulations are
plotted. The higher the more text is shown in the plot.
Default: 1.

Automatically adjust plot title width using ggplot

I am fairly new to R/ggplot2 and still learning on the go. Hopefully I am not missing something obvious!
I am trying to create several different plots using ggplot2 that I am layouting using the function plot_grid from the cowplot package to make the plots visible side by side and add plot numeration and captions. The problem is that if the generated plots are displayed in a small window or I have many plots beside one another then the titles of the two plots sometimes overlap. To solve this problem I tried to automatically insert line breaks in my too long titles using code I found in another thread since I wanted the text size of the titles to stay constant.
Using the following code I can easily automatically insert the necessary line breaks to make my title a specific width, but the problem is that I always need to enter a numeric value for the width. Depending on the number of plots I am inserting this value would of course change. I could of course go through my code and manually set the width for each set of plots until it is the correct value, but I was hoping to automate this process so that the title width is adjusted automatically to match the width of the x-axis. Is there anyway to implement this in R?
#automatically line break and add titles
myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
plot +
labs(title = paste(strwrap(plot.title, width = 50), collapse = "\n"),
x = x.title,
y = y.title)
}
# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point()
title <- "This is a title that is very long and does not display nicely"
myplot_theme1(plot1, plot.title = title)
My test plot
I have tried searching but I haven't found any solutions that seem to address what I am looking for. The only solution I did find that looked promising was based on the package gridDebug. This packages doesn't seem to be supported by my operating system anymore though (macOS Sierra Version 10.12.6) since when I try to install it I get the following error message:
Warning in install.packages: dependencies ‘graph’, ‘Rgraphviz’ are not available
And on the CRAN package documentation it states that the package is not even available for macOS El Capitan which was my previous operating system. If someone knows what is causing this issue so that I could try the solution from the above thread that would of course be great as well.
One idea (but perhaps not an ideal solution) is to adjust the size of text based on the number of characters in the title. You can adjust ggplot properties using theme and in this case you want to adjust plot.title (the theme property, not your variable). plot.title has elements size and horizontal justification hjust, the latter is in range [0,1].
# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point()
title1 <- "This is a title that is very long and does not display nicely"
title2 <- "I'm an even longer sentence just test me out and see if I display the way you want or you'll be sorry"
myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
plot +
labs(title = plot.title,
x = x.title,
y = y.title) +
theme(plot.title = element_text(size=800/nchar(plot.title), hjust=0.5)) # 800 is arbitrarily chosen
}
myplot_theme1(plot1, plot.title = title1)
myplot_theme1(plot1, plot.title = title2)

Define margins of PDF used for boxplot rendering

When I render a boxplot on a PDF device in R there is a large white space besides the graph, especially at the top that i intent to reduce.
My script is basically just:
data <- read.csv("input.csv")
pdf(file="output.pdf", width=4, height=5)
boxplot(data, xlab="input graphs", ylab="vertex count")
This leads to something like:
where the grey outline indicates the end of the document.
I tried to use the par attributes "mar" and "mai" as described in https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/par.html but it had no effect.
boxplot(data, mar=c(0,0,0,0=, mai=c(0,0,0,0))
Do you have an advice how I can gain whitespace control? I want to have zero outer whitespace as the generated graph will be used in a Latex environment that provides sufficient spacings on its own. I am using Ubuntu as OS.
Define mar right after pdf. Try this as an example
pdf(file = "test.pdf", width = 5, height = 5)
par(mar = c(5, 5, 0.05, 0.05))
set.seed(42)
plot(rnorm(20))
dev.off()

How to avoid plotting of the title in SOM's plot.kohonen function

I am using plot function from the kohonen package in R. The function inherently produces title called "property plot" or whatever different plot I specify. I can change the name and customize it but I am not able to alter its size.
I want to either remove the inherent title plotting or increase the size of the title.
I have attached a plot in which there are two different titles. The smaller one is the inherent one and the larger one is added through title function. The code for the same is also given
plot(knime.model, type = "property", property = unscaled, main = colnames(knime.model$data)[5], , cex = 2, font.main= 4 palette.name=coolBlueHotRed, heatkey = F, ncolors = length(unique(data[,5])))
title(colnames(knime.model$data)[5], cex.main = 3)

ggplot Multiline Title with Different Font Sizes

<SlightlyLookingAway>I am attempting to reproduce an excel plot in R.</SlightlyLookingAway> The Excel plot has a two line title. I know how to handle this by putting a '\n' in the title text. What I do not know how to handle is that the first line has a larger font size than the second row of the title... I have done some google searching and have come up with a general lack of response.
I realize that I might be able to cobble something together with an annotation of some kind but that seemed like a kludge. If that is the only answer then it is, but I wanted to ask the community first.
Any ideas?
It looks as though I have found a hacked solution which gets the job done but does not offer a lot of flexability. The idea is to put in a math expression using the atop() command along with the bold() and scriptstyle() functions.
myplot +
ggtitle(expression(atop(bold("This is the Top Line"), scriptstyle("This is the second line")))) +
theme(plot.title = element_text(size = 20))
If you know of a better solution with more control over the line spacing and even being able to adjust the font face, please let me know...
try this,
library(gridExtra)
titleGrob <- function(x=c("First line", "second line"), size=10, ...){
n <- length(x)
size <- rep(size, length.out=n)
one_label <- function(x, size, ...)
textGrob(x, gp=gpar(fontsize=size), ...)
lg <- mapply(one_label, x=x, size=size, ..., SIMPLIFY=FALSE)
wg <- lapply(lg, grobWidth)
hg <- lapply(lg, grobHeight)
widths <- do.call(unit.c, wg)
heights <- do.call(unit.c, hg)
maxwidth <- max(widths)
g <- frameGrob(layout = grid.layout(n, 1, width=maxwidth, height=heights) )
for(ii in seq_along(lg))
g <- placeGrob(g, lg[[ii]], row=ii)
g
}
grid.newpage()
g <- titleGrob(size=c(18,12))
grid.arrange(qplot(1,1), top=g)
To perfectly center everything (which \n will not do), adapt every size of text whatever the number of lines and at the same time being able to adjust the interlinear space, use this instead:
e.g. for smaller to larger text size
ggtitle(expression(atop(scriptscriptstyle("whateverline1"),atop(scriptstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))
Then use labeller=label_parsed
This also works for facet_grid, xlab and ylab
Note the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle or textstyle depending on your needs and of course use element_text(size=whatevernumber) in the theme section

Resources