This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 6 years ago.
I use ggplot2 to create a graph using
dat <- data.frame(xx=c("IND","AUS","USA"), yy=c(1,5,2))
ggplot(data=dat, aes(x=reorder(xx,xx), y=yy))
and this nicely sorts my x-axis alphabetically. However, I want to sort the string variable xx in reverse alphabetical order but cannot seem to get it. While reorder(yy,-yy) can sort my numeric variable, reorder(xx,-xx) does not work.
How about:
ggplot(data=dat, aes(x=forcats::fct_rev(reorder(xx,xx)), y=yy))
Related
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Reorder bars in geom_bar ggplot2 by value
(3 answers)
ggplot2 geom_bar - how to keep order of data.frame [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to do a barplot where entries of the same factor are next to eachother so I can compare (in this example I want to compare the tissue type).
I sort the dataframe but for some reason ggplot does not follow the sorting.
tp <- ms %>%
arrange(tissue) %>%
mutate(tissue=as.factor(tissue))
ggplot(tp) +
geom_bar(aes(x=Sample, y=!!sym(cv), fill=tissue), stat="identity") +
ylab("")
head(tp$tissue)
and I check and i know that tp is sorted
head(tp$tissue) outputs
[1] adjacent adjacent adjacent adjacent adjacent adjacent
Levels: adjacent normal tumor
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Remove all of x axis labels in ggplot [duplicate]
(1 answer)
Closed 2 years ago.
I have a dataframe DiatomFiltered containing the column species. I want to have a ggplot (ggplot2) of all species and their frequency ordered from highest to lowest. The below code works, but the names are a mess (because of way too many species names) so I want to remove that and i want the frequencies ordered. How do i do this?
ggplot(DiatomFiltered, aes(species)) +
geom_bar(fill = "#0073C2FF")
This question already has answers here:
How to reorder a legend in ggplot2?
(2 answers)
Reorder levels of a factor without changing order of values
(9 answers)
Closed 5 years ago.
I am making CDF plots using this line of code:
library(ggplot2)
ExpDF <- data.frame(x=c('gene1','gene2','gene3','gene4'),"FC"=c(1,2,3,4))
ts_miR_15 <- data.frame(x=c('gene1','gene3','gene4'),"FC"=c(1,3,4))
ahc_miR_15_3UTR <- data.frame(x=c('gene1','gene4','gene12'),"FC"=c(1,4,12))
g <- ggplot(data = NULL)
g + geom_step(aes(x=ExpDF$FC, color="All_genes"),stat="ecdf") +
geom_step(aes(x= ts_miR_15$FC, color="Targetscan_miR-15/16"), stat="ecdf") +
geom_step(aes(x= ahc_miR_15_3UTR$FC, color="3UTR_miR-15/16_seed"), stat="ecdf") +
scale_colour_manual("Subsets", values = c("All_genes"='black',
"Targetscan_miR-15/16"='orange',
"3UTR_miR-15/16_seed"='red'))
...(etc. for each additional data.frame
I would like to be able to reorder the labels for the legend such that "All_genes" is on top (and preferably have it in any oder I want). I would prefer to keep the data.frames separate because they have different numbers of genes (which mark the rows) in them and some of them are subsets of the other.
This question already has answers here:
Don't drop zero count: dodged barplot
(6 answers)
Closed 6 years ago.
I'm trying to find a ggplot specific work around so that I can generate bar plots in which all the bars are the same width. I know that this is because I am "missing values" and because the bar width fills in side-to-side over a blank. BUT I'm working with very large data sets and using reshape to make the data wide and then inserting place holder values to eliminate blanks is not something I want to do.
Test data:
df<-data.frame(tax=c("type1","type1","type1","type1","type2","type2"),Gene=c("a","b","c","c","a","b"),logFC=c(-2,-4,2,1,3,-1))
ggplot code, which gives me an extra wide bar for "c"
bar<-ggplot(df, aes(x=Gene, order=Gene,y=logFC,fill=tax))+ geom_bar(stat="identity",position="dodge")
Any suggestions that don't require me to change any values in the input df?
**This question is not a duplicate. I am looking for an ALTERNATIVE solution to what has been given before. Previous solutions DO NOT WORK. I cannot simply dcast (with fill=0) and re-melt my data frame (trust me, I've been trying this for weeks).
I am looking for a ggplot specific answer.
I think it will remain as a wide bar because c has only type1 twice and it doesn't have type 2
If you use facet_wrap, it will remain the same width
ggplot(df, aes(x=Gene, y=logFC, color = tax))+
geom_bar(stat = "identity", position="dodge", width=.5) +
facet_wrap(~tax)
This question already has an answer here:
Remove legend entries for some factors levels
(1 answer)
Closed 7 years ago.
I'm creating a stacked bar plot of relative abundance data, but I only want to display the ten most abundant organisms in the legend. How do I do this? I have no idea where to begin and haven't found any answers online.
Here's the plot with a full legend:
Thanks.
As pointed out by user20650 in the comments, the answer is to add a list of selected items to the breaks= argument in scale_fill_manual()
scale_fill_manual(breaks=list,values=colpal)