For the first graph, just add position = ‘fill’ to your geom_bar line !. You don’t actually need to scale the counts as ggplot has a way to do it automatically.
ggplot(dat, aes(x = fruit)) + geom_bar(aes(fill = variable), position = 'fill')
More Related Contents:
- How can the facet function in ggplot be used to create a histogram in order to visualize distributions for all variables in a dataset?
- Add a common Legend for combined ggplots
- Stacked Bar Plot in R
- Controlling ggplot2 legend display order
- How can I remove empty factors from ggplot2 facets?
- How to add a ggplot2 subtitle with different size and colour?
- How to get a reversed, log10 scale in ggplot2?
- How can we make xkcd style graphs?
- Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found
- Plot data in descending order as appears in data frame [duplicate]
- geom_boxplot with precomputed values
- Reproducing lattice dendrogram graph with ggplot2
- Inserting a table under the legend in a ggplot2 histogram
- World map with ggmap
- Changing the line type in the ggplot legend
- How can I add a table to my ggplot2 output?
- Saving a graph with ggsave after using ggplot_build and ggplot_gtable
- ggplot replace count with percentage in geom_bar
- ggplot geom_bar where x = multiple columns
- Change colors in ggpairs now that params is deprecated
- Changing the order of dodged bars in ggplot2 barplot
- stat_contour not able to generate contour lines
- Add x and y axis to all facet_wrap
- How to change positions of x and y axis in ggplot2
- R Plot Filled Longitude-Latitude Grid Cells on Map
- How to get geom_vline to honor facet_wrap?
- When does the argument go inside or outside aes()?
- R ggplot ordering bars within groups
- ggplot for loop outputs all the same graph
- Make a rectangular legend, with rows and columns labeled, in grid