nVennr. Change standard form figures and remove legend - r

I am using nVennr, which has proved to be quite useful, although have been through all of the supporting documentation and still can't find out how to do 2 things that I am imagine are possible. They are how to change the displayed figures to non-standard-form format, and how to remove the legend.
Below is some data that illustrates how nVennr automatically converts figures to standard form.
library(nVennr)
library(dplyr)
library(rsvg)
library(grImport2)
SetA = round(runif(10000000),0)
SetB = round(runif(10000000),0)
SetC = round(runif(10000000),0)
allSets = cbind(SetA, SetB, SetC) %>%
as.data.frame() %>%
rownames_to_column()
SetA = subset(allSets, SetA == 1)$rowname
SetB = subset(allSets, SetB == 1)$rowname
SetC = subset(allSets, SetC == 1)$rowname
myV = plotVenn(list(SetA = SetA,
SetB = SetB,
SetC = SetC),
nCycles = 10000)
Any help would be greatly appreciated, in particular with the standard form issue, and this renders the plots quite ugly.
Thanks!

Glad you like the package. I am not sure what you mean by standard form. Perhaps you want one of those diagrams with three overlapping circles. In that case, I would recommend using other packages, like those suggested here. You can get a regular Venn diagram with nVennR, as shown in the vignette, but the default shape is always round in the region, not in the set:
library(nVennR)
myV3 <- createVennObj(nSets = 3, sSizes = c(rep(1, 8)))
myV3 <- plotVenn(nVennObj = myV3, labelRegions=F, showNumbers=F, opacity=0.2, borderWidth=4)
The result would be:
Regarding the legend, I did not think that would be necessary, as it is very easy to remove from the resulting svg file. However, since I need to upload a new version, I will add this function shortly.
EDIT:
The new version of nVennR (0.2.2) has just been released. Now, plotVenn and showSVG have the option showLegend. If set to false, the legend is not shown.

Related

R Reference a cell within a user defined function

I'm relatively new to R and really new to creating functions, but need to make some for others who don't use r to be able to use easily. I'm looking for a function that will allow me to reference a the top cell, or row of a column I've searched pretty hard but everything I've found seems to return errors. The function I need will subtract cells from the same column as the reference but moving down the column while the reference remains fixed. Code so far is:
df <- tibble(depth.along.core = 1:5,
Age.cal.BP = 1:5,
AFB = 1:5,
assumed.C = rep(0.5, 5))
Mega_Bog <- function(data) {
require(dplyr)
data %>% mutate(PCAR=((lead(depth.along.core) - depth.along.core )/(lead(Age.cal.BP) - Age.cal.BP))*AFBD*assumed.C*10000,
PCA_NCP = PCAR*(lead(Age.cal.BP)-Age.cal.BP),
PCA_NCP[is.na(PCA_NCP)] <- 0,
CCP_Bottom_Up = rev(cumsum(rev(PCA_NCP)))
CCP_Top_Down = CCP_Bottom_Up##Need cell reference here## -CCP_Bottom_Up)}
I know the is.na is a bit weird but it works, although I'm not sure why. The output I'm after will look like:
CCP_Top_Down <- c(0,-1,-2,-3,-4)
Thanks people

dplyr new version 1.0.0 eliminate tbl_cube function and I found no replace of it

I work with multiple dimensional arrays and when I need to plot I usually convert my data to a tibble through tbl_cube to then plot it with ggplot2. Today the new dplyr 1.0.0 was updated to CRAN, and I found that now tbl_cube is no more available. And I could not found a replacement to tbl_cube. I did something like this toy example before today to get a plot:
test_data1 <- array(1:50, c(5,5,2))
test_data2 <- array(51:100, c(5,5,2))
# list of my arrays
test_data <- list(exp1 = test_data1, exp2= test_data2)
# list of the dimentions
dims_list <- list(lat = 1:5, lon = 1:5, var = c('u','v'))
new_data <- as_tibble(tbl_cube(dimensions = dims_list, measures = test_data))
# Make some random plot
ggplot(new_data, aes(x=lon,y=lat)) +
geom_tile(aes(fill=exp2))+
geom_contour(aes(z=exp1),col='black')
This example runs and works with previous dplyr release, but not now since tbl_cube does not exist anymore. I know that in this example the third dimension is not used to do the plot but I wanted to show that I need something to used over at least a 3D array or even a 4D.
Any suggestion of how to solve this in an easy way such as tbl_cube ?

flextable package assigns changes without <-

I'm not sure if this is the correct forum to post this, but I have noticed some strange behavior with the flextable package in R, and was wondering if anyone can shed any light.
In the documentation for flextable it shows objects being modified when they are re-assigned to themselves, eg:
ft <- regulartable(head(iris))
ft <- color(ft, color = "orange", part = "body" )
However, my code is modifying the actual table even without re-assigning it, just using piping %>%:
myft <- regulartable(head(iris))
myft %>% align(j = 1, align = "left")
myft # changed
I don't think piping is the issue as it doesn't have the same effect with other packages, eg:
library(plyr)
df <- head(iris)
df %>% mutate(Sum=Sepal.Width*2)
df # unchanged
Is this a bug in flextable? Or is this by design?
It's true that you can mutate formats without assigning the object. But that's not a behavior you can rely on. This is an unwanted design ;) and should be corrected in the next versions so it is safer to assign the result if you want your code to work with future versions.

how to save r dataframe as an image

How can I create an image from a data frame? For example:
library(tidyverse)
library(gridExtra)
df = iris %>% slice(1:4)
I've the following but:
1. I haven't been able to get this to be saved to a variable. It just pops up in the plots pane of Rstudio. Am I missing something obvious? I'd like to be able to have this plot referenced to a variable so I could save it as a png or something.
2. Is there a way to remove the row numbers that seem to appear?
3. This look is fine, but is there a way to make it more of a lighter background compared to what this is?
gridExtra::grid.table(df)
To save a relevant variable, use
myTable <- tableGrob(df)
since
grid.table
# function (...)
# grid.draw(tableGrob(...))
# <bytecode: 0x10758c078>
# <environment: namespace:gridExtra>
Given that, you can run
library(grid)
grid.draw(myTable)
For that you want
myTable <- tableGrob(df, rows = NULL)
See ?tableGrob and particularly ttheme_default (its source code makes pretty clear what are possible parameters; see also here). For instance,
myTable <- tableGrob(
df,
rows = NULL,
theme = ttheme_default(core = list(bg_params = list(fill = "grey99")))
)
grid.draw(myTable)

How to set highchart global options IN R

I see a lot of examples in javascript but I cannot find an example to do it in R
Here is the api link: http://api.highcharts.com/highcharts#global
I am trying to set "timezoneOffset" and I have tried many different ways.
When I do this in R: highChart$global(timezoneOffset=-300)
I do not get any warning or error, but it's not working.
Thanks a lot for the help!
Here is a piece of code:
library(rCharts)
highChart <- Highcharts$new()
highChart$global(timezoneOffset=-300)
highChart$chart(zoomType = "xy")
highChart$exporting(enabled = T)
highChart$xAxis(type="datetime",list( title = list(text = "Time")))
highChart$yAxis(list
(
list(title = list(text = "Variance"))
))
highChart$series(data=list(c(x=1407795845000,y=1),c(x=1407795846000,y=2),c(x=1407795847000,y=3)))
highChart
As you can see, the timezoneOffset is not working when I run this piece of code and the time is still displayed in GMT.
As of version 0.5.0 of highcharter, it seems the option highcharter.options is not there any more, but there are several separate options, e.g. highcharter.lang, highcharter.global, etc. So the following approach works:
lang <- getOption("highcharter.lang")
lang$decimalPoint <- ","
lang$numericSymbols <- highcharter::JS("null") # optional: remove the SI prefixes
options(highcharter.lang = lang)
In addition to changing the decimal point, the SI prefixes ("k", "M", "G", etc.) are turned off by setting the numericSymbols to null, see Replacing/removing the metric notations like thousands "k" abbreviation.
The highcharter options can be accessed, but they are set inside the standard R options under the list element highcharter.options. They are not given directly to the highchart, and inside highchart(), there is the code line opts <- getOption("highcharter.options", list()).
I don't think there is another way than just get the options, alter whatever options you need to change and then set the options again with your additions.
The following is a simple illustration:
library(highcharter)
# normal highchart
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
opts <- getOption("highcharter.options")
opts$lang$decimalPoint <- "."
options(highcharter.options = opts)
# now with "," instead of "." (confirm in tooltip)
highchart() %>%
hc_add_serie_labels_values(1:901, seq(1, 10, 0.01))
Of course in your case, you need to set the $global$timezoneOffset part.
First you have to switch of the useUTC flag to FALSE. Than you can set the timezoneOffset as you wish and save the options back.
global <- getOption("highcharter.global")
global$useUTC <- FALSE
global$timezoneOffset <- -300
options(highcharter.global = global)
For better understanding make sure you take a look at global:
str(global)

Resources