Change the representation of the graph - r

i would like to have help with my graph. I would like to be able to change the graph representation algorithm (LGL or other) but I can't. How can I do it? have tried several options that do not work...
library(NetworkToolbox)
library(dplyr)
library(igraph)
library(ggplot2)
library(ggnetwork)
M1 <- as_tibble(replicate(21,sample(1:3,100,rep=TRUE)))
colnames(M1) <- c("1st", "2nd", "3th", "4th", "5th", "6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21th")
M2 <- as.matrix(round(cor(M1[,],method ="kendall"),2))
gr4ph <- graph.adjacency(M2, mode = "undirected",weight=TRUE)
MAST <- MaST(M2, normal = False)
gr4ph <- graph.adjacency(MAST , mode = "lower",weight=TRUE)
ggplot(gr4ph, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey", alpha = 1) +
geom_nodes(aes(color = name)) + theme_blank() +
geom_nodetext(aes(label = name), color = "black") +
geom_edgetext(aes(label = weight))+
theme(legend.position = "none")

You can use two solution. The layout argument is the key of the graph's representation :
The following exemple are coded with "Large graph layout"
With ggplot :
ggplot(gr4ph1, aes(x = x, y = y, xend = xend, yend = yend),layout = layout_with_lgl(gr4ph1))+
geom_edges(color = "grey", alpha = 1,size=1.5,) +
geom_nodes(aes(color = name), size = 3) + theme_blank() +
geom_nodetext(aes(label = name), color = "black", size = 3) +
geom_edgetext(aes(label = weight), size = 3,label.padding=unit(0.01, "lines"))+
theme(legend.position = "none")
With ggraph :
ggraph(gr4ph, layout = "lgl") +
geom_edge_link(aes(label = weight), angle_calc = 'along',label_dodge = unit(2.5, 'mm')) +
geom_node_point(aes(size=3,color= name)) +
theme(legend.position = "none")+
geom_node_text(aes(label = name), repel = TRUE)

Related

ggplot geom_rect color gradient (without reference to data)?

I was wondering if it is possible to have a geom_rect with a color gradient without a data reference, i.e. outside of aes().
I would like the two rectangles in the bottom of the following plot to show a color gradient from red to white (left to right) and the top one to show a color gradient from yellow to white.
Is this possible in a simple way or do I have to create data to refer to?
ggplot() +
geom_rect(aes(xmin = c(1, 3), xmax = c(2.5, 4), ymin = c(1, 1), ymax = c(2, 2)), color = "black", fill = "red") +
geom_rect(aes(xmin = 1, xmax = 3.5, ymin = 3, ymax = 4), color = "black", fill = "yellow") +
theme_bw() +
theme(panel.grid = element_blank())
I tried to use scale_fill_gradient with geom_tile but this doesn't really do what I want: 1. the two supposed-to-be-red rectangles share a gradient and don't start with pure red each and 2. I can't manage to use two different scale_fill_gradient's in one plot.
foo <- tibble(x = seq(from = 1, to = 2.5, by = 0.001),
y = rep(1, 1501))
bar <- tibble(x = seq(from = 3, to = 4, by = 0.001),
y = rep(1, 1001))
foobar <- tibble(x = seq(from = 1, to = 3.5, by = 0.001),
y = rep(3, 2501))
ggplot() +
geom_tile(data = foo, aes(x = x, y = y, fill = x)) +
geom_tile(data = bar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'red', high = 'white') +
geom_tile(data = foobar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'yellow', high = 'white') +
theme_bw() +
theme(panel.grid = element_blank())
You could use the function new_scale_fill from ggnewscale between your two different scale_fill_gradient functions in your plot process. This will reset your aesthetics to make it possible to use another gradient like this:
library(tibble)
foo <- tibble(x = seq(from = 1, to = 2.5, by = 0.001),
y = rep(1, 1501))
bar <- tibble(x = seq(from = 3, to = 4, by = 0.001),
y = rep(1, 1001))
foobar <- tibble(x = seq(from = 1, to = 3.5, by = 0.001),
y = rep(3, 2501))
library(ggplot2)
library(ggnewscale)
ggplot() +
geom_tile(data = foo, aes(x = x, y = y, fill = x)) +
geom_tile(data = bar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'red', high = 'white') +
new_scale_fill() +
geom_tile(data = foobar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'yellow', high = 'white') +
theme_bw() +
theme(panel.grid = element_blank())
Created on 2022-09-23 with reprex v2.0.2
To add for each geom_tile a gradient color, you could use for each tile new_scale_fill like this:
library(ggplot2)
library(ggnewscale)
ggplot() +
geom_tile(data = foo, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'red', high = 'white', guide = 'none') +
new_scale_fill() +
geom_tile(data = bar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'red', high = 'white') +
new_scale_fill() +
geom_tile(data = foobar, aes(x = x, y = y, fill = x)) +
scale_fill_gradient(low = 'yellow', high = 'white') +
theme_bw() +
theme(panel.grid = element_blank())
Created on 2022-09-23 with reprex v2.0.2

How to hide information on graph from the Legend in ggplotly?

I have horizontal dots plot replotted via ggplotly
df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2)
library(ggplot2)
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_hline(aes(yintercept = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(color = 'Rate'), size = 10) +
geom_text(aes(label = rate), color = 'white') +
geom_point(aes(x = change, color = 'Change'), size = 10) +
geom_text(aes(label = change, x = change)) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_color_manual(values = c('#aac7c4', '#5f9299')) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL)
ggplotly(plt)
The only issue is that when I hide one of the dot from the figure, texts are still appeared (see below), so is there way to tackle this issue and hide text with circle by clicking on legend?
P.S
(setting text colour in white color = 'white' is not option for me)
You can use the fill aesthetic for the points (as long as they are shape = 21) and use the color aesthetic for the text. As long as these have the same labels for aesthetic mapping, the interactivity for both points and text will be linked.
One minor annoyance is that this changes the plotly legend labels, even though they are correct in the ggplot version. This requires a little direct manipulation of the plotly object itself:
plt <- ggplot(df, aes(x = rate, y = factor(origin, rev(origin)))) +
geom_segment(aes(x = -100, xend = 200,
y = origin, yend = origin), color = 'gray') +
geom_vline(xintercept = 0, linetype = 2, color = 'gray') +
geom_point(aes(fill = 'Rate'), shape = 21, size = 10, color = NA) +
geom_text(aes(label = rate, color = 'Rate')) +
geom_point(aes(x = change, fill = 'Change'),
color = NA, shape = 21, size = 10) +
geom_text(aes(label = change, x = change, color = "Change")) +
theme_minimal(base_size = 16) +
scale_x_continuous(labels = ~paste0(.x, '%'), name = NULL) +
scale_fill_manual(values = c('#aac7c4', '#5f9299')) +
scale_color_manual(values = c("black", "white")) +
theme(panel.grid = element_blank(),
axis.text.y = element_text(color = 'gray50')) +
labs(color = NULL, y = NULL, fill = NULL)
p <- ggplotly(plt)
p$x$data[[3]]$name <- p$x$data[[3]]$legendgroup <-
p$x$data[[4]]$name <- p$x$data[[4]]$legendgroup <- "Rate"
p$x$data[[5]]$name <- p$x$data[[5]]$legendgroup <-
p$x$data[[6]]$name <- p$x$data[[6]]$legendgroup <- "Change"
p
This gives us the following plot:
Now clicking on Rate we get:
And clicking on Change we get

barplot with lineplot - secondary axis

After referring to multiple links i have got to the below code however i still am not succeeding to get the line with labels. I suspect some mistake in sec.axis transformation but i can't figure it out.
# dummy data
df_dummy = data.frame('Plan_code'=c('A','B','C','D','E','F','G'),
'Total'=c(191432,180241,99164,58443,56616,29579,19510),'STP'=c(41,40,44,37,37,37,45))
# creation of plot
[![g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_line(data = df_dummy, aes(x = Plan_code, y = STP,group=1)) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT')) +
labs(fill = NULL, color = NULL) +
theme_minimal()
print(g)][1]][1]
Like that?
g <- ggplot(data = df_dummy, aes(x = Plan_code, y = Total)) +
geom_col(aes(fill = 'Total')) +
geom_point(data = df_dummy, aes(x = Plan_code,y=STP * 2000)) +
geom_label(data = df_dummy, aes(x = Plan_code, y = STP *2000, fill = Plan_code, label = paste0('%', STP)), color = 'white', vjust = 1.6, size = 3) +
scale_y_continuous(sec.axis = sec_axis(~. / 2000, name = 'PERCENT'))+
geom_line(data = df_dummy, aes(x = Plan_code, y = STP * 2000,group=1), col = 'blue') +
theme(axis.text.y.right = element_text(color = 'blue'),axis.title.y.right = element_text(color = 'blue'))
labs(fill = NULL, color = NULL) +
theme_minimal()
I just multiplied your data with 2000, so that the absolute y-coordinates were right.
And I changed the color.

Error bars on a radar plot?

I'm looking to make a radar plot for multivariate data, a task simple enough for excel.
The problem comes when I would like to also plot some error bars on this. From what I understand, I cannot do this in excel. Is this possible on R?
Or can someone suggest an alternative? I have 32 single value dimensions.
Thanks!
I don't much like radar charts but here are some ideas to get you going, drawing on this approach. I like the look of my option 1 best, but I'm not sure how to solve the gap between var32 and var1 (I have some ideas, but a bit awkward).
library(tidyverse)
library(ggplot2)
library(scales)
# make some mock data
mydata <- data.frame(variable = paste0("Var", 1:32),
midpoint = rnorm(32),
stderr = rnorm(32, 1, 0.1),
stringsAsFactors = FALSE) %>%
mutate(upper = midpoint + 1.96 * stderr,
lower = midpoint - 1.96 * stderr) %>%
mutate(variable = factor(variable, levels = variable))
# Option 1:
mydata %>%
ggplot(aes(x = variable, y = midpoint, group = 1)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey50", alpha = 0.5) +
geom_line(colour = "purple") +
theme_light() +
theme(panel.grid.minor = element_blank()) +
coord_polar() +
labs(x = "", y = "")
# Option 2:
mydata %>%
gather(measure, value, -variable, -stderr) %>%
ggplot(aes(x = variable, y = value, colour = measure, group = measure, linetype = measure)) +
geom_polygon(fill = NA) +
theme_light() +
theme(panel.grid.minor = element_blank()) +
coord_polar() +
scale_colour_manual(values = c("steelblue", "black", "steelblue")) +
scale_linetype_manual(values = c(2,1,2)) +
labs(x = "", y = "")
# Option 3:
mydata %>%
ggplot(aes(x = variable, y = midpoint, group = 1)) +
geom_polygon(fill = NA, colour = "purple") +
geom_segment(aes(xend = variable, y = lower, yend = upper), colour = "grey50") +
geom_point(colour = "purple") +
theme_light() +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
coord_polar() +
labs(x = "", y = "")
Edit / addition
I think I prefer this one:
# Option 4:
mydata %>%
ggplot(aes(x = variable, y = midpoint, group = 1)) +
geom_polygon(aes(y = upper), fill = "grey50", alpha = 0.5) +
geom_polygon(aes(y = lower), fill = "grey99", alpha = 0.7) +
geom_polygon(fill = NA, colour = "purple") +
theme_light() +
theme(panel.grid.minor = element_blank()) +
coord_polar() +
labs(x = "", y = "")

Labeling two contours with direct.labels

I'm trying to compare two scalar fields and want to draw them in the same plot using contours labeling their values with directlabels.
The thing is, I'm not able to use two direct labels in the same plot.
Example:
library(ggplot2)
library(data.table)
library(directlabels)
grid <- expand.grid(lon = seq(0, 360, by = 2), lat = seq(-90, 0, by = 2))
grid$z <- with(grid, cos(lat*pi/180))
grid$z2 <- with(grid, sin(lat*pi/180))
grid.long <- melt(grid, id.vars = c("lon", "lat"))
# Manually adding two geom_dl's
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "top.pieces", color = "black")
Only one variable is labeled.
Another way:
ggplot(grid.long, aes(lon, lat)) +
geom_contour(aes(z = value, color = variable)) +
geom_dl(aes(z = value, label = ..level.., color = variable),
stat = "contour", method = "top.pieces")
Any solution?
Thanks!
One solution is to provide different method= argument for the second geom_dl() call.
ggplot(grid, aes(lon, lat)) +
geom_contour(aes(z = z), color = "black") +
geom_contour(aes(z = z2), color = "red") +
geom_dl(aes(z = z2, label = ..level..), stat = "contour", method = "top.pieces", color = "red") +
geom_dl(aes(z = z, label = ..level..), stat = "contour", method = "bottom.pieces", color = "black")

Resources