I am trying to rotate labels (not variable names) for a plot I have created, and I am struggling to find and adopt any workable solution. As you can see the labels are not readable I the current form.
Plot is looking into reasons for planning permission objections, and variables are Income, politics, Sex, and Attitude to new homes in their region.
Here is the code. Last iteration, includes labeling function, but does not have any effect on the plot.
library(vcd)
mosaic(~Sex+HomsBultBPV+HHIncQV++PartyID, data=BSA, shade=TRUE,
labeling_args = list(set_varnames = c(Sex="Gender", HomsBultBPV="Attitude To Homes Built in Area",
PartyID="Political Affiliation", HHIncQV="Income Quartile",
labeling= labeling_border(rot_labels = c(25,25,25,0),
just_labels = c("left",
"center",
"center",
"center")))))
The solution is this parameter: "vcd::labeling_border(rot_labels = c(__, __))"
Try out this code:
library(vcd)
mosaic(~ Sex + Age + Survived, data = Titanic,
main = "Survival on the Titanic", shade = TRUE, legend = TRUE,
labeling = vcd::labeling_border(rot_labels = c(45, 45)))
Related
I'm trying it again since it seems like a beginners problem, I hope someone can help me..
I have the following model which I want to plot via the "plot_model" function:
ols4 <- feols(instrastate_war_recurrence ~ Governance.Score + log(1+conflict_cntr_count_region_7_l1) + log(gdppc_out) |time_since_conflict,data=data1)
I tried the following:
forestmodel <- plot_model(ols4, labels =c("Governance Score", "Conflict in Region", "GDP per Capita"), type = "est", title = "Effects on Intrastate War Recurrence", show.values = "TRUE", digits = 4, show.p = TRUE, show.legend = TRUE)
And got the following:
https://docs.google.com/document/d/1yBVRxS8AchJ8jjt0aA6F1GZGM6HeWbTTD6aFl2oHESw/edit?usp=sharing
How can I rename/change the labels of the variables "(1+conflict_cntr_count_region_7_l1)" and "gdppc out (log)" in the forest plot?
(I only want the variable names and not the additional transformations like log/ 1 + etc.)
Thanks again :)
Data (Excel sheet): https://docs.google.com/spreadsheets/d/1nqQajKefmu2dgexQKMMcgsvh70G6aO4G/edit?usp=sharing&ouid=107305854232845923897&rtpof=true&sd=true
I would like to add line of text under the whole plot. However, it seems ggsurvplot handles plot and risk-table as two entities. I would like to have it like this: enter image description here
However, this is added in MS Word and the journal asks to have it embedded in the picture itself and I am unable to do that.
Thank you :-)
ggsurvplot(fit = fit, data = dat, pval = TRUE,
color = "black",
risk.table = T,
break.time.by = 12,
surv.scale = "percent",
linetype = c("solid","dotted", "dashed"),
legend.labs = c("Control group", "TMA R-ve", "TMA R+ve"),
censor.shape = 124,
legend.title = "",
title = "5-years death-censored graft survival",
xlab = "Months from transplantation",
ylab = "Survival (%)")
I suppose there may be multiple approaches to laying out the plot, table, and text caption. Here is one way I thought might be easier to work with.
The ggsurvplot object, if you include the risk table, will have two ggplot objects contained in it, one for the curve plot, and one for the table (the table itself is a plot).
You can just add to the table plot a caption, and this will appear below the table in the end. If you include hjust = 0 with plot.caption it will be left justified.
Here is an example:
library(survival)
library(survminer)
fit <- survfit(Surv(time, status) ~ sex, data = lung)
ggsurv <- ggsurvplot(fit, risk.table = TRUE)
ggsurv$table <- ggsurv$table +
theme(plot.caption = element_text(hjust = 0)) +
labs(caption = "Figure 1: 5-years death-censored graft survival")
ggsurv
I am trying to optimize a sjPlot for black and white printing.
My plot looks at essentially numbers of parasites, depending on the amount of rain and is categorized by breeding status.
My data is broken up into groups of bachelor and breeder/territorial males.
While I can break up the prediction lines (GLMM pred models) into dashed and solid for the two groups when using "bw" for colors in the plot.model function, I cannot change the shape (or even grayscale color) of the raw data points for the two groups.
I can split the colors of the data points by group when using a different color scheme, but this distinction disappears with "bw" (all of the points are the same shade).
Key Takeaway: Is there a way to maintain a dashed and solid line distinction between two prediction lines (achieved using [colors="bw"]) and to either change the shape or color of raw data points by group in sjPlot? Thank you in advance!
I have tried using various additions to change shape; however, nothing has worked. Here are a few of the code additions to the plot that I have tried:
aes(shape=Reproductive_Status)
dot.shape="Reproductive_Status"
group="Reproductive_Status"....shape=group
Code: set up of the GlmmTMB model
strong.2.int.y<-glmmTMB(log.strongyle.
~ Rainfall_2MDelay*Reproductive_Status+age_y+(
1 | Animal_ID),data=parasites,na.action=na.exclude)
Code: plot; "aes(shape=Reproductive_Status)" does nothing to change point shape in this function unfortunately
test2.plot_strong.2.int.y.bw<-plot_model(strong.2.int.y, type ="pred", terms = c("Rainfall_2MDelay","Reproductive_Status"),
sort.est = NULL, rm.terms = NULL, group.terms = NULL, pred.type = "fe",
title = "(a)", axis.title = NULL, axis.labels = NULL, legend.title = NULL,
colors = "bw", show.intercept = FALSE, show.values = TRUE, show.p = F,
show.data = T, show.legend = TRUE, digits = 2, show.scatter=T,
dot.size = 2, dot.alpha=0.3,line.size = 1, vline.color = NULL,
aes(shape=Reproductive_Status))
+ labs(x=expression('Rainfall, R'[t-2]*' (mm)'),
y="Strongyle Count (log[x])")
+ theme(plot.title = element_text(hjust=0.0))
plot(test2.plot_strong.2.int.y.bw,add=TRUE)
For marginal effects plots (i.e. when type = "pred"), sjPlot simply calls functions from ggeffects. So if you're familiar with ggplot2, I suggest you use directly ggpredict(), which returns the underlying data that is needed for plotting.
You can also try ggeffects' plot()-method, and then try to customize it. There are two vignettes describing how to customize the plots:
Plotting Marginal Effects and Customize Plot Appearance.
Hope that helps.
I am trying to create a function to map different variables for a specific state's school districts. However I a running into some problems formatting the legend. At the moment I have it laid out the best way to fit the maps(horizontally), but some of the text is being cut off (Below Average Poverty Rate), and I'd like to add % to the number labels in the legend. My code and an image of the legend is below. Any help you can provide would be very much appreciated. Thank You.
MakeLEAMap <-function(StateNum,NCHE_VAR,VAR1_NAME,In,Folder){
as.character(substitute(StateNum))
as.character(substitute(NCHE_VAR))
as.character(substitute(NCHE_In))
as.character(substitute(VAR1_NAME))
as.character(substitute(Folder))
map <-
tm_shape(LEA_1415_New[LEA_1415_New#data$STATEFP == StateNum, ]) +
tm_polygons(NCHE_VAR,border.col="#000000", lwd= .5, textNA="Below Average \nPoverty Rate" , palette = 'Blues', style="quantile",
title=paste(In," State LEA Map: ",VAR1_NAME),
legend.is.portrait = FALSE) +
tm_text("LCITY", size=NCHE_VAR,scale=.8, root=2,print.tiny = FALSE, size.lowerbound = .85, bg.alpha = .75,
remove.overlap = TRUE,legend.size.show = FALSE, col="black") +
tm_layout( legend.title.size = 3,
frame = FALSE, inner.margins = c(0,.0,.05,.0), asp = 1.5,
legend.text.size = 1,
legend.outside=TRUE, legend.outside.position = 'bottom',
legend.frame = TRUE,
legend.outside.size = .3, legend.position = c(-0.1, 0.3))
save_tmap(map, filename=paste("State_Maps_TEST/",Folder,"/",In,".pdf", sep = ''),width=8, height=8 )
}
MakeLEAMap("48","Abv_Diff_Home_Pov","% Children in Poverty minus \n% Children HCY (Ages5-17)",
"TX","ALL")
Here is what the legend looks like now
To make the legend show percentages use this function inside your tm_polygons call:
legend.format=list(fun=function(x) paste0(formatC(x, digits=0, format="f"), " %"))
You can play with the digits (decimal points) and you can drop the space before % sign if you desire.
To make the legend more legible increase the space around your map by making a bigger bbox (possibly using extent function from raster package to read bbox of your spatial object and then enlarging it) and move the legend by adjusting its position.
This is what I came up with in a different context, but one which also called for a percentage sign in tmap legend.
I am trying to visualize my data flow with a Sankey Diagram in R.
I found this blog post linking to an R script that produces a Sankey Diagram; unfortunately, it's quite raw and somewhat limited (see below for sample code and data).
Does anyone know of other scripts—or maybe even a package—that is more developed? My end goal is to visualize both data flow and percentages by relative size of diagram components, like in these examples of Sankey Diagrams.
I posted a somewhat similar question on the r-help list, but after two weeks without any responses I'm trying my luck here on stackoverflow.
Thanks,
Eric
PS. I'm aware of the Parallel Sets Plot, but that is not what I'm looking for.
# thanks to, https://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/
sourc.https <- function(url, ...) {
# install and load the RCurl package
if (match('RCurl', nomatch=0, installed.packages()[,1])==0) {
install.packages(c("RCurl"), dependencies = TRUE)
require(RCurl)
} else require(RCurl)
# parse and evaluate each .R script
sapply(c(url, ...), function(u) {
eval(parse(text = getURL(u, followlocation = TRUE,
cainfo = system.file("CurlSSL", "cacert.pem",
package = "RCurl"))), envir = .GlobalEnv)
} )
}
# from https://gist.github.com/1423501
sourc.https("https://raw.github.com/gist/1423501/55b3c6f11e4918cb6264492528b1ad01c429e581/Sankey.R")
# My example (there is another example inside Sankey.R):
inputs = c(6, 144)
losses = c(6,47,14,7, 7, 35, 34)
unit = "n ="
labels = c("Transfers",
"Referrals\n",
"Unable to Engage",
"Consultation only",
"Did not complete the intake",
"Did not engage in Treatment",
"Discontinued Mid-Treatment",
"Completed Treatment",
"Active in \nTreatment")
SankeyR(inputs,losses,unit,labels)
# Clean up my mess
rm("inputs", "labels", "losses", "SankeyR", "sourc.https", "unit")
Sankey Diagram produced with the above code,
This plot can be created through the networkD3 package. It allows you to create interactive sankey diagrams. Here you can find an example. I also added a screenshot so you have an idea what it looks like.
# Load package
library(networkD3)
# Load energy projection data
# Load energy projection data
URL <- paste0(
"https://cdn.rawgit.com/christophergandrud/networkD3/",
"master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30)
I have created a package (riverplot) that has a slightly different, but overlapping functionality compared to the Sankey function, and can produce plots like this one:
If you want to do it with R, your best bid seems to be #Roman suggestion - hack the SankeyR function. For example - below is my very quick fix - simply orient labels verticaly, slighlty offset them and decrease the font for input referals to make it look a bit better. This modification only changes line 171 and 223 in the SankeyR function:
#line171 - change oversized font size of input label
fontsize = max(0.5,frInputs[j]*1.5)#1.5 instead of 2.5
#line223 - srt changes from 35 to 90 to orient labels vertically,
#and offset adjusts them to get better alignment with arrows
text(txtX, txtY, fullLabel, cex=fontsize, pos=4, srt=90, offset=0.1)
I am no ace in trigonometry, but this is really what you need for changing the direction of arrows. That would be ideal in my view - if you could adjust looses arrows so they are oriented horizontally rather then vertically. Otherwise, why my solution fixes the problem with labels orientation, it doesn't make the diagram much more readable...
In addition to rCharts, Sankey diagrams can now be also generated in R with googleVis (version >= 0.5.0). For example, this post describes the generation of the following diagram using googleVis:
R's alluvial package will also do this (from ?alluvial).
# install.packages(c("alluvial"), dependencies = TRUE)
require(alluvial)
# Titanic data
tit <- as.data.frame(Titanic)
# 4d
alluvial( tit[,1:4], freq=tit$Freq, border=NA,
hide = tit$Freq < quantile(tit$Freq, .50),
col=ifelse( tit$Class == "3rd" & tit$Sex == "Male", "red", "gray") )
plotly has the same power as networkD3 package (example link).
For completeness, there is also the ggalluvial package which is a ggplot2 extension for alluvial/Sankey diagrams.
Here is an example taken from the package's documentation
# devtools::install_github("corybrunson/ggalluvial", ref = "optimization")
library(ggalluvial)
titanic_wide <- data.frame(Titanic)
ggplot(data = titanic_wide,
aes(axis1 = Class, axis2 = Sex, axis3 = Age,
y = Freq)) +
scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.1, .05)) +
xlab("Demographic") +
geom_alluvium(aes(fill = Survived)) +
geom_stratum() + geom_text(stat = "stratum", label.strata = TRUE) +
theme_minimal() +
ggtitle("passengers on the maiden voyage of the Titanic",
"stratified by demographics and survival") +
theme(legend.position = 'bottom')
ggplot(titanic_wide,
aes(y = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 0, reverse = FALSE) +
guides(fill = FALSE) +
geom_stratum(width = 1/8, reverse = FALSE) +
geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
scale_x_continuous(expand = c(0, 0),
breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
scale_y_discrete(expand = c(0, 0)) +
coord_flip() +
ggtitle("Titanic survival by class and sex")
Created on 2018-11-13 by the reprex package (v0.2.1.9000)
Judging by these definitions this function, like the Parallel Sets Plot, lacks the capacity to split and combine flows (i.e. through more than one transition).
Since Sankey diagrams are directed weighted graphs, a package like qgraph might be useful.
The SankeyR function provides clearer labels if you sort the losses in descending order as the text is placed closer to the arrow heads without overlapping.
have a look at //sankeybuilder.com as it offers a ready to go solution where you can upload your data and playback variations over time. The transition works well (similar to the youtube demo in your question). If you load the SankeyTrend demo it includes many time slots (Years of data). Once loaded (builds sankeys automatically), click the play button in the upper right hand corner of the page for playback of the time slots, you can even pause and resume time. Demo url is here: SankeyTrend Hope this helps your quest for the perfect Sankey diagram.
Just open sourced a package that uses an alluvial diagram to visualize workflow stages. Since history is kept when the alluvial form is used, there aren't any crossovers in the edges.
https://github.com/claytontstanley/shiny.alluvial