How do i change facet_grid position from side to up - r

Im using this code:
bp <- ggplot(data2, aes(x="", y=percentage, fill=pangolin_lineage)) + geom_bar(width = 2, stat = "identity",alpha = 1.2) +
#scale_fill_manual(values = getPalette(colourCount)) +
scale_fill_manual(values=cal6) + facet_grid(year ~ month, switch = "x", space = "free") + #facet_wrap(year ~ month, nrow=1, scales="free_x", strip.position = c("top"))
ylab("Percentage of sequences") + xlab(NULL) + #titulos
theme_minimal()+ scale_y_continuous(labels=scales::percent)+ theme(strip.text = element_text(size=18))
And I need year to be showed up while month is showed down. I wonder if there is a way to fix this a make it use the same percentage scale too. This is the plot I'm getting with this code ):
graph

Related

Can I reorder the position of samples in a ggplot barplot

I created a stacked bar chart using ggplot and the following code:
ggplot(group, aes(x = variable, y = value, fill = Taxa)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(values = Cb64k) +
scale_y_continuous(labels = percent_format()) +
theme(legend.position = "bottom", text=element_text(size=10.5),
axis.text.x = element_text(angle=0, vjust=1)) +
guides(fill = guide_legend(ncol=6)) +
facet_grid(cols=vars(group), scales = "free_x", space = "free_x") +
ggtitle(opt$gtitle) +
xlab("Patient ID") + ylab("Relative Activity")
To get this output:
Is there a way to reorder the "stacks" in each bar so that the size of the stacks go from largest to smallest starting at the bottom? As you can see with the current output it seems to be random.

Adjusting percentage decimals for a bar plot with facet_grid()

I have the following line:
p1 <- ggplot(mtcars, aes(x= cyl)) + geom_bar(aes(fill = vs), stat = "count") + geom_text(aes(label = scales::percent(..prop..), ymax= ..prop..), stat = "count", vjust = -0.5) + theme_classic() + ylab("Count") + facet_grid(vs ~ .) + ylim(0, 15)
which gives this plot. This is a plot where I want to keep the count integers on the y-axis, but I want the percentages displayed above each bar.
I would like to edit the number of decimals over each bar plot. However, when using the line below:
p2 <- ggplot(mtcars, aes(x= cyl)) + geom_bar(aes(fill = vs), stat = "count") + geom_text(aes(label = scales::percent(round((..count..)/sum(..count..),1)), ymax= ((..count..)/sum(..count..))), stat="count", vjust = -.25) + theme_classic() + ylab("Count") + facet_grid(vs ~ .) + ylim(0, 15)
The percentages are now off (see below), displaying the percentages for the whole plot, and not the separated facets. Is there a way to round the percentages without compromising the numbers?
You can use accuracy = 2 in the scales::percent function:
p1 <- ggplot(mtcars, aes(x= cyl)) + geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(label = scales::percent(..prop.., accuracy = 2), ymax= ..prop..), stat = "count", vjust = -0.5) +
theme_classic() + ylab("Count") + facet_grid(vs ~ .) + ylim(0, 15)
p1
There is an accuracy option in scales::percent:
p1 <- ggplot(mtcars, aes(x= cyl)) +
geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(label = scales::percent(..prop..,accuracy=2)),
stat = "count", vjust = -0.5) +
theme_classic() + ylab("Count") + facet_grid(vs ~ .) + ylim(0, 15)

Reordering Plot and Changing Axis Size (R ggplot)

I'm trying to change in both of my plots, the order and the x axis size for both. These are not being able to be changed accordingly
DF Creation
contig_count_average <- data.frame(Genome_Type = c("MBT", "Anglucyclines", "Whole Genome"),
Contig_Count_Average = c("2.91","83.7","608.3"))
Plot
p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type, Contig_Count_Average), Contig_Count_Average, fill = Genome_Type)) +
xlab("Genome") +
ylab("Contig No.") +
ggtitle("Contig Count per Genome Distribution") +
geom_bar(stat = "identity") +
theme(text = element_text(size=20),
axis.text.x = element_text(angle=90, hjust=1)) +
guides(fill=guide_legend(title="Genome Type")) +
coord_flip() +
theme_bw() +
scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
scale_x_discrete(labels = abbreviate)
p
I get the following warning:
1: In Ops.factor(Contig_Count_Average) : ‘-’ not meaningful for factors
The issue is because Contig_Count_Average is treated as factors in contig_count_average.
We can change it to numeric by doing either :
contig_count_average <- type.convert(contig_count_average, as.is = TRUE
Or
contig_count_average$Contig_Count_Average <- as.numeric(as.character(contig_count_average$Contig_Count_Average))
and then use the ggplot code.
p2 <- ggplot(contig_count_average, mapping = aes(x = reorder(Genome_Type,
Contig_Count_Average), Contig_Count_Average, fill = Genome_Type)) +
xlab("Genome") +
ylab("Contig No.") +
ggtitle("Contig Count per Genome Distribution") +
geom_bar(stat = "identity") +
theme(text = element_text(size=20),
axis.text.x = element_text(angle=90, hjust=1)) +
guides(fill=guide_legend(title="Genome Type")) +
coord_flip() +
theme_bw() +
scale_y_continuous(limits = c(0,2835), expand = c(0, 0)) +
scale_x_discrete(labels = abbreviate)
p2
Also note that you can use geom_col instead of geom_bar(stat = "identity").

Scale the distance between ticks on x-axis in gglot r

I have a graph like this. I am interested in the minor change between the range (-3.5, 0.5), however, the occupied only a small portion of the x-axis, so that it's hard to interpret.
I tried to use transform to log scale for better visualization, however, it apparently not works for negative values.
So is there any method to expand this region to make the graph look nicer?
Code:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18))
Something like this
library(ggforce)
library(ggolot2)
ggplot(mtcars, aes(x=mpg, y=disp, group=1)) +
geom_line() + facet_zoom(xlim = c(15, 20))
You may try adding the xlim = c(minor value, major value) option of ggplot, and use the range which works better for you
Something like that:
ggplot() + geom_line(data = Final_diction, aes(x = Final_diction[,1], y
= Final_diction[,4])) +
xlim(-3.5,20) +
geom_vline(xintercept=c(-0.5,0.5), linetype="dashed", color = "red") +
geom_vline(xintercept=c(-0.25,0.25), linetype="dashed", color = "blue") +
theme_bw() +
theme(axis.title = element_text(size = 20)) +
theme(axis.text = element_text(size = 18)) +
xlim = c(-4, 1)

Change the font size of variable names in ggplot

I am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides
new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())

Resources