Trying to recreate a centrality graph from the qgraph R package - r

I am trying to recreate the graph that is provided by the function centralityPlot in qgraph. I got a dataframe that looks like this:
symptom structure(list(symptom = c("9", "8", "7", "6", "5", "4", "3",
"2", "1"), lower_bound = c(0.209023862993771, -0.656057911395765,
-0.144732954079441, -0.240150983834066, -2.09690619987396, -1.14713000698362,
-1.78304406354482, -1.31269792892215, -1.04552934099257), mean = c(1.35359542511945,
0.546873106351184, 0.787717966105717, 0.42221064177518, -1.18693181743255,
-0.284265955202698, -1.19008711707311, -0.377827032555581, -0.0712852170875892
), upper_bound = c(1.9749871489344, 1.54642345677796, 1.46727206716789,
1.10712439281518, -0.0748008645128608, 0.812125575894532, -0.510038969136605,
0.587753574399307, 0.981045133733119)), class = "data.frame", row.names = c(NA,
-9L))
it should look like a singular plot like this one
It's supposed to be doable in GGplot but so far What I've gotten is a complete mess:
temporal.dep.in.plot <- ggplot(temporal.dep.in, aes(x = symptom)) +
ylim(NA, 2.25) +
geom_errorbar(
aes(ymin = lower_bound, ymax = upper_bound),
width = 0.4,
color = "#56B4E9"
) +
geom_segment(
aes(y = lower_bound, yend = upper_bound, xend = symptom),
linetype = "solid",
color = "#2166AC",
size = 6
) +
geom_point(
aes(y = mean),
shape = 16,
size = 9,
color = "#D6604D"
) +
theme_classic() +
coord_flip() +
ylab("Z-scores") + xlab("Symptoms") +
theme(axis.text.y = element_text(
face = "bold",
colour = c(
"#ff0000",
"#ffaa00",
"#aaff00",
"#00ff00",
"#00ffaa",
"#00aaff",
"#0000ff",
"#aa00ff",
"#ff00aa"
),
size = 14
))
which honestly only works trough sheer force of will.
If that's too much, the main part that I'm trying to achieve right now is actually connecting the dots (mean) with a line, which so far has not been working with many methods that I've tried.

Not sure how your final plot should look like. As is it looks a quite different from the one in your image. But to connect your mean points to could use a geom_line where the important step is to set group aes to a constant value e.g. 1.
library(ggplot2)
ggplot(temporal.dep.in, aes(x = symptom)) +
ylim(NA, 2.25) +
geom_errorbar(
aes(ymin = lower_bound, ymax = upper_bound),
width = 0.4,
color = "#56B4E9"
) +
geom_segment(
aes(y = lower_bound, yend = upper_bound, xend = symptom),
linetype = "solid",
color = "#2166AC",
size = 6
) +
geom_point(
aes(y = mean),
shape = 16,
size = 6,
color = "#D6604D"
) +
geom_line(aes(y = mean, group = 1), color = "#D6604D", size = 1) +
theme_classic() +
coord_flip() +
ylab("Z-scores") +
xlab("Symptoms") +
theme(axis.text.y = element_text(
face = "bold",
colour = c(
"#ff0000",
"#ffaa00",
"#aaff00",
"#00ff00",
"#00ffaa",
"#00aaff",
"#0000ff",
"#aa00ff",
"#ff00aa"
),
size = 14
))

Related

How to present the results of a dataframe in a serial scale using ggplot as in the example attached?

I have this data frame :
Raw.Score = c(0,1,2,3,4,5,6,7,8)
Severity = c(-3.56553994,-2.70296933,-1.63969850,-0.81321707,-0.04629182,
0.73721320,1.61278518,2.76647043,3.94804472)
x = data.frame(Raw.Score = Raw.Score, Severity = Severity)
Raw.score are raw numbers from 0 to 8 (let's consider them as the labels of the severity numbers)
Severity are relative numbres that represent the locations of the scores in the diagram
I want to graphically present the results as in the following example using ggplot (the example includes different numbers but I want something similar)
As a fun exercise in ggplot-ing here is one approach to achieve or come close to your desired result.
Raw.Score = c(0,1,2,3,4,5,6,7,8)
Severity = c(-3.56553994,-2.70296933,-1.63969850,-0.81321707,-0.04629182,
0.73721320,1.61278518,2.76647043,3.94804472)
dat <- data.frame(Raw.Score, Severity)
library(ggplot2)
dat_tile <- data.frame(
Severity = seq(-4.1, 4.1, .05)
)
dat_axis <- data.frame(
Severity = seq(-4, 4, 2)
)
tile_height = .15
ymax <- .5
ggplot(dat, aes(y = 0, x = Severity, fill = Severity)) +
# Axis line
geom_hline(yintercept = -tile_height / 2) +
# Colorbar
geom_tile(data = dat_tile, aes(color = Severity), height = tile_height) +
# Sgements connecting top and bottom labels
geom_segment(aes(xend = Severity, yend = -ymax, y = ymax), color = "orange") +
# Axis ticks aka dots
geom_point(data = dat_axis,
y = -tile_height / 2, shape = 21, stroke = 1, fill = "white") +
# ... and labels
geom_text(data = dat_axis, aes(label = Severity),
y = -tile_height / 2 - .1, vjust = 1, fontface = "bold") +
# Bottom labels
geom_label(aes(y = -ymax, label = scales::number(Severity, accuracy = .01))) +
# Top labels
geom_point(aes(y = ymax, color = Severity), size = 8) +
geom_text(aes(y = ymax, label = Raw.Score), fontface = "bold") +
# Colorbar annotations
annotate(geom = "text", fontface = "bold", label = "MILD", color = "black", x = -3.75, y = 0) +
annotate(geom = "text", fontface = "bold", label = "SEVERE", color = "white", x = 3.75, y = 0) +
# Fixing the scales
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(limits = c(-ymax, ymax)) +
# Color gradient
scale_fill_gradient(low = "orange", high = "red", guide = "none") +
scale_color_gradient(low = "orange", high = "red", guide = "none") +
# Get rid of all non-data ink
theme_void() +
# Add some plot margin
theme(plot.margin = rep(unit(10, "pt"), 4)) +
coord_cartesian(clip = "off")

How to type subscripts in geom_text labels in ggplot

I have timeseries data plotted and separated by timepoints that I'd like to label with subscripts. Below is the code I'm using to generate the figure and timepoint labels. I'd like for the -1, 3 and 6 to be subscripts. Thanks in advance!
timepoints=data.frame(date=as_datetime(c("2016-08-15" ,"2016-11-22",
"2017-02-25")), timepoint=c("T-1", "T3", "T6"))
TimeseriespH = ggplot(FinalSeaphox, aes(x=DTTM)) +
geom_line(aes(y=MpH, color = "Outer Bay", group = grp), size = 0.5) +
geom_line(aes(y=CpH, color = "Inner Bay", group = grp), size = 0.5) +
scale_x_datetime(labels = date_format("%b '%y"), date_breaks = "1
month", limits = as_datetime(c("2016-07-01","2017-04-19"))) +
labs(x = "", y = "pH") +
scale_y_continuous(limits = c(7.4,8.2)) +
geom_vline(xintercept = as_datetime("2016-12-01"), linetype = 2, color
= "black") +
geom_vline(xintercept = as_datetime("2016-08-26"), linetype = 2, color
= "black") +
geom_vline(xintercept = as_datetime("2017-03-06"), linetype = 2, color
= "black") +
geom_text(data=timepoints, mapping=aes(x=date, y=c(8.18, 8.18, 8.18),
label=timepoint), size=5, vjust=-0.4, hjust=0, inherit.aes = FALSE,
color = "black")
For the subscripts, you need to enclose between brackets:
timepoint = c("T[-1]", "T[3]", "T[6]")
Then use parse = TRUE in geom_text:
library(ggplot2)
library(lubridate)
timepoints=data.frame(
date = as_datetime(c("2016-08-15" ,"2016-11-22", "2017-02-25")),
Y = c(8, 8.1, 8)
timepoint = c("T[-1]", "T[3]", "T[6]")
)
ggplot(timepoints) +
geom_point(aes(x = date, y=Y), size = 3) +
geom_text(data=timepoints,
mapping=aes(x=date, y=c(8.18, 8.18, 8.18),
label = timepoint),
size=5, vjust=0.4, hjust=0, inherit.aes = FALSE,
color = "black", parse = TRUE)

R add legend for multiple layers

I want to add a legend for the plot, but it doesn't work,
can anyone please help me to see where it went wrong.
this is the code.
ggplot(data = dfNorm1, aes(x = X)) +
geom_col(aes(y = Government_suppliment),
fill = "#0000FF", color = "white", alpha = 0.8) +
geom_smooth(data = subset(dfNorm1,X >= 24), aes(y = Government_suppliment),
method = "lm", se = FALSE, color = "#FF4040",
linetype = "dashed", size = 0.7) +
geom_smooth(data = subset(dfNorm1, X <= 24), aes(y = Government_suppliment),
method = "lm", se = FALSE, color = "#FF4040",
linetype = "dashed", size = 0.7) +
geom_vline(xintercept = 24.5, size = 0.8, alpha = 0.8) +
geom_line(aes(y = Poverty_funds),
color = "#FF0000", size = 1, alpha = 0.7) +
geom_line(aes(y = MLI), color = "#EF3EFF", size = 1,
alpha = 0.8) +
scale_fill_manual(name = "",values = c("bar.label" = "#0000FF")) +
scale_color_manual(name = "", values = c("line.label1" = "#FF0000", "line.label2" = "#EF3EFF",
"line.labeld" = "#FF4040"))
You usually can produce a legend by setting aes(color = column_title) in one of your geom layers. This code doesn't particularly make sense because you are referencing more than one y-axis without creating a second y-axis (a bad habit if you are trying to do so). Is there a way you can post more relevant code or a reproducible example so people can see exactly what you're trying to do?

Extend bars on a ggplot2 to show the data labels not squished

Here is a data frame:
library(tidyverse)
example_df <- structure(list(Funnel = c("Sessions", "AddToCart", "Registrations", "ShippingDetails", "Checkout", "Transactions"), Sum = c(1437574, 385281, 148181, 56989, 35613, 29671), End = c(NA, 1437574, 385281, 148181, 56989, 35613), xpos = c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5), Diff = c(NA, 1052293, 237100, 91192, 21376, 5942), Percent = c("NA %", "73.2 %", "61.5 %", "61.5 %", "37.5 %", "16.7 %")), .Names = c("Funnel", "Sum", "End", "xpos", "Diff", "Percent"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))
And here is a ggplot2:
ggplot(example_df, aes(x = reorder(Funnel, -Sum), y = Sum)) +
geom_col(alpha = 0.6, fill = "#008080") +
stat_summary(aes(label = scales::comma(..y..)), fun.y = 'sum',
geom = 'text', col = 'white', vjust = 1.5) +
geom_segment(aes(x=xpos, y = End, xend = xpos, yend = Sum)) +
geom_text(aes(x=xpos,y = End-Diff / 2, label=Percent), hjust = -0.2) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
scale_y_continuous(labels = function(l) {l = l / 1000; paste0(l, "K")}) +
Here's what it looks like:
The values on the plot from Shipping Details: Transactions are tricky to read because the bars are smaller.
I wondered if there was a good approach to dealing with this. I tried extending the range with:
+ expand_limits(y = -100000)
But that just lowers the y axis.
Is there a sensible solution to visualizing the data points in a way they are not squished? If I could somehow lower the green bars into the minus region without impacting the proportions?
Very dirty solution, but works. Add dummy geom_bar's bellow each segment (ie., extend original segment by adding negative bar) with the same color and alpha.
Bars to add:
geom_bar(data = data.frame(x = example_df$Funnel, y = -2e4),
aes(x, y),
stat = "identity", position = "dodge",
alpha = 0.6, fill = "#008080")
Final code:
# Using OPs data
library(ggplot2)
ggplot(example_df, aes(x = reorder(Funnel, -Sum), y = Sum)) +
geom_col(alpha = 0.6, fill = "#008080") +
geom_segment(aes(x=xpos, y = End, xend = xpos, yend = Sum)) +
geom_text(aes(x=xpos,y = End-Diff / 2, label=Percent), hjust = -0.2) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
scale_y_continuous(labels = function(l) {l = l / 1000; paste0(l, "K")}) +
geom_bar(data = data.frame(x = example_df$Funnel, y = -2e4),
aes(x, y),
stat = "identity", position = "dodge",
alpha = 0.6, fill = "#008080") +
stat_summary(aes(label = scales::comma(..y..)), fun.y = 'sum',
geom = 'text', col = 'white', vjust = 1.5) +
theme_classic()
Plot:
PS:
You have to add stat_summary after geom_bar

ggplot2 Colour & Shape by different Factors

I have a data set with 2 factors (MACH & YOU) Id like to produce a BoxPlot using ggplot2 and have the BoxPlot colour split by MACH whilst highlighting certain points (YOU) in a different shape and in Black..?
I can get the plot working but i can't make the (YOU) factor be bigger in terms of shape and make it black...without effecting all other points on the graph.
Ignore the commented lines - I was just playing around with those.
My dataframe x has the form
MEDIAN MACH YOU PROD
34.5 tool1 false ME
33.8 tool1 false ME
32.9 tool2 true ME
30.1 tool2 true ME
33.8 tool2 false.....etc
x<- data.frame(MEDIAN=c(34,32,56,34,45,34,45,33,23), MACH=c("t1","t1","t1","t2","t2","t2","t1","t1","t2"), YOU=c("false","false","false","false","true","true","true","false","false"), PROD="U","U","U","U","U","U","U","U","U")
ggplot(data=x,aes(MACH,MEDIAN ))+
geom_boxplot(fill = "white", colour = "blue")+
theme(panel.grid.minor = element_line(colour = "grey"), plot.title = element_text(size = rel(0.8)),axis.text.x = element_text(angle=90, vjust=1), strip.text.x = element_text(size = 8, colour = "black", face = "bold")) +
#geom_abline(colour = "grey80")+
#geom_point(shape = factor(YOURLOTS)), size = 3) +
#geom_hline(yintercept=x$TARG_AVG,colour = "green")+
#geom_hline(yintercept=x$TARG_MIN,colour = "red")+
#geom_hline(yintercept=x$TARG_MAX,colour = "red")+
geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0), aes(colour=factor(MACH),shape = factor(YOU)), size =3)+
facet_wrap(~PROD, scales = "free") +
ggtitle("MyTitle") +
scale_size_area() +
xlab("STAGE HIST EQUIPID")+
ylab("yaxis")
If you want to make the points for YOU of different size, depending on their value, you can add aes(size = factor(YOU)) inside geom_point().
You can choose the range of size of the points adding scale_size_discrete(range = c(3, 6)) to you plot. In this example, the minimum size would be 3 and the maximum value would be 6.
That would be
ggplot(data = x, aes(MACH, MEDIAN)) +
geom_boxplot(fill = "white", aes(color = MACH)) +
geom_point(aes(shape = factor(YOU), size = factor(YOU)), color = "black", alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0)) +
labs(title = "My Title", x = "Stage Hist Equip ID", y = "y-axis") +
scale_size_discrete(range = c(3, 6))
I would solve this by using two subsets and two calls to geom_point():
library(ggplot2)
x <- data.frame(MEDIAN = c(34,32,56,34,45,34,45,33,23),
MACH = c("t1","t1","t1","t2","t2","t2","t1","t1","t2"),
YOU = c("false","false","false","false","true","true","true","false","false"),
PROD = c("U","U","U","U","U","U","U","U","U"))
ggplot(data = x, aes(MACH, MEDIAN)) +
geom_boxplot(fill = "white", colour = "blue") +
geom_point(data = subset(x, YOU != "true"), aes(color = MACH),
size = 8, alpha = 0.6,
position = position_jitter(w = 0.05, h = 0.0)) +
geom_point(data = subset(x, YOU == "true"), aes(shape = YOU),
color = "black", size = 8, alpha = 0.6,
position = position_jitter(w = -0.05, h = 0.0)) +
labs(title = "My Title", x = "Stage Hist Equip ID", y = "y-axis")

Resources