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.
Related
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
I have a stacked bar graph and ggplot has automatically generated a color legend that I want to remove. I have tried show.legend=FALSE, theme(legend.position="none"), and guides(colour=FALSE) and none of those solutions have removed the legend. I'll include the code below.
ggplot(unique_per_day, aes(fill=Entity.Name,y=prop, x=Entity.Type, width = org.count, label=Entity.Name), show.legend=FALSE) +
geom_bar(position="fill", stat="identity", colour= "black") +
facet_grid(~Entity.Type, scales="free_x", space="free_x" ) +
theme(legend.position="none", panel.spacing.x = unit(0, "npc")) +
guides(colour=FALSE) +
geom_text(size = 2.4, position = position_stack(vjust = 0.5)) +
theme_void()
It would be easier to troubleshoot if you provided an example dataset (e.g. dput(unique_per_day)), but my guess is that you need to remove the "fill" aesthetic, instead of the "color" aesthetic, e.g.
ggplot(unique_per_day, aes(fill=Entity.Name,y=prop, x=Entity.Type, width = org.count, label=Entity.Name)) +
geom_bar(position="fill", stat="identity", colour= "black", show.legend=FALSE) +
facet_grid(~Entity.Type, scales="free_x", space="free_x" ) +
guides(fill = FALSE) +
geom_text(size = 2.4, position = position_stack(vjust = 0.5)) +
theme_void() +
theme(legend.position="none", panel.spacing.x = unit(0, "npc"))
Edit
Here is an example using the "palmerpenguins" example dataset:
With the original code:
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
filter(island == "Dream") %>%
ggplot(aes(x = species, y = 1, fill = sex)) +
geom_bar(position = "fill", stat = "identity", colour = "black") +
facet_grid(~ island, scales = "free_x", space = "free_x") +
theme(legend.position = "none", panel.spacing.x = unit(0, "npc")) +
guides(colour = FALSE) +
theme_void()
With "theme_void" moved above "theme":
penguins %>%
na.omit() %>%
filter(island == "Dream") %>%
ggplot(aes(x = species, y = 1, fill = sex)) +
geom_bar(position = "fill", stat = "identity", colour = "black") +
facet_grid(~ island, scales = "free_x", space = "free_x") +
theme_void() +
theme(legend.position="none", panel.spacing.x = unit(0, "npc"))
I'm using a ggplot chart with geom_tile in RShiny. When I select a category with fewer y-axis elements, the tiles increase in height. I'd like to fix the tile height so that the chart height decreases when there are fewer y-axis elements, code below:
ggplot(selected_data(), aes(week, zone, fill= value, text=text)) +
scale_y_discrete(limits = unique(rev(selected_data()$zone))) +
geom_tile(aes(fill = value)) +
geom_text(aes(label = format(round_half_up(value,digits = 1), nsmall = 1)), size=4) +
labs(x="Week", y="") +
scale_x_discrete(position = "top") +
scale_fill_viridis(option = "viridis") +
theme_grey(base_size = 16) + labs(fill = "Rate per\n1,000 population") +
theme(legend.position = "top")
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").
Legends in two ggplot graphs in grid.arrange() are overlapping. I have used legend.positon = "top" or "bottom", another problem arises i.e the ggplot is not showing complete legends. Actually, my legends are quite long. How can get legends printed in two lines to avoid above-mentioned problem?
I have tried legend.position = "top", also legend.box = "vertical" but nothing worked
k1 %>%
ggplot(aes(Alert,Sum, fill = Alert)) +
geom_bar(stat = "identity") +
facet_wrap(~ Model , nrow= 5) +
coord_flip() +
geom_text(aes(label = Sum), fontface = "bold") +
theme(legend.position = "none")+
ggtitle("MODEL WISE ALERT COUNT")+
theme_grey(base_size = 22)+
theme(legend.position = "top")
I have found the answer.
k1 %>% ggplot(aes(Alert,Sum, fill = Alert)) + geom_bar(stat = "identity") + facet_wrap(~ Model , nrow= 5) + coord_flip() + geom_text(aes(label = Sum), fontface = "bold") + ggtitle("MODEL WISE ALERT COUNT")+ theme_grey(base_size = 22)+theme(legend.position = "bottom") **+ guides(fill=guide_legend(nrow=2,byrow=TRUE))