How do I display only selected items in a ggplot2 legend? [duplicate] - r

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)

Related

R remove names and order ggplot by frequency [duplicate]

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")

R, ggplot2: reverse alphabetical order [duplicate]

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))

R, ggplot bar, all bars same width? [duplicate]

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)

Barplot/barchart in R [duplicate]

This question already has answers here:
How to create grouped barplot with R [duplicate]
(1 answer)
plotting grouped bar charts in R
(3 answers)
Closed 3 years ago.
I have a data table with two small columns.
I want to do a pairwise comparison between the values. The first column is results of one test and the second of another test. So I want a barplot where the first pair of bars show value [1,1] and next to it [1,2] then [1,2] besides [2,2] and so on.
I have 20 values (10 in each from 10 instances) and want 20 bars in one plot. I have no category variable but I want to preserve the order in which they appear in the column (each result corrosponds to an instance). Hence 20 values represented by 20 bars.
Hope you can help.
Edit: Sry for the bad explanation.
Based on your vague question I think this is what you want. Here is a quick example you can use to get your results:
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
xlab="Number of Gears", col=c("darkblue","red"),
legend = rownames(counts), beside=TRUE)
Source: http://www.statmethods.net/graphs/bar.html

% in barplot() instead of count [duplicate]

This question already has answers here:
How can I change the Y-axis figures into percentages in a barplot?
(4 answers)
Closed 8 years ago.
I did a script to have a good barplot with barplot() on R
Is it possible to have on the y-axis the % and not the quantity (number of observations) ?
I would love to have the % without to change all of the script...
Thanks a lot
ggplot has this option, but for barplot() I believe you will have to make a new variable that represents the percent and then feed that into barplot()

Resources