Annotate ggplot bar plot in R - r

How can I annotate the following plot? I want to include counts on top of each bar.
g <- ggplot(mpg, aes(class))
# Number of cars in each class:
g + geom_bar()
I only know how to do it if I do a group by and create a new column 'count' for example.

You can do:
ggplot(mpg, aes(class)) +
geom_bar() +
geom_text(stat = "count", aes(label = after_stat(count)), nudge_y = 1)

Related

Add an extra point to a single facet in ggplot

I want to add a single point to only one facet. I tried to set the group in geom_point:
ggplot(data=mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class) + geom_point(aes(x=5, y=25, group = "pickup"), colour="blue")
But that put a blue dot in all facets. Is there any way to constrain the point to only one?
You could achieve your desired result by putting the information for your point in a dataframe which you pass to the data argument of your second geom_point:
library(ggplot2)
ggplot(data=mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class) +
geom_point(data = data.frame(displ = 5, hwy = 25, class = "pickup"), colour="blue")

How to minimize the white space created by the guide_area() function of the patchwork package in plots made with ggplot2?

I made 3 plots with the ggplot2 package. To arrange the plots in a single figure I used the patchwork package. In the arrangement, I put 2 plots at the top, the common legend below these plots and below the common legend the third plot. I created the common legend space with the guide_area() function, but a big unused blank area is created along with it.
How can I keep this unused blank space to a minimum?
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = mpg,
aes(x = fl,
y = displ)) +
geom_col(aes(fill = cty))
p2 <- ggplot(data = mpg,
aes(x = year,
y = hwy)) +
geom_point(aes(color = drv))
p3 <- ggplot(data = mpg,
aes(x = class,
y = displ)) +
geom_col() +
facet_grid(~year)
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
White space remains in different sizes and proportions of the figure (the white space is marked with red).
Use heights = ... inside plot_layout.
For example,
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect", heights = c(3,1,3)) &
theme(legend.position = "bottom")

Increase Plot layout and reduce the legend list

Below is my code to plot Stacked BarPlot
ggplot(data = mdata, aes(x = variable, y = value, fill = Species)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.text=element_text(size=rel(0.7)),
legend.key.size = unit(0.5, "cm")) +
scale_y_continuous(labels=function(x)x*100) +
coord_flip() +
ylab("Species Percentage") +
xlab("Samples")
OutputPlot:
As you can see from the plot my Species legends are split in to 5 column list, which takes the 50% of the total plot layout.
Is there a way to make/convert legend list in to only 2 or 3 column so that area above and below will be covered and BarPlot can be widened.
Also to make Legend Text Bold its looking blurred with many legends
You can set any number of columns with the ncol argument in guide_legend():
library(ggplot2)
dat <- cbind(car = rownames(mtcars), mtcars)
ggplot(dat, aes(mpg, wt, colour = car)) +
geom_point() +
scale_colour_discrete(guide = guide_legend(ncol = 3))
EDIT: As Z.Lin pointed out, for fill scales; replace scale_colour_* by scale_fill_*.

ggplot Adding Tracking Colors Below X-Axis

I'd like to add a line below the x-axis where its color is dependant on a factor that is not plotted.
In this example, I'm creating a box plot and would like to add a line that indicates another variable.
Using the cars data set as an example and then physically dawing in what I'm trying to do:
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot()
My thought was to create a bar, column, or geom_tile plot and then arrange it below the boxplot. This is how I would do it in base R. Is there a way to add in these kinds of color labels in ggplot2?
The natural way in ggplot2 to do this sort of thing would to be facet on the categorical variable to create subplots. However if you want to keep everything on the same graph you could try using a geom_tile() layer something like this:
df <-data.frame(x = factor(c(4,6,8)), colour = factor(c(1,2,1)))
ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 8, fill = colour))
Alternatively as you suggest you could align an additional plot underneath it. You could use ggarrange() in the ggpubr package for this:
plot1 <- ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) +
geom_boxplot() +
geom_tile(data=df, aes(x = x, y = 10, fill = colour))
theme(legend.position = 'none')
plot2 <- ggplot(df, aes(x=x, y=1, fill = colour)) +
geom_tile() +
theme_void() +
scale_fill_manual(values=c('orange', 'green', 'orange')) +
theme(legend.position = 'none')
library(ggpubr)
ggarrange(plot1, plot2, nrow = 2, heights = c(10, 1), align = 'h')

I want to change the color of ggplot bar charts

I want to change the color of ggplot bar charts manually using the scale_color_manual function. Here is the code:
library(ggplot2)
ggplot(UM.Leads, aes(Leads, Count, fill = Model)) +
geom_bar(stat = "identity") +
xlab("Electrode Model") +
ylab("DBS Leads") +
ggtitle("University of Minnesota") +
scale_color_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()
I cannot seem to change the fill of the bar graphs from the default pink, green, and blue that ggplot provides. Any help would be much appreciated!
See plot here: http://rpubs.com/Gopher16/393415
To illustrate the comment from #Jack Brookes and create a reproducible example:
library(ggplot2)
df <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30)
)
ggplot(df, aes(gp, y, fill=gp)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()

Resources