How to split the label in R for a plot - r

I am trying to plot CART model that I have built,the node label is too long and want to split it into two lines. I have used gsub() to split it but I want to split the next line too.
here is the code.
model=rpart(Train$service_date_max_count~ injury_type_cd+specialty_type_cd
+state+age+sex+pedestrian_yn+vehicle_driver_yn+vehicle_passenger_yn
+marital_status+category+extesio, data=Train,method ="anova")
split.fun <- function(x, labs, digits, varlen, faclen)
{
gsub(" = ", ":\n", labs)
}
fancyRpartPlot(model , palettes = 'Oranges' ,tweak =0.8, split.fun=split.fun)
I have attached a snippet showing how the plot looks

Related

R multi boxplot in one graph with value (quantile)

How to create multiple boxplot with value shown in R ?
Now I'm using this code
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
I tried to use this
boxplot(Data_frame[ ,2] ~ Data_frame[ ,3], )
text(y=fivenum(Data_frame$x), labels =fivenum(Data_frame$x), x=1.25)
But only first boxplot have value. How to show value in all boxplot in one graph.
Thank you so much!
As far as I understand your question (it is not clear how the fivenum summary should be displayed) here is one solution. It presents the summary using the top axis.
x <- data.frame(
Time = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Value = c(5,10,15,20,30,50,70,80,100,5,7,9,11,15,17,19,17,19,100,200,300,400,500,700,1000,200))
boxplot(x$Value ~ x$Time)
fivenums <- aggregate(x$Value, by=list(Time=x$Time), FUN=fivenum)
labels <- apply(fivenums[,-1], 1, function(x) paste(x[-1], collapse = ", "))
axis(3, at=fivenums[,1],labels=labels, las=1, col.axis="red")
Of course you can additionally play with the font size or rotation for this summary. Moreover you can break the line in one place, so the label will have smaller width.
Edit
In order to get what have you posted in the comment below you can add
text(x = 3 + 0.5, y = fivenums[3,-1], labels=fivenums[3,-1])
and you will get
however it won't be readable for other boxplots.

Pasting value into R plot title

I would like to paste the value of the mean in a plot title using the title() function.
e.g. title("My plot \n mean = mean(x)")
where x is a numeric vector of observations.
I know how to do this using plot(main = " ... ", ). Simply use paste() or substitute(expression()); however, this doesn't seem work with the title() function.
Any ideas?
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))
See this example where title works correctly:
set.seed(1)
x <- rnorm(30)
txt <- paste("Mean =", round(mean(x),3) )
plot(x)
title(main=txt)

How can I show a vector of labels with bold and normal font on a plot?

I would like to add text labels to a plot that contain both bold and normal font for several labels that are stored in a vector.
While I managed to do that for a single label (shown in green in the example plot below), I failed when I have several labels that I'd like to show with a single text call (shown in red in the plot).
Here is an example which creates the plot below:
plot(0,ylim=c(1,10),xlab="",ylab="",xaxt="n",yaxt="n")
mylabels <- c("Some text in bold: normal text", "Some other text in bold: more normal text")
text(x=rep(1, 2), y=c(10,9), labels=mylabels)
# change labels to show bold font before the colon and normal behind
mylabels.bold <- gsub("$","')", gsub(":", "'), ':", gsub("^","paste(bold('", mylabels)))
#> mylabels.bold
#[1] "paste(bold('Some text in bold'), ': normal text')"
#[2] "paste(bold('Some other text in bold'), ': more normal text')"
# this works
text(x=1, y=8, labels=parse(text=mylabels.bold[1]), col="green")
# this doesn't
parsetext <- function(x) { parse(text=x) }
text(x=rep(1, 2), y=c(7,6), labels=lapply(mylabels.bold,parsetext), col="red")
So the question is: How can I get the red text looking like the green?
This is one way to do this. So, instead of trying to loop in the labels argument you can loop outside so that you add the labels one by one.
plot(0, ylim=c(1,10), xlab="", ylab="", xaxt="n", yaxt="n")
xs <- rep(1, 2)
ys <- c(6, 7)
for (i in seq_along(mylabels.bold)) {
text(x = xs[i], y = ys[i], labels = parse(text=mylabels.bold[1]), col = "red")
}
Another solution would be to do:
plot(0,ylim=c(1,10),xlab="",ylab="",xaxt="n",yaxt="n")
parsetext <- function(x) { parse(text=x) }
text(x=rep(1, 2), y=c(7,6), labels=sapply(mylabels.bold,parsetext), col="red")
If you read the documentation of ?text you will see that labels need to be a character vector or expression. lapply creates a list and this is why it fails. sapply on the other hand creates a vector and works.
Out:

How to put comma in large number of VennDiagram?

I have a venn diagram that I make with the package VennDiagram. The numbers are above the 100,000.
I would like the number in the iddle to be 150,001, with a comma separator, or 150 000, with a small space in between. Is this possible to do with VennDiagram?
This is my example
library(VennDiagram)
venn.diagram(x = list(A = 1:200000,B = 50000:300000), filename = "../example.tiff")
I dont think you can do this easily. There are two print modes, raw, and percent, but these are hard-coded in the function (have a look at VennDiagram::draw.triple.venn). You can add formats by changing the function (which I wouldn't fancy) or by manually tweaking the grobs (which is done below)
library(VennDiagram)
p <- venn.diagram(x = list(A = 1:200000,B = 50000:300000), filename = NULL)
# Change labels for first three text grobs
# hard-coded three, but it would be the number of text labels
# minus the number of groups passed to venn.diagram
idx <- sapply(p, function(i) grepl("text", i$name))
for(i in 1:3){
p[idx][[i]]$label <-
format(as.numeric(p[idx][[i]]$label), big.mark=",", scientific=FALSE)
}
grid.newpage()
grid.draw(p)

rpart plot text shorter

I am using the prp function from the rpart.plot package to plot a tree. For categorical data like states, it gives a really long list of variables and makes it less readable. Is there any way to wrap text to two or more lines if exceeds some length?
Here's an example that wraps long split labels over multiple
lines. The maximum length of each line is 25 characters. Change the
25 to suit your purposes. (This example is derived from Section 6.1 in
the rpart.plot vignette.)
tree <- rpart(Price/1000 ~ Mileage + Type + Country, cu.summary)
split.fun <- function(x, labs, digits, varlen, faclen)
{
# replace commas with spaces (needed for strwrap)
labs <- gsub(",", " ", labs)
for(i in 1:length(labs)) {
# split labs[i] into multiple lines
labs[i] <- paste(strwrap(labs[i], width=25), collapse="\n")
}
labs
}
prp(tree, split.fun=split.fun)

Resources