Tutorial:Making LDA Score plot
0
1
Entering edit mode
19 months ago

This blog post is dedicated to my mentor and people out there who are trying to make LDA Score plot or a normal bar chart as shown below. This bar chart contains legend flipped on the opposite sides of the bars unlike normal bar charts where the Y axis ticks are present on one side:

Data: Here I present a dummy data but in metagenomics data the first column is usually genus and second column is LDA score. I save it as Book3.xlsx.

name value
x 4
y 6
z 9
t 5
u 2
v -5
w -4
m -2
c -2
b -1

Code to make LDA Score bar chart

######### Loading libraries
library("dplyr")
library("ggplot2")
library(schoolmath)
library(ggtext)

########### Organising data
dummydf <- readxl::read_excel("Book3.xlsx")
dummydf$name <- as.factor(dummydf$name)
dummydf$class <- ifelse(is.positive(dummydf$value),"Positive","Negative")
dummydf$class <- as.factor(dummydf$class)
class(dummydf$name)

######Plotting LDA Score plot

ggplot(dummydf[order(dummydf$value),], aes(x = name, y = value)) +
geom_bar(aes(fill = class), stat = 'identity') +  
coord_flip() +  
geom_richtext(aes(y = 0, label = name, hjust = as.numeric(value > 0)),fill = NA, fontface="bold",label.color = NA) +
theme_minimal()+
theme(axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),axis.ticks = element_line(color="black", size = 0.3),
    axis.title.y = element_blank(),legend.position = "top",
    legend.justification = 0.05,text = element_text(face="bold",color="black",size = 15),
    legend.title = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),axis.line.x = element_line(color="black", size = 0.3),
    panel.grid.major.x = element_line(colour = "grey80", linetype = "dashed"),
    panel.grid.minor.x = element_blank())+scale_fill_manual(values = c( "turquoise1", "tan1"))+
scale_y_continuous(expression(log[10](italic("LDA score"))),
                     breaks = -10:10, limits = c(-10, 10))

enter image description here

LDA metagenomics • 920 views
ADD COMMENT

Login before adding your answer.

Traffic: 1949 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6