How to change font size in Euler plot in R? - r

I am trying to create a Euler diagram in R using the eulerr package. I would like to reduce the font size of the quantities text on the plot.
I have tried using cex=0.5 (as per example below) and have also tried fontsize = and font = but none have reduced the font size. Am I placing cex=0.5 in the wrong position?
library(eulerr)
set1 <- euler(c("A&B" = 3103,
"A&C" = 1034,
"A&D" = 118,
"B&C" = 2690,
"B&D" = 1017,
"C&D" = 1383,
"A&B&C" = 394,
"A&B&D" = 73,
"A&C&D" = 45,
"B&C&D" = 153,
"A&B&C&D" = 32))
eulerr.plot <- plot(set1,
fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = TRUE, legend = list(labels = c("A", "B", "C", "D")), cex = 0.5)

You can change the text size of the quantities by passing a list defining the size to that argument like so:
plot(set1, fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = list(cex = .5), legend = list(labels = c("A", "B", "C", "D")))

Related

How can I adjust the legend box?

This is my code:
score <- tapply(exams$writing.score
, list(exams$gender,
exams$race.ethnicity
)
, mean)
plot1 <- barplot(score
, beside = TRUE
, main = "Comparison of Writing Score"
, col = c("red", "lightyellow")
, xlab = "Race Ethnicity Group"
, ylab = "Average Writing Score"
, legend.text = c("Female", "Male")
, args.legend = list(x = "topright")
)
As I want to make the box: Female and Male smaller so it does not hide the bar behind. How can I make the legend box smaller? I tried to move it to the top right of the chart, but I do not think it moves.
You could use the argument cex. Here is a reproducible example:
data <- matrix(c(1,2,3,4,5,6,7,8,9,10), ncol = 5)
colnames(data) <- paste0("V", 1:5)
rownames(data) <- c('A','B')
# Normal
barplot(data, col = 1:nrow(data))
legend("topright", legend = rownames(data), pch = 15, col = 1:nrow(data))
# With cex
barplot(data, col = 1:nrow(data))
legend("topright", legend = rownames(data), pch = 15, col = 1:nrow(data), cex = 0.5)
Created on 2022-10-21 with reprex v2.0.2
Another option (in addition to using cex as #Quinten shows) is to also change the inset to move the legend outside of the plot boundary, as well as using par to specify the parameters for margins, etc.
par(mar = c(5, 4, 4, 8),
xpd = TRUE)
# Normal
barplot(df, col = 1:nrow(df))
legend(
"topright",
inset = c(-0.1, 0),
# Create legend outside of plot
legend = rownames(df),
pch = 15,
col = 1:nrow(df),
cex = 0.8
)
Data
df <- structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), dim = c(2L, 5L), dimnames = list(
c("Female", "Male"), c("V1", "V2", "V3", "V4", "V5")))
It doesn't move because you already are at the very top. To move the top upwards and let the legend follow, expand ylim.
Also try if you like setting the legend horizontal and remove the bty (boxtype). Don't choose the cex too small.
barplot(score
, beside=TRUE
, main="Comparison of Writing Score"
, col=c("red", "lightyellow")
, xlab="Race Ethnicity Group"
, ylab="Average Writing Score"
, legend.text=c("Female", "Male")
, args.legend=list(x="topright", cex=.9, horiz=TRUE, bty='n')
, ylim=c(0, max(score)*1.2)
)
Data:
score <- structure(c(96.8, 95.2, 100, 100, 89.7, 89.2, 81.4, 81, 85.1,
82), dim = c(2L, 5L), dimnames = list(c("1", "2"), c("A", "B",
"C", "D", "E")))

add trace in Plotly won't plot the right colour

I am trying to make a contour plot and draw a line on top of it (which I can do). I then overlaid two other lines using add_trace. For some reason the two lines I add using add_trace comes out orange instead of grey even though I specify grey (line = list(color = 'grey', )
I don't know how to add my data, it is very big. Is there any obvious reason as to why the colour changes to orange? If I change the width or the dash, it works. it just doesn't want to use the grey colour!
Thank you
(plot <- plot_ly(df, x = ~A, y = ~B, z = ~Difference, zauto = FALSE, zmin = -250, zmax = 250,
type="contour",
colorbar = list(title = "", titleside='right',
tickvals=c(-250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250), len = 1),
colorscale = "RdBu",
contours = list(start = 0, end = 0, coloring='heatmap', coloring='lines'),
line = list(color = 'black', width = 2)) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'dash'), contours = list(start = 0, end = 0, coloring='lines')) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'solid'), contours = list(start = 0, end = 0, coloring='lines')) %>%
layout(margin = list(l = 50, r = 70, b = 50, t = 50, pad = 4),
title = "", xaxis = x, yaxis = list(title = ""), font=t))
I guess the problem in your above code is, that you aren't specifiying the trace type. Accordingly plot_ly assumes that you are adding two more contour traces. Those traces are inheriting the colorscale you defined.
To avoid this you need to specify type = "scatter", mode = "lines", inherit = FALSE.
I made a simple example based on this.
library(plotly)
fig <- plot_ly(
x = c(-9, -6, -5, -3, -1),
y = c(0, 1, 4, 5, 7),
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5),
type = "contour", colorbar = list(title = "", titleside='right'),
colorscale = "RdBu",
line = list(color = 'black', width = 2)) %>%
add_trace(x = -1:-7, y = 1:7, type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE)
fig

Annotation / Text for ticklabels in R plotly polar chart

Let's say, I have a simple polar chart:
R code:
library(plotly)
p <- plot_ly(
type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers'
) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE#,
#tickmode="array",
#tickvals = c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5),
#ticktext = c('A', "B", "C", "D", "E", "F", "G", "H")
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Chart plotted:
When I set showticklabels = FALSE, the angle tick labels disappears, and then I want to put A,B,C,D,E,F,G and H at angles c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5).
I am not able to get the below expected plot using ticktexts and tickvals.
Can someone please help me with getting me to the solution, or if it is possible with add_text or add_annotation ?
Expected plot:
you could remove all grid lines altogether in the angularaxis with showgrid = FALSE, or you can have a line per 22.5 degree starting from 0 and then the ticktext would be something like this c('', 'A', '', 'B', '', 'C', .......) , or you could tediously add the lines you expect to have and then remove the grid lines like this:
p <- plot_ly() %>%
# data points
add_trace(type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers') %>%
# straight line from 0 dg to 180 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 180, 360),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 45 dg to 225 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 45, 225),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 90 dg to 270 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 90, 270),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 135 dg to 315 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 135, 315),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# fill circle of radius 1
add_trace(type = 'scatterpolar',
mode = 'lines',
r = 1,
theta =seq(0, 360, 0.1),
line = list(color = 'grey'),
fill = 'toself',
fillcolor = 'grey',
opacity = 0.5) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE,
# remove grid lines and ticks
showgrid = FALSE,
ticks = '',
# if you want the axis to go the other way around
# direction = 'clockwise',
tickmode="array",
tickvals = seq(22.5, 337.5, 45),
ticktext = LETTERS[1:8]
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Output chart:
I noticed that the expected output you have there, lists the letters the other way around as you have them in your code. If this is something you also want just change the order of the letters to match the angles like this c("A", "H", "G", "F", "E", "D", "C", "B") (reversed order starting from A)

Customizing Euler Diagram colors with eulerr R

I'm trying to plot an Euler Diagram using the eulerr package by Johan Larsson in R. I'm following this example where the developer explains how to customize colors/border transparency. However, when I try to implement it with the following code:
fit2 <- euler(c(A = 16971, B = 218, C = 215,
"A&B" = 112, "A&C" = 112, "B&C"= 51,"A&B&C" = 23))
plot(fit2,
polygon_args = list(col = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
border = "transparent"),
text_args = list(font = 8), counts=TRUE)
Colors/border remain unchanged.
I am using RStudio 1.0.136, R 3.3.1 on Windows.
Sorry about this. That post is old and the arguments have changed.
Try this instead
fit2 <- euler(c(A = 16971, B = 218, C = 215,
"A&B" = 112, "A&C" = 112, "B&C"= 51,"A&B&C" = 23))
plot(fit2,
fills = c("dodgerblue4", "darkgoldenrod1", "cornsilk4"),
edges = FALSE,
fontsize = 8,
quantities = list(fontsize = 8))

How to increase distance between nodes in DiagrammeR R

I have a nice graph with DiagrammeR in R studio, but the nodes are too clustered togather. I have searched everywhere but I cannot find a way of increasing the distance between them. Can I be shown?
Here is my code:
library(magrittr)
library(DiagrammeR)
# Create a simple NDF
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize=55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7))
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"))
graph <-
create_graph(
nodes_df = nodes,
edges_df = edges,
graph_attrs <-
c("layout = dot","overlap = FALSE","outputorder = edgesfirst"),
node_attrs <-
c("shape = circle",
"fixedsize = TRUE",
"width = 100",
"penwidth = 1",
"color = DodgerBlue",
"style = filled",
"fillcolor = Aqua",
"alpha_fillcolor = 0.5",
"fontname = Helvetica",
"fontcolor = Black"),
edge_attrs = "color = gray20")
# View the graph
render_graph(graph,layout=constant,output="visNetwork")
You could just set the length for the arrows between different nodes:
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"),
length = c(200,200,50,50,200,200))
Or you could define a precise spot for each node:
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize = 55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7),
x = c(-80,80,-80,80),
y = c(-80,80,80,-80))

Resources