How to display both raw and percent in Venn diagram - r

I am plotting a Venn diagram using the function draw.triple.venn() library(VennDiagram). This is my code in R:
g = draw.triple.venn(area1 = 4.1, area2 = 5.6, area3 = 15.9, n12 = 1.3, n23 = 4.2, n13 = 2.3, n123 = 1.2, category = c("Landuse", "Environment", "Space"), fill = c("darkgray", "gray", "lightgrey"), print.mode = c("percent", "percent", "percent"), sigdig=2, ind = T)
grid.arrange(gTree(children=g))
This is the current figure:
Now, I would like to display both 'percentage' and 'raw' for each fraction. In the package description it states: 'print.mode' can be either 'raw' or 'percent'. This is the format that the numbers will be printed in. Can pass in a vector with the second element being printed under the first.
This seems to suggest that both 'raw' and 'percent' can be displayed together. Any suggestions on how to do this?
Also, how can I control that number of digits is used consistently, i.e. have 56.0% (rather than 56%) and 0.5% (rather than 0.53%)? I have set sigdig=2 which I thought would force consistency in that space.
Moreover, is there a way to control the fill colour of each fraction (as compared to only a vector of 3 colours)?
Finally, is there any way to add text manually? I would like to note the proportion of residual variation in the bottom left corner.
This is a link to the package https://cran.r-project.org/web/packages/VennDiagram/VennDiagram.pdf
Any help with this is much appreciated.

Using print.mode = c("raw", "percent") works to include both raw and percent values.
Function grid::grid.text("some label", x=0.1, y=0.1) works to add text manually.

Related

Select argument doesn't work on cca objects

I created an object of class cca in vegan and now I am trying to tidy up the triplot. However, I seemingly can't use the select argument to only show specified items.
My code looks like this:
data("varechem")
data("varespec")
ord <- cca(varespec ~ Al+S, varechem)
plot(ord, type = "n")
text(ord, display = "sites", select = c("18", "21"))
I want only the two specified sites (18 and 21) to appear in the plot, but when I run the code nothing happens. I do not even get an error meassage.
I'm really stuck, but I am fairly certain that this bit of code is correct. Can someone help me?
I can't recall now, but I don't think the intention was to allow "names" to select which rows of the scores should be selected. The documentation speaks of select being a logical vector, or indices of the scores to be selected. By indices it was meant numeric indices, not rownames.
The example fails because select is also used to subset the labels character vector of values to be plotted in text(), and this labels character vector is not named. Using a character vector to subset another vector requires that the other vector be named.
Your example works if you do:
data("varechem")
data("varespec")
ord <- cca(varespec ~ Al + S, varechem)
plot(ord, type = "n")
take <- which(rownames(varechem) %in% c("18", "21"))
# or
# take <- rownames(varechem) %in% c("18", "21")
text(ord, display = "sites", select = take)
I'll have a think about whether it will be simple to support the use case of your example.
The following code probably gives the result you want to achieve:
First, create an object to store the blank CCA1-CCA2 plot
p1 = plot(ord, type = "n")
Find and then save the coordinates of the sites 18 and 21
p1$p1$sites[c("18", "21"),]
# CCA1 CCA2
#18 0.3496725 -1.334061
#21 -0.8617759 -1.588855
site18 = p1$sites["18",]
site21 = p1$sites["21",]
Overlay the blank CCA1-CCA2 plot with the points of site 18 and 21. Setting different colors to different points might be a good idea.
points(p1$sites[c("18", "21"),], pch = 19, col = c("blue", "red"))
Showing labels might be informative.
text(x = site18[1], y = site18[2] + 0.3, labels = "site 18")
text(x = site21[1], y = site21[2] + 0.3, labels = "site 21")
Here is the resulted plot.

Modify axes in control chart RStudio

I would like to ask a help in modify axes in control chart in RStudio, by qcc package.
Generated a controlchart type EWMA, in the axes x, appear group, sequencially (that represents each observations), but I need insert an information about year (divide group into sequence of year). I tried using the command (axes), but doesn't work.
Which command or function it is possible to modify to achieve this?
Thank you very for help!
Yours faithfully
Guilherme
The ewma function inqcc accepts a labels parameter: a character vector of labels for each group.
You could, for instance, do something like:
library(qcc)
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)
label_x <- as.character(seq.Date(from = as.Date("2020/01/01"), length.out = 25, by = "day"))
q2 <-
ewma(
diameter[1:25, ],
center = 0,
lambda = 0.4,
std.dev = 0.57,
nsigmas = 3,
add.stats = FALSE,
labels = label_x
)
And end up with a properly labelled x-axis in your EWMA plot:

Taylor diagram size, color, save argument?

I generated Taylor diagram
using library:
library(plotrix)
library(rJava)
library(xlsxjars)
library(readxl)
library(xlsx)
and data used dput(df)
structure(list(Pb = c(12.54, 12.53, 13.57, 19.22, 22.27, 18.37,
19, 14.34, 16.35), Boruta_XGBoost = c(8.89136409759521, 8.85756206512451,
8.21786308288574, 12.9624891281128, 13.2968854904175, 12.7977981567383,
12.3957328796387, 9.69775390625, 11.781777381897), Boruta_SVM = c(12.8297886877439,
14.1176611943491, 14.9879296831078, 18.1703565549757, 19.5840584003096,
18.8792561701891, 16.2778795502105, 13.4901086669503, 14.2836465797069
), Boruta_RF = c(14.3177444444444, 11.1845266666667, 10.8805244444444,
17.6771022222222, 20.7142466666667, 18.5657155555556, 16.8954355555556,
12.9909844444444, 15.9368244444444), GA_XGBoost = c(8.90407180786133,
8.7996244430542, 8.19470405578613, 12.927845954895, 13.1713933944702,
12.896990776062, 12.4013185501099, 9.44187641143799, 11.9617586135864
), GA_SVM = c(14.5574045001675, 14.7111528658119, 14.5103784219767,
17.6100336774524, 19.0957864342351, 17.836320761182, 15.7884998778531,
13.2352198846198, 14.7596456378561), GA_RF = c(13.7602755555556,
11.43164, 10.6581822222222, 18.0504933333333, 20.8309088888889,
18.5738422222222, 16.5107822222222, 12.9161022222222, 15.53854
), XGBoost_XGBoost = c(8.42767715454102, 8.02925300598145, 7.27468395233154,
12.2474374771118, 13.0366401672363, 13.088475227356, 12.264874458313,
8.62850284576416, 10.4450559616089), XGBoost_SVM = c(13.0668720920662,
11.9439605396003, 12.2892781235256, 18.9685052474489, 22.1775370825045,
18.5729878124879, 18.412140924591, 13.7120185862238, 15.2216196143028
), XGBoost_RF = c(12.5330444444444, 12.6012, 10.6674022222222,
17.7389044444445, 20.4518844444445, 18.4774088888889, 17.0711288888889,
12.8564933333333, 14.4656444444444)), row.names = c(NA, -9L), class = c("tbl_df",
"tbl", "data.frame"))
using below code:
taylor.diagram(Actual,
Boruta_XGBoost,
add=FALSE,
col="#FFFF33", #the color for the points displayed.
pch=19, #the type of point to display.
#For Color: ("#FFFFFF" = White, “#990000” = Red) see more:- http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
pos.cor=TRUE,
xlab="Standard deviation (Normalized)",
ylab="Standard deviation (Normalized)",
main="Taylor Diagram of all applied models Vs Actual",
show.gamma=TRUE, #whether to display standard deviation arcs around the reference point (only for pos.cor=TRUE).
ngamma=4, #the number of gammas to display (default=3).
gamma.col = "red", #color to use for the gamma arcs (only with pos.cor=TRUE).
sd.arcs=3, #whether to display arcs along the standard deviation axes.
ref.sd=TRUE, #whether to display the arc representing the reference standard deviation.
sd.method=TRUE, #Whether to use the sample or estimated population SD.
grad.corr.lines = c(0.2,0.4,0.6,0.8,0.9), #the values for the radial lines for correlation values
pcex=2.8, cex.axis=1.8, cex.lab=2.2, #pcex for size of 'pch'; cex.axis for size of tick;
normalize=TRUE, #whether to normalize the models so that the reference has a standard deviation of 1.
mar=c(5,6,5,10), #margin=( bottom, left, top, right); only apply for pos.cor = TRUE plot
lwd=10,
font=6,
lty=1)
My quarries are:
why the size of x-axis-label is not increasing?, however, I used cex.sub the argument as well.
how to increase the size of gamma and sd.arc line?
how to change the color of sd.arc line?
how to save this graph in an editable file?; however, I used library(officer) but did not workout
Don't we have ggplot2 for the same? if yes then, please share
Any single answer to the single question would be appreciated as well.
many thanks!
This is a partial answer. Answers by quarry number in the question:
Your code cex.lab=2.2 is working. I changed it to cex.lab=1 and got very noticeable differences. Ditto for the other cex options.
2 - 3. These are not options in the function. However, what you can do is copy the code for the function from here: https://github.com/plotrix/plotrix/blob/master/R/taylor.diagram.R and change what you want, save it as your own function. For example, in the segment annotated " # add the axes", change the color option to col = "red" or whatever.
I don't know what you mean by an editable plot. Maybe someone else can address.
Search of https://ggplot2.tidyverse.org/reference/index.html results in nothing matching "taylor", so I don't think so.

R map plotting issue

I'm simply plotting lat/longs from a file onto a map in R. I'm having some issues as the points I plot are displayed in roughly the correct area - but not accurate enough for me. I don't know if this is my code, the google map, or the resultion of the lat/longs? I think I've eliminated the latter (see below).
Code:
library (ggmap)
library (gdata)
whitelee_boundary <- c(left = -4.6, bottom = 55.5, right = -3.8, top = 55.8)
map2 <- get_stamenmap(whitelee_boundary, zoom = 12, maptype = "toner-lite")
ggmap(map2)
df <- read.table("WhiteleeLatLong.txt", header = FALSE)
lat=df[1]
lon=df[2]
lati=as.numeric(unlist(lat))
long= as.numeric(unlist(lon))
points(lati, long, col = "blue", cex = 1.0)
which results in blue circles plotted in a very tight(incorrect) geographical range
I then attempted to error check the points by manually displaying:
points(55.7, -4.4, col = "blue", cex = 1.0)
This results in no point being visibly added (making me think that this is my code or google map accuracy).
I originally thought this was a precision issue in read.table or numeric conversion, and have already consulted:
How to stop read.table from rounding numbers with different degrees of precision in R?
as.numeric() removes decimal places in R, how to change?
However it seems that the rounding is only aesthetic, and the numbers are stored with the requisite precision.
Apologies in advance - its been a very long time since my last coding task!
EDIT: When I command
points(-4.4, 55.7, col = "blue", cex = 1.0)
THe output is (ignore large circles):
Plot appears on map but not in correct location

Using optional arguments (...) in a function, as illustrated with new population pyramid plot

Wanting to show the distribution of participants in a survey by level, I came upon the recently-released pyramid package and tried it. As the font on the x-axis is too large and there seem to be no other formatting choices to fix it, I realized I don't know how to add "other options" as permitted by the ... in the pyramid call.
install.packages("pyramid")
library(pyramid)
level.pyr <- data.frame(left = c(1, 4, 6, 4, 41, 17),
right = c(1, 4, 6, 4, 41, 17),
level = c("Mgr", "Sr. Mgr.", "Dir.", "Sr. Dir.", "VP", "SVP+"))
pyramid(level.pyr, Laxis = seq(2,50,6), Cstep = 1, Cgap = .5, Llab = "", Rlab = "", Clab = "Title", GL = T, Lcol = "deepskyblue", Rcol = "deepskyblue", Ldens = -1, main = "Distribution of Participants in Survey")
Agreed, the plot below looks odd because the left and the right sides are the same, not male and female. But my question remains as to how to invoke the options and do something like "Laxis.size = 2" of "Raxis.font = "bold".
Alternatives to this new package for creating pyramid plots include plotrix, grid, and base R, as demonstrated here:
population pyramid density plot in r
By the way, if there were a ggplot method, I would welcome trying it.
Contrary to Roland's and now nrussell's guesses (without apparently looking at the code) expressed in comments, the dots arguments will not be passed to pyramid's axis plotting routine, despite this being a base graphics function. The arguments are not even passed to an axis call, although that would have seemed reasonable. The x-axis tick labels are constructed with a call to text(). You could hack the text calls to accept a named argument of your choosing and it would be passed via the dots mechanism. You seem open to other options and I would recommend using plotrix::pyramid.plot since Jim Lemon does a better job of documenting his routines and it's more likely they will be using standard R plotting conventions:
library(plotrix)
pyramid.plot(lx,rx,labels=NA,top.labels=c("Male","Age","Female"),
main="",laxlab=NULL,raxlab=NULL,unit="%",lxcol,rxcol,gap=1,space=0.2,
ppmar=c(4,2,4,2),labelcex=1,add=FALSE,xlim,show.values=FALSE,ndig=1,
do.first=NULL)
with( level.pyr, pyramid.plot(lx=left, rx=right, labels=level,
gap =5, top.labels=c("", "Title", ""), labelcex=0.6))

Resources