Plotting a tree - collapsing a vector of nodes - r

I am trying to plot a large tree using ggtree, but, due to its size, I would like to collapse multiple nodes. I am following a tutorial , but it collapses the nodes one at the time, and this is not an option in my case.
Here is my code:
library(ggtree)
library(ape)
library(ggplot2)
library(colorspace)
library(Biostrings)
library(phytools)
library(treeio)
library(dplyr)
library(readr)
library(tidyr)
library(reshape2)
tempnwk<- "((('clade01_1':1.35E-4,('clade01_2':1.0E-6,'clade01_3':1.0E-6):3.3E-5):3.3E-5,('clade02_1':2.7E-4,'clade02_2':3.3E-5):3.3E-5):1.0E-6,'clade03_1':1.0E-6);"
testTree0 <- read.tree(text = tempnwk)
#
testcollapse0<- ggtree(testTree0)
#Now, this works:
#
testcollapse0b<- testcollapse0 %>% collapse(node = 10) +
geom_point2(aes(subset=(node==10)),
shape=21, size=5, fill='green')
testcollapse0b<- collapse(testcollapse0b, node = 11) +
geom_point2(aes(subset=(node==11)),
shape=21, size=5, fill='red')
testcollapse0b ####This works
#
#
##############THis does not:
nodes2go<- c(10, 11)
myTestCols<- c('green', 'red')
testcollapse1<- testcollapse0
for(i in 1:2) {
testcollapse1<- collapse(
testcollapse1, node = nodes2go[i]) +
geom_point2(
aes(subset=(node==i)), shape=23,
size=7, fill=myTestCols[i])
}
rm(i)
#
testcollapse1 + geom_text(aes(label=label))
#
#Error in FUN(X[[i]], ...) : object 'i' not found
I need some help, I am not sure how to fix it. I had a look at drop.tip, but I am not sure that is what I want, since I still want a colored dot where the collapsed node is.
I am looking forward to your feedback, thank you for your kind attention.

Well,
While waiting for a sane way to do it, quick and dirty will do the job:
myTestCols2<- c("'green'", "'red'")
testcollapse2<- testcollapse0
teststring0<- "testcollapse2<- collapse(testcollapse2, node=NODE) + geom_point2(aes(subset=(node==NODE)), shape=23, size=7, fill=COLOR);"
testString2<- character()
for(i in 1:2) {
indString<- gsub(
pattern = "NODE",replacement = nodes2go[i],
x = teststring0)
indString<- gsub(
pattern = "COLOR", replacement = myTestCols2[i],
x = indString)
testString2<- c(testString2, indString)
}
rm(i, indString)
#
#Run the command
eval(parse(text = testString2))
##And now plot:
testcollapse2
And yes, I am aware that there must be a better way to do it 🙄

Related

graphical analysis of item theory in R with itan package

I am analyzing tests with the itan package which turns out to be an incredible weapon to analyze item and of the few that I know it will be possible to shape the graphics that this package returns, I will paste the codes as they are shown on your page
library(itan)
datos<-data(datos) #data that is already part of the itan package
clave<-data(clave)
respuestas <- datos[,-1]
alternativas <- LETTERS[1:5]
#Alternative frequency chart
g <- graficarFrecuenciaAlternativas(respuestas, alternativas, clave)
g$i01
g$i02
g$i03
g$i04
The general question is whether it is possible to change the aesthetics of these graphics to fit them to my project?
Doing some research I found the source code of the packet on the next page:
enter link description here
With which it will be enough to simply modify the following code
graficarFrecuenciaAlternativas <- function(respuestas, alternativas, clave=NULL) {
item <- ncol(respuestas)
fa <-calcularFrecuenciaAlternativas(respuestas, alternativas)
names <- colnames(fa)
output <- c();
for (i in 1:item) {
colnames(fa) <- ifelse(colnames(fa) == clave[[i]],
paste(c("*"), colnames(fa), sep = ""),
colnames(fa))
fam <- melt(fa[i,], id.vars = "item")
output[[i]] <- ggplot2::ggplot(fam, aes_string(x="variable", y="value", fill="variable")) +
ggplot2::geom_col(show.legend = F) +
ggplot2::labs(title = paste("\u00CDtem ", i),
x="Alternativa",
y="Frecuencia") +
ggplot2::theme(plot.title = element_text(size=18, face="bold" ,hjust=0.5))
colnames(fa) <- names
}
names(output) <- colnames(respuestas)
return(output);
}

I can't get my plots to a single grid please help correct my code

I have 11 plots and used a looping function to plot them see my code below. However, I can't get them to fit in just 1 page or less. The plots are actually too big. I am using R software and writing my work in RMarkdown. I have spent almost an entire week trying to resolve this.
group_by(Firm_category) %>%
doo(
~ggboxplot(
data =., x = "Means.type", y = "means",
fill ="grey", palette = "npg", legend = "none",
ggtheme = theme_pubr()
),
result = "plots"
)
graph3
# Add statistical tests to each corresponding plot
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
graph3.i <- graph3$plots[[i]] +
labs(title = Firm_category[i]) +
stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
print(graph3.i)
}
#output3.long data sample below as comments
#Firm_category billmonth Means.type means
#Agric 1 Before 38.4444
#Agric 1 After 51.9
Complete data is on my github: https://github.com/Fridahnyakundi/Descriptives-in-R/blob/master/Output3.csv
This code prints all the graphs but in like 4 pages. I want to group them into a grid. I have tried to add all these codes below just before my last curly bracket and none is working, please help me out.
library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))
library(ggpubr)
print(ggarrange(graph3.i[1:11], nrow = 4, ncol = 3))
I tried the gridExtra command as well (they all seem to do the same thing). I am the one with a mistake and I guess it has to do with my list. I read a lot of similar work here, some suggested
dev.new()
dev.off()
I still didn't get what they do. But adding either of them caused my code to stop.
I tried defining my 'for' loop function say call it 'XX', then later call it to make a list of graph but it returned NULL output.
I have tried defining an empty list (as I read in some answers here) then counting them to make a list that can be printed but I got so many errors.
I have done this for almost 3 days and will appreciate your help in resolving this.
Thanks!
I tried to complete your code ... and this works (but I don't have your 'stat.test' object). Basically, I added a graph3.i <- list() and replaced graph3.i in the loop ..
Is it what you wanted to do ?
library(magrittr)
library(dplyr)
library(rstatix)
library(ggplot2)
library(ggpubr)
data <- read.csv(url('http://raw.githubusercontent.com/Fridahnyakundi/Descriptives-in-R/master/Output3.csv'))
graph3 <- data %>% group_by(Firm_category) %>%
doo(
~ggboxplot(
data =., x = "Means.type", y = "means",
fill ="grey", palette = "npg", legend = "none",
ggtheme = theme_pubr()
),
result = "plots"
)
graph3
# Add statistical tests to each corresponding plot
graph3.i <- list()
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
graph3.i[[i]] <- graph3$plots[[i]] +
labs(title = Firm_category[i]) # +
# stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
print(graph3.i)
}
library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))

Weird characters appearing in the plot legend when using DoHeatmap

I was using Seurat to analyse single cell RNA-seq data and I managed to draw a heatmap plot with DoHeatmap() after clustering and marker selection, but got a bunch of random characters appearing in the legend. They are random characters as they will change every time you run the code. I was worrying over it's something related to my own dataset, so I then tried the test Seurat object 'ifnb' but still got the same issue (see the red oval in the example plot).
example plot
I also tried importing the Seurat object in R in the terminal (via readRDS) and ran the plotting function, but got the same issue there, so it's not a Rstudio thing.
Here are the codes I ran:
'''
library(Seurat)
library(SeuratData)
library(patchwork)
InstallData("ifnb")
LoadData("ifnb")
ifnb.list <- SplitObject(ifnb, split.by = "stim")
ifnb.list <- lapply(X = ifnb.list, FUN = function(x) {
x <- NormalizeData(x)
x <- FindVariableFeatures(x, selection.method = "vst", nfeatures = 2000)
})
features <- SelectIntegrationFeatures(object.list = ifnb.list)
immune.anchors <- FindIntegrationAnchors(object.list = ifnb.list, anchor.features = features)
immune.combined <- IntegrateData(anchorset = immune.anchors)
immune.combined <- ScaleData(immune.combined, verbose = FALSE)
immune.combined <- RunPCA(immune.combined, npcs = 30, verbose = FALSE)
immune.combined <- RunUMAP(immune.combined, reduction = "pca", dims = 1:30)
immune.combined <- FindNeighbors(immune.combined, reduction = "pca", dims = 1:30)
immune.combined <- FindClusters(immune.combined, resolution = 0.5)
DefaultAssay(immune.combined) <- 'RNA'
immune_markers <- FindAllMarkers(immune.combined, latent.vars = "stim", test.use = "MAST", assay = 'RNA')
immune_markers %>%
group_by(cluster) %>%
top_n(n = 10, wt = avg_log2FC) -> top10_immune
DoHeatmap(immune.combined, slot = 'data',features = top10_immune$gene, group.by = 'stim', assay = 'RNA')
'''
Does anyone have any idea how to solve this issue other than reinstalling everything?
I have been having the same issue myself and while I have solved it by not needing the legend, I think you could use this approach and use a similar solution:
DoHeatmap(immune.combined, slot = 'data',features = top10_immune$gene, group.by = 'stim', assay = 'RNA') +
scale_color_manual(
values = my_colors,
limits = c('CTRL', 'STIM'))
Let me know if this works! It doesn't solve the source of the odd text values but it does the job! If you haven't already, I would recommend creating a forum question on the Seurat forums to see where these characters are coming from!
When I use seurat4.0, I met the same problem.
While I loaded 4.1, it disappeared

How to use a custom-defined function to change a text label in geom_text()

I have some data, and I want to use some variables from stat_count() to label a bar plot.
This is what I want to do:
library(ggplot2)
library(scales)
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(aes(x=Type)) +
stat_count(aes(y=(..prop))) +
geom_text(aes(y=(..prop..), label=percent_and_count(..prop.., ..count))),
stat='count')
However, I get this error, since it can't find the function in what I assume is either some base packages or the data frame:
Error in eval(expr, envir, enclos) : could not find function "percent_and_count"
I get this error if I do percent(..prop..) as well, although it is fine with scales::percent(..prop..). I did not load my function from a package.
If all else fails, I can do
geom_text(aes(y=(..prop..), label=utils::getAnywhere('percent_and_count')$objs[[1]]((..prop..),(..count..))))
But this seems needlessly roundabout for what should be a stupidly simple task.
You can use bquote and aes_:
# Sample data
set.seed(2017);
df <- data.frame(
Type = sample(6, 100, replace = T)
);
library(ggplot2);
library(scales);
# Your custom function
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(df, aes(x = Type)) +
stat_count(aes(y = ..prop..)) +
geom_text(
stat = "count",
aes_(
y = ~(..prop..),
label = bquote(.(percent_and_count)((..prop..), (..count..)))))
Explanation: bquote(.(percent_and_count)(...)) ensures that percent_and_count is found (as terms .(...) are evaluated in the parent environment). We then use aes_ to ensure that quoted expressions (either with ~ or bquote) are properly evaluated.
Still not pretty, but probably more straighforward than using utils::getAnywhere.

Shiny : variable height of renderplot

I would like to take the height of my plot reactive because sometimes I have to draw just one graph and sometimes two or three graphs. Here my code :
output$myplot<-renderPlot({
plot_to_draw <- data[data$code==input$code,"River_name"]
plot(plot_to_draw)
number_of_plot <- length(plot_to_draw)
},height = 500*number_of_plot)
But shiny reads the height of the plot just one time so that it's not reactive.
Thank you for your answers !
I finally figuerd out a solution ;
server.R
output$myplot<-renderPlot({
plot_to_draw <- data[data$code==input$code,"River_name"]
plot(plot_to_draw)
number_of_plot <- length(plot_to_draw)
},height = function(){500*number_of_plot})
ui.R
plotOutput(outputId="myplot",height = "auto")
This is the solution that I finally got, after drudging with my app and thanks to all persons in this thread for your kind suggestions. Please don't mind the name of the variables.
In the server part:
#I had to transform my imput into a data.frame, otherwise sqldf didn't work.
country12<- reactive({as.data.frame(matrix(c(input$sel_country121),1,1))})
question12<-reactive({
country121 <- country12()
sqldf("SELECT dp.Year, dp.Type_of_Product COUNT (*) as num_products12 FROM dataPanelV5 dp, country121 p WHERE dp.Country_name = p.V1 GROUP BY dp.Year, dp.Type_of_Product")})
#I use this function to calculate the number of different types of products resulting from the query, using unique() and calculating its length, as that number is the number of facets.
n_facets12<-function(){
question121<- question12()
return (500*length(unique(question121$Type_of_Product)))}
output$barplot12 <- renderPlot({
question121<-question12()
ggplot(question121,aes(x=factor(Year),y=num_products12,fill=Type_of_Product)) + geom_bar(stat="identity") + facet_grid(Type_of_Product ~ .,scales = "free_y") +
geom_text(aes(label=num_products12), vjust=-0.2, colour="black") + scale_x_discrete(breaks=question121$Year,labels=as.character(question121$Year),position = "top") + theme(legend.position="top",axis.title.y=element_blank(),axis.text.y = element_blank(),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank()) + labs(fill="Type_of_Product", x="Year")
},height = n_facets12)
#And it works!!

Resources