R ggplot2 gave accent in legend - r

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.

Related

Export manually edited htmlwidget to SVG or similar

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.

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')

Blank page arrange_ggsurvplots Rstudio

Using the function arrange_ggsurvplots() in order to have 4 ggsurvplot() in a 2x2 representation, but using this function, it builds a blank page before the plot of the 4 groups graphs.
I build 4 different survfit as:
library(survminer)
fit_1 <- survfit(Surv(...) ~ ..., data = data_1)
splots[[1]] <- ggsurvplot(fit_1,...)
In order to arrange it all:
arrange_ggsurvplots(splots, print = TRUE,
ncol = 2, nrow = 2)
You need to save the plot as an object, and then save it to disk with ggsave():
myplots_out <- arrange_ggsurvplots(
myplots,
print = FALSE,
ncol = 2,
nrow = 2,
title = "MySuperPlots")
ggsave(
myplots_out,
file = "Results/Survival.pdf",
width = 10.5,
height = 5)
That will avoid the production of the blank first page in the output file.

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")

rCharts nvd3 library force ticks

I would like to force all tick marks and tick labels to appear along the axis in an rCharts nPlot from the nvd3 library. I have tried several approaches without success.
This is the default behaviour:
df <- data.frame(x = 1:13, y = rnorm(13))
library(rCharts)
n <- nPlot(data = df, y ~ x, type = 'lineChart')
n$yAxis(showMaxMin = FALSE)
I would like to have all data in 1:13 show along the x axis.
Ultimately, I have custom tickmarks I want to show equally-spaced with the following replacement:
n$xAxis(tickFormat = "#! function (x) {
ticklabels = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913',
'1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030',
'2030-2050', '2050-2070', '2070-2100']
return ticklabels[x-1];
} !#")
I hope it is clear why I want to have all tick marks and labels printed (and equally spaced).
To give an idea of the finished product, here is a ggplot2 impression:
library(ggplot2)
df <- data.frame(x = c('0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913',
'1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050',
'2050-2070', '2070-2100'), y = rnorm(13), z = "group1")
ggplot(data = df, aes(x = x, y = y, group = z)) + geom_line()
Here are several things I have tried, based on several suggestions I found here and there: neither work.
Based on my reading of the docs, I thought this would work:
n$xAxis(tickFormat = "#! function (x) {
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13][x-1];
} !#")
I also tried this, on the off chance:
n$xAxis(ticks = 13)
I also tried to combine tickValues and tickFormat but with no success.
I also thought about writing a script, but again my understanding of the nvd3 library was insufficient.
n$setTemplate(afterScript =
"<script>
var chart = nv.models.lineChart();
var newAxisScale = d3.scale.linear();
.range(d3.extent(chart.axes[0]._scale.range()))
.domain([1, d3.max(chart.axes[0]._scale.domain())])
chart.axes[0].shapes.call(
d3.svg.axis()
.orient('bottom')
.scale(newAxisScale)
.ticks(13)
//.tickValues()
//.tickFormat(d3.format())
).selectAll('text')
.attr('transform','')
</script>"
)
None of these report errors in the console, but none of them modify the appearance of the first plot above.
It turns out I was not correctly setting tickValues as I got the syntax confused with tickFormat. Here is an rCharts solution. The corresponding d3 or nvd3 solution should be easy to deduce.
n <- nPlot(data = df, y ~ x, type = 'lineChart')
n$yAxis(showMaxMin = FALSE)
n$addParams(height = 500, width = 1000)
n$xAxis(tickValues = "#! function (x) {
tickvalues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
return tickvalues;
} !#")
n$xAxis(tickFormat = "#! function (x) {
tickformat = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913',
'1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050',
'2050-2070', '2070-2100'];
return tickformat[x-1];
} !#")
n
Notice how the code has tickvalues in tickValues but tickformat[x-1] in tickFormat.

Resources