How to solve the issue of the group title being cut off? - r

this is my first time to ask questions in stack overflow.
Recently I have done a bit of data visualization on the Amino Acids by making a multiple pie charts
First, this is my dataset
library(ggplot2)
df = data.frame(Species <-c('Chicken','Chicken','Chicken','Chicken','Chicken','Human','Human','Human','Human','Human','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Crab-eating macaque','Mouse','Mouse','Mouse','Mouse','Mouse','Zebrafish','Zebrafish','Zebrafish','Zebrafish','Zebrafish'),
Amino_acids <- c('E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others','E','R','G','P','Others'),
value <- c(18,6,10,9,57,26,14,8,5,46,29,15,10,4,42,23,17,7,5,48,31,4,13,7,46))
df$Species <- factor(df$Species)
df$Amino_acids <- factor(df$Amino_acids)
Then, using ggplot2 to make the data visualization
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) + facet_wrap(.~Species, strip.position="top")+theme_void()
and the result is this
Results](https://i.stack.imgur.com/8EheY.png)
The problem is I dont know why some of the group (species) titles are being cut off, for example, crab-eating macaque, that is very confusing to me. Also, I have tried hjust on the axis.title.x and y already and it seems no use at all.
Can anyone solve this issue for me? I would rly appreciate it if you can.

You're looking to edit strip.title.x. You have several ways to go around this. I'd prefer editing the box size for the text:
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) +
facet_wrap(.~Species) +
theme_void() +
theme(strip.text.x = element_text(margin = margin(1,0,1,0), "cm"))
However, you could also adjust the vertical position:
ggplot(data=df, aes(x=" ", y=value, group=Amino_acids, colour=Amino_acids, fill=Amino_acids)) +
geom_bar(width = 1, stat = "identity", position= "fill") +
coord_polar("y", start=0)+
facet_grid(.~ Species) +
facet_wrap(.~Species) +
theme_void() +
theme(strip.text.x = element_text(vjust = 1))
Both should result in this:

Related

ggplo2 sample size annotation: How do you place the sample size under the x-axis?

I tried with the "solutions" from other posts without success. So, I am trying to add to my ggplot2 barplot the sample size per group under the names at the x-axis. I am using the stat_n_text(), but it does not allow you to change the position outside the chart. Does anyone know how to do this, or is there any other approach to adding the sample size per group?
Here is my code and my output.
ggplot(data, aes(group, pm_l, fill=condition)) + theme_classic() +
geom_bar(position = position_dodge(), stat = "identity") +
facet_wrap(~parameter, scales = "free") +
ylab("pm_l") +
scale_fill_brewer(palette = "Paired") +
stat_n_text(y.pos = 0)
I want the n=X to be named MICRO and ONE in each plot.
Any help or suggestions are highly appreciated!!!!
Maybe you can try something like this. As we don't have the data, this is just an example:
library(ggplot2)
df <- data.frame(ColA = c("MICRO","MICRO","MICRO","ONE","ONE","ONE"),
ColB = c(6,-5,9,-2,2,-1),
group = c("FLUX","ROXn","ROXn","FLUX","ROXn","ROXn"),
condition = c("PRE","POST","PRE","POST","PRE","POST"))
ggplot(df,aes(ColA,ColB,fill = condition, color = condition)) +
geom_bar(stat="identity") +
facet_wrap(~group) +
theme_minimal() +
ylab("pm_l") +
xlab("group") +
scale_fill_brewer(palette = "Paired")
OUTPUT:

ggplot2: doughnuts, how to conditional color fill with if_else

Following guides like ggplot Donut chart I am trying to draw small gauges, doughnuts with a label in the middle, with the intention to put them later on on a map.
If the value reaches a certain threshold I would like the fill of the doughnut to change to red. Is it possible to achieve with if_else (it would be most natural but it does not work).
library(tidyverse)
df <- tibble(ID=c("A","B"),value=c(0.7,0.5)) %>% gather(key = cat,value = val,-ID)
ggplot(df, aes(x = val, fill = cat)) + scale_fill_manual(aes,values = c("red", "yellow"))+
geom_bar(position="fill") + coord_polar(start = 0, theta="y")
ymax <- max(df$val)
ymin <- min(df$val)
p2 = ggplot(df, aes(fill=cat, y=0, ymax=1, ymin=val, xmax=4, xmin=3)) +
geom_rect(colour="black",stat = "identity") +
scale_fill_manual(values = if_else (val > 0.5, "red", "black")) +
geom_text( aes(x=0, y=0, label= scales::percent (1-val)), position = position_dodge(0.9))+
coord_polar(theta="y") +
xlim(c(0, 4)) +
theme_void() +
theme(legend.position="none") +
scale_y_reverse() + facet_wrap(facets = "ID")
Scale fill manual values= if else.... this part does not work, the error says: Error in if_else(val > 0.5, "red", "black") : object 'val' not found. Is it my error, or some other solution exists?
I also realize my code is not optimal, initially gather waited for more variables to be included in the plot, but I failed to stack one variable on top of the other. Now one variable should be enough to indicate the percentage of completion. I realise my code is redundant for the purpose. Can you help me out?
A solution for the color problem is to first create a variable in the data and then use that to map the color in the plot:
df <- tibble(ID=c("A","B"),value=c(0.7,0.5)) %>% gather(key = cat,value = val,-ID) %>%
mutate(color = if_else(val > 0.5, "red", "black"))
p2 = ggplot(df, aes(fill=color, y=0, ymax=1, ymin=val, xmax=4, xmin=3)) +
geom_rect(colour="black",stat = "identity") +
scale_fill_manual(values = c(`red` = "red", `black` = "black")) +
geom_text( aes(x=0, y=0, label= scales::percent (1-val)), position = position_dodge(0.9))+
coord_polar(theta="y") +
xlim(c(0, 4)) +
theme_void() +
theme(legend.position="none") +
scale_y_reverse() + facet_wrap(facets = "ID")
The result would be:

Controlling the total width of a barplot

How to get rid of all this space where the blue lines are?
Data:
data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))
data$is_repeat = factor(data$is_repeat, levels = c(0,1),
labels = c("One-time", "Repeat"))
Plot:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat = "identity", width = 0.3) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
theme_minimal()
edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.
another edit: Added data.
If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:
geom_bar(stat="identity", position="stack", width=1)
or theme(aspect.ratio=1)
And to remove the space from the end of the plot to the bars you need
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat"))
So your code looks like this:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat="identity", position="stack", width=1) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
theme_minimal()
And the output:
You can add space between bars with changing the width=1

How to add multiple geom_hlines with color equal to grouping variable

I've created a grouped boxplot and added three specific geom_hlines to the plot. However, I want to set the hline colors to fill=factor(Training.Location), rather than trying to match the colors manually with a color palette. Is there a way to do this?
ggplot(aes(x=factor(CumDes),y=Mn_Handle), data=NH_C) +
geom_boxplot( aes(fill=factor(Training.Location))) +
geom_point( aes(color=factor(Training.Location)),
position=position_dodge(width=0.75) ) +
theme(axis.ticks = element_blank(), axis.text.x = element_blank()) +
coord_cartesian(ylim = c(0, 2000)) +
geom_hline(yintercept=432, linetype="dashed", lwd=1.2) +
geom_hline(yintercept=583, linetype="dashed", lwd=1.2) +
geom_hline(yintercept=439, linetype="dashed", lwd=1.2)
This is the sort of thing that seems easiest with a new dataset. I'm not sure how you are calculating the values you are using for the horizontal lines, but often times I want to calculate these from the original dataset and use some sort of aggregation function/package for that.
Here is a modified example from the help page for geom_hline.
Make the dataset to give to geom_hline, including the values for the horizontal lines as well as the grouping variable.
mean_wt = data.frame(cyl = c(4, 6, 8), wt = c(2.28, 3.11, 4.00))
Then just plot with the new dataset for that layer, using whatever aesthetic you wish with the grouping variable.
ggplot(mtcars, aes(x = factor(vs), wt) ) +
geom_boxplot(aes(fill = factor(cyl))) +
geom_point(aes(color = factor(cyl)), position = position_dodge(.75)) +
geom_hline(data = mean_wt, aes(yintercept = wt, color = factor(cyl)) )
Here's a somewhat hackish solution (I had to improvise on the data, feel free to improve)
# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
col <- c("#CC6666", "#9999CC", "#66CC99")
ggplot(mtcars, aes(x = factor(cyl), y=mpg)) +
geom_boxplot(aes(fill=gear)) +
geom_point( aes(color=factor(gear)),
position=position_dodge(width=0.75) ) +
scale_colour_manual(values= col) +
theme(axis.ticks = element_blank(), axis.text.x = element_blank()) + coord_cartesian(ylim = c(8, 35)) +
geom_hline(yintercept=12, linetype="dashed", lwd=1.2, color=col[1]) +
geom_hline(yintercept=18, linetype="dashed", lwd=1.2, color=col[2]) +
geom_hline(yintercept=28, linetype="dashed", lwd=1.2, color=col[3])

R: Combine pie charts with ggplot2

EDITED
I have the following example where I create 3 pie charts , but I would like to have them 3 combined into 1 pie + donuts pie.
Besides, it would be really useful to have the numbers as well, how can this be accomplished? Thanks a lot.
df.mut <- data.frame(Avrg.muts.HLA.A11.A24=c(20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087,20.20000,37.39286,11.85714,50.26087), Avrg.muts.HLA.A11=c(32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973,32.86842,32.86842,35.72973,35.72973), Avrg.muts.HLA.A24=c(15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608,15.33333,43.19608), variable=c("HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11.A24","HLA.A11","HLA.A11","HLA.A11","HLA.A11","HLA.A24","HLA.A24","HLA.A24","HLA.A24"), value=c("+/+","+/-","-/+","-/-","+","+","-","-","+","-","+","-"))
df.mut$variable <- factor(df.mut$variable, levels=unique(df.mut$variable))
png(file="IMAGES/test1.png")
print(
ggplot(df.mut, aes(x="")) +
facet_grid(variable~., scales="free_y") +
geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
aes(x='0', y=Avrg.muts.HLA.A11.A24, fill=value), width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A11'),
aes(x='1', y=Avrg.muts.HLA.A11, fill=value), width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A24'),
aes(x='2', y=Avrg.muts.HLA.A24, fill=value), width = 1, stat = "identity") +
ggtitle("TEST1") +
theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
scale_y_continuous(name="") +
scale_x_discrete(name="") +
coord_polar(theta="y")
)
dev.off()
This produces the following image:
However, when I try to having the 3 of them together, the best I get is this mess:
How can I combine the pie charts above? And include numbers.
This should get you started:
df.test <- data.frame(genotype.1=c("+","+","-","-"), genotype.2=c("+","-","+","-"), count=c(345,547,678,987))
require(ggplot2)
require(grid)
ggplot(df.test, aes(y = count)) +
geom_bar(aes(x='0', fill = paste(genotype.1, genotype.2, sep="/")), color='black', width = 1, stat = "identity") +
geom_bar(aes(x='1', fill = genotype.1), width = 1, color='black', stat = "identity") +
geom_bar(aes(x='2', fill = genotype.2), width = 1, color='black', stat = "identity") +
coord_polar(theta="y") +
scale_x_discrete(name='', breaks=c('0', '1', '2'), labels=rep('', 3)) +
theme(axis.ticks.length = unit(0, "npc")) +
scale_fill_discrete(name='genotype', breaks = c('-', '+', '-/-', '-/+', '+/-', '+/+')) +
scale_y_continuous(breaks=0)
EDIT: Part of the reason, you get something different with faceting than without is because you use scales="free_y". To get the same thing without the facets, you can do scale the variables yourself.
p <- ggplot(df.mut, aes(x="")) +
geom_bar(data=subset(df.mut, variable=='HLA.A11.A24'),
aes(x='0', y=Avrg.muts.HLA.A11.A24/sum(Avrg.muts.HLA.A11.A24), fill=value), color='black', width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A11'),
aes(x='1', y=Avrg.muts.HLA.A11/sum(Avrg.muts.HLA.A11), fill=value), color='black', width = 1, stat = "identity") +
geom_bar(data=subset(df.mut, variable=='HLA.A24'),
aes(x='2', y=Avrg.muts.HLA.A24/sum(Avrg.muts.HLA.A24), fill=value), color='black', width = 1, stat = "identity") +
ggtitle("TEST1") +
theme(axis.text.x=element_blank(), legend.title=element_blank(), legend.position="right", legend.background=element_blank(), legend.box.just="left", plot.title=element_text(size=15, face="bold", colour="black", vjust=1.5)) +
scale_y_continuous(name="") +
scale_x_discrete(name="") +
coord_polar(theta="y")
# now look at the faceted and unfaceted plots...
p
p + facet_grid(variable~., scales="free_y")
However, your faceted plots also don't line up as nicely as your previous test data did. That just appears to be because the data is actually not exactly lined up (there are really only 2 unique values for the HLA.A11 and HLA.A24, so it's impossible to get 4 different sizes).

Resources