Export manually edited htmlwidget to SVG or similar - r

I often create Sankey-diagrams in R via {sankeyD3}, because it seems to be the package with the most options/features to do so. However, one feature that is missing is the ability to set the order of nodes on the y-axis (although this issue tried to fix that?).
Therefore, I must arrange the nodes manually afterwards. I can do this by setting dragY = TRUE when creating the diagram and then exporting it to an html file via htmlwidgets::saveWidget(). This allows me to manually drage the nodes when opening the html file.
reprex
links <- data.frame(
source = c(0, 0, 0, 1, 2, 3, 4, 4),
target = c(1, 2, 3, 4, 4, 4, 5, 6),
value = c(2, 3, 4, 2, 3 , 4, 4, 5)
)
nodes <- data.frame(
label = c("A1", "B1", "B3", "B2", "C1", "D1", "D2"),
yOrder = c(1, 1, 3, 2, 1, 1, 2)
)
out <- sankeyD3::sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "label",
fontFamily = "Arial",
fontSize = 12,
numberFormat = ",.1s",
height = 500,
width = 700,
dragY = TRUE)
htmlwidgets::saveWidget(out,
file = here::here("out.html"),
selfcontained = TRUE)
and here is a screenshot showing the exported html on the left and the one where I manually rearranged the nodes on the right:
Question
My goal is to insert the edited diagram into a word-document in the best possible quality. So I guess I want to know how to export the edited html-file to a SVG format or similar?

Open the result in a browser, make any manual adjustments you want, then use an SVG extractor like https://nytimes.github.io/svg-crowbar/ to save it as an SVG.

Related

R plotly scatter3d: How to make nearest marker appear the biggest?

Currently my code looks like this:
library(plotly)
count = data.frame(
row.names = c("Cell1", "Cell2", "Cell3", "Cell4", "Cell5", "Cell6"),
Gene1 = c(10, 11, 8, 3, 2, 1),
Gene2 = c(6, 4, 5, 3, 2.8, 1),
Gene3 = c(12, 9, 10, 2.5, 1.3, 2),
Gene4 = c(5, 7, 6, 2, 4, 7),
stringsAsFactors = FALSE
)
threeD = plotly::plot_ly(
data = count,
x = ~Gene1,
y = ~Gene2,
z = ~Gene3,
type = "scatter3d",
mode = "markers",
marker = list(size = 20),
color = row.names(count)
)
threeD
This code generates following output:
I would like to make the marker scale with the distance. So the markers nearest to "me" is bigger (Cell1 & Cell 2) and the markers far away appear smaller (Cell5 & Cell6). This would achieve a more realistic 3D feeling.
It's possible to make a kind of "bubble" plot by assigning a list of size to the markers so that they grow according to their respective value along the z-axis. The simplest way is to reuse the same data and apply some scaling function (product, log, etc.) as needed for example :
marker = list(size = c(12, 9, 10, 2.5, 1.3, 2)*5)
The problem is that if you change your point of view, the markers won't update magically for the intended 3D feeling.
You can also use color scaling by adding the colorscale property to the marker object, for example :
colorscale = c('#FFE1A1', '#683531')

Networkd3 Sankey Diagram Won't Print to HTML

I am trying to create a Sankey Network for energy flow from data imported from excel sheets. I don't need it to be interactive and I think I have the code right. However, when I run the code in R Markdown it just creates a blank space - no diagram. Code as follows:
library(dplyr)
library(shiny)
library(htmlwidgets)
library(networkD3)
nodes = data.frame("name" = c(Energy$`Alberta Energy Flow in 2015: PJ`[2:11], Energy$...5[1], Energy$...6[1], Energy$...7[1], Energy$...8[1], Energy$...9[1], Energy$...10[1], Energy$...11[1],Energy$...2[13],Energy$...3[13] ), stringsAsFactors = FALSE)
links = as.data.frame(matrix(c(
0, 10, Energy$...5[2],
1, 10, Energy$...5[3],
2, 10, Energy$...5[4],
3, 10, Energy$...5[5],
4, 10, Energy$...5[6],
5, 10, Energy$...5[7],
6, 10, Energy$...5[8],
7, 10, Energy$...5[9],
8, 10, Energy$...5[10],
9, 10, Energy$...5[11],
0, 12, Energy$...7[2],
1, 12, Energy$...7[3],
#code continues as such for a while
byrow = TRUE, ncol = 3 ))
names(links)=c("source", "target", "value")
s <- sankeyNetwork(Links=links, Nodes=nodes, Source="source", Target="target",Value="value", NodeID="name", fontSize=12, nodeWidth=25 )
sankeyNetworkOutput("ABEnergy15.html", width = "500px", height = "1000px")
sankeyNetworkOutput() is used for Shiny apps, so you probably don't want to use that.
If you want to use it in RMarkdown, you probably want to add s to the last line of your code chunk, because that will tell R to "print" the s object which contians the htmlwidget that sankeyNetwork() created.
If you want to save it to a file, which it seems like based on your code having an html filename it, try using...
saveNetwork(s, "ABEnergy15.html")

sankeyD3 > sankeyNetwork implementation of 'NodePosX'

I have been using the sankeyD3 package to create SankeyNetworks and the 'NodePosX' feature isn't working for me yet. The 'NodePosX' feature is not in the 'networkD3' package but it is in the 'sankeyD3' package.
To help illustrate the problem that I am having, I have edited the example from akraemer007 that was posted here to include the X positions of the nodes (see below) but it's still not working in the way that he had originally wanted, with manual control over the x-position of the 'Opted-Out' node.
We're aiming for something like this, but without the small line from 'Opted-Out' to 'Activated':
library(devtools)
devtools::install_github("fbreitwieser/sankeyD3")
library(sankeyD3)
name <- c('Enrolled', 'Opted-Out', 'Invited', 'Activated')
xpos <- c(0, 1, 1, 2)
nodes <- data.frame(name, xpos)
source <- c(0, 0, 2, 1)
target <- c(1, 2, 3, 3)
value <- c(20, 80, 60, 0)
links <- data.frame(source, target, value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",NodePosX = "xpos",
units = "TWh", fontSize = 12, nodeWidth = 30)
Assuming the last row in your links data frame is only there to force the plot to look the way you want and not part of the actual data you want to plot, you can achieve this with networkd3 using the sinksRight = FALSE parameter.
library(networkD3)
name <- c('Enrolled', 'Opted-Out', 'Invited', 'Activated')
xpos <- c(0, 1, 1, 2)
nodes <- data.frame(name, xpos)
source <- c(0, 0, 2)
target <- c(1, 2, 3)
value <- c(20, 80, 60)
links <- data.frame(source, target, value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30, sinksRight = FALSE)

R ggplot2 gave accent in legend

I created a function to plot some data per city in a line graph. I want the user to be able to change the label of each city in the legend.
A simplified example:
example_plot <- function(plot_labs = c("Anvers", "Liège")){
graphics.off()
input <- data.table(x_axis = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
y_axis = c(5, 6, 4, 2, 8, 9, 3, 1, 7, 5),
City = c("Anvers", "Anvers", "Anvers", "Anvers", "Anvers",
"Liege", "Liege", "Liege", "Liege", "Liege"))
ggplot(data = input, aes(x = x_axis, y = y_axis, group = City, lty = City)) +
geom_line() + scale_linetype_manual(labels = plot_labs, breaks = c("Anvers",
"Liege"), values = 1:2)
}
My problem:
When I save the function as "example_plot.R" and then call it in the command prompt with no argument, the accent in "Liège" does not display correctly:
example_plot()
If I call the function with the plot_labs argument, it displays correctly:
example_plot(plot_labs = c("Anvers", "Liège"))
What I find even stranger is that if I copy-paste the function's code in the command prompt (instead of 'source(example_plot.R")'), then everything works fine.
Any idea why it behaves differently when the function is saved?
You're probably saving your source file in an encoding such as UTF-8 and then reopen or source it assuming it's in Latin-1.
If you're using RStudio, check the menu points File/Save with encoding, and File/reopen with encoding, and ensure the character encodings match.

Plotting with Vennerable package in R

Here is a very basic example:
library(vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
plot(srl.venn)
All I'm trying to do is to remove borders around circles, and format colors and fonts. However, still haven't done much.
Could you please share any useful examples?
Check out VennThemes for changing parameters within the plot. For example:
library(Vennerable)
srl.venn <- Venn(SetNames=c("Cognitive condition","Operations","Individual differences"),
Weight=c(0,30, 21, 15, 1, 8, 3, 6))
srl.venn.c <- compute.Venn(srl.venn, doWeights=T)
gp <- VennThemes(srl.venn.c, colourAlgorithm = "binary")
plot(srl.venn.c, gpList = gp, show = list(FaceText = "signature", SetLabels = FALSE,
Faces = FALSE, DarkMatter = FALSE))
More detail can be found in the man pages or by calling vignette("Venn")

Resources