Insert percentage of values in ggplot - r

I'm quite new in R, but I'm trying to do a facet_grid using ggplot package in R and, for better data visualization, I'd like to insert the percentage of values in each quadrant for the column groupings, like the image below:
Can be one or another, in each quadrant or in legend.
My code is this one below
ggplot(df1_final,aes(x=revenue,y=visits,col=groupings)) +
geom_jitter(alpha=I(1/2)) +
xlim(c(0,20000)) +
facet_grid(group_lvl_1_visits ~ group_lvl_1_revenue)
Could anyone help me on this?
EDIT: Both solutions helped me a lot, very good ones.

Here's one way to do it:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class, nrow = 4) ->
p
p + geom_text(
data = setNames(as.data.frame(prop.table(table(mpg$class))),c("class", "lab")),
mapping = aes(label = scales::percent(lab)),
x = 4,
y = 40
)
or, for facet_grid:
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(drv ~ cyl)
p + geom_text(
data = setNames(as.data.frame(prop.table(table(mpg$drv, mpg$cyl)), stringsAsFactors = F), c("drv","cyl","lab")),
mapping = aes(label = scales::percent(lab)),
x=4.5,
y=30
)

Here's a tidyverse + label-in-legend solution:
library(ggplot2)
library(dplyr)
library(scales)
group_by(mpg, cyl, drv) %>%
mutate(color=sprintf("%s-%s",cyl,drv)) %>%
ungroup(mpg) -> mpg
gg <- ggplot(mpg, aes(displ, cty))
gg <- gg + geom_point(aes(color=color))
gg <- gg + facet_grid(drv ~ cyl)
count(mpg, color) %>%
ungroup() %>%
mutate(pct=percent(n/sum(n)),
lab=sprintf("%s (%s)", color, pct)) -> pct_df
gg <- gg + scale_color_discrete(name="Title", labels=pct_df$lab)
gg

Related

How to incorporate legend as plot of plot size when arranging multiple plots using patchwork?

Dummy code:
library(ggplot2)
library(patchwork)
plot1 <- mpg %>% ggplot()
plot2 <- mpg %>% ggplot()
plot3 <- mpg %>% ggplot(aes(cyl, displ)) +
geom_point(aes(colour = manufacturer)) +
guides(colour=guide_legend(ncol=4))
(plot1 + plot2) / plot3
I'm not looking for a combined legend, but something which looks like:
Where the legend is 'considered' part of the plot. I've tried adding in margins with theme() and plot_spacer() but it's not exactly what I want. This is what I get:
For your example code one option would be to make use of guide_area() like so:
library(ggplot2)
library(patchwork)
library(magrittr)
plot1 <- mpg %>% ggplot()
plot2 <- mpg %>% ggplot()
plot3 <- mpg %>% ggplot(aes(cyl, displ)) +
geom_point(aes(colour = manufacturer)) +
guides(colour = guide_legend(ncol=3))
plot1 + plot2 + plot3 + guide_area() +
plot_layout(guides = 'collect')
Another option would be to extract the guide via cowplot::get_legend and add it to the patchwork like so:
(plot1 + plot2) / (plot3 + guides(color = "none") + cowplot::get_legend(plot3))
Created on 2021-09-22 by the reprex package (v2.0.1)

ggplot labels with "K" for thousands or "M" for millions (maintaining 'comma' y-axis labeling)

library(tidyverse)
df <- mpg %>% head() %>% mutate(hwy = hwy * 10000)
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = scales::comma) +
geom_text(aes(label = hwy), hjust = -0.25)
I want the labels on this plot to use "K" for thousands (eg 260K instead of 260000). BUT - I want to maintain the y-axis as is, with commas (eg 260,000). How can I achieve this?
You can use scales::label_number_si():
library(scales)
library(ggplot2)
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = comma) +
geom_text(aes(label = label_number_si()(hwy)), hjust = -0.25)
You can just add a custom column called myLabel to your dataframe which holds your desired labels. In the package scales you can find a function that does the converting part for you:
df <- mpg %>% head() %>% mutate(hwy = hwy * 10000)
df$myLabels <- scales::label_number_si()(df$hwy)
Now, use the new column myLabels as the aesthetic for creating the labels
ggplot(df, aes(cty, hwy)) +
geom_point() +
scale_y_continuous(label = scales::comma) +
geom_text(aes(label = myLabels), hjust = -0.25)

Adding smoother to ggplot 2

I have used the following code to create a plot in r using ggplot2:
g <- ggplot(newdata, aes(MVPAper, FMI) +
geom_smooth(method = 'lm'))
I then added the following:
p <- g + geom_point(aes(color = Age)) +
facet_grid(Age ~ .) +
stat_smooth(method = 'lm') +
theme_bw(base_family = 'Times')`
I am wanting to have a smoother for each of the four graphs I have created, using the facet grid to split the graph into four ages 8,9,12,and 15) can anyone assist with my code?
You don't need both geom_smooth() and stat_smooth(). Try this:
library(tidyverse)
df <- diamonds %>% filter(price < 10000, carat < 2.5)
g <- ggplot(df, aes(carat, price, color = cut))
g +
geom_point() +
geom_smooth(method = 'lm') +
facet_grid(cut ~ .) +
theme_bw()

Combined bar plot and points in ggplot2

I would like to plot a "combined" bar plot with points.
Consider to following dummy data:
library(ggplot2)
library(gridExtra)
library(dplyr)
se <- function(x){sd(x)/sqrt(length(x))}
p1 <- ggplot(mtcars, aes(y=disp, x=cyl, fill=cyl))
p1 <- p1 + geom_point() + theme_classic() + ylim(c(0,500))
my_dat <- summarise(group_by(mtcars, cyl), my_mean=mean(disp),my_se=se(disp))
p2 <- ggplot(my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se))
p2 <- p2 + geom_bar(stat="identity",width=0.75) + geom_errorbar(stat="identity",width=0.75) + theme_classic() + ylim(c(0,500))
The final plot should look like that:
You can add layers together, but if they have different data and/or aesthetics you'll want to include the data and aes arguments in each graphical layer.
p3 <- ggplot() +
geom_bar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), stat="identity", width = 0.75) +
geom_errorbar(data=my_dat, aes(y=my_mean,x=cyl,ymin=my_mean-my_se,ymax=my_mean+my_se), width = 0.75) +
geom_point(data=mtcars, aes(y=disp, x=cyl, fill=cyl)) +
ylim(c(0,500)) +
theme_classic()
If you want to make it so that the the points are off to the side of the bars, you could subtract an offset from the cyl values to move over the points. Like #LukeA mentioned, by changing the geom_point to geom_point(data=mtcars, aes(y=disp, x=cyl-.5, fill=cyl)).
You can specify each layer individually to ggplot2. Often you are using the same data frame and options for each geom, so it makes sense to set defaults in ggplot(). In your case you should specify each geom separately:
library(ggplot2)
library(gridExtra)
library(dplyr)
se <- function(x){sd(x)/sqrt(length(x))}
my_dat <- summarise(group_by(mtcars, cyl),
my_mean = mean(disp),
my_se = se(disp))
p1 <- ggplot() +
geom_bar(data = my_dat,
aes(y = my_mean, x = cyl,
ymin = my_mean - my_se,
ymax = my_mean + my_se), stat="identity", width=0.75) +
geom_errorbar(data = my_dat,
aes(y = my_mean, x = cyl,
ymin = my_mean - my_se,
ymax = my_mean + my_se), stat="identity", width=0.75) +
geom_point(data = mtcars, aes(y = disp, x = cyl, fill = cyl)) +
theme_classic() + ylim(c(0,500))
p1

Plot mean in an R plot

I would like to add a mean of valuus to windows in a scatter plot I have. I created the scatter plot with ggplot2
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
This will give the scatter plot but I woudl like to add add the mean of a window (say size equals 1) and plot this points of the mean as a line. Additionally I woudl like to have vertical bars at each point to indicate the variance.
Mtcars is the data set standard available in ggplot 2
This uses the new dplyr library.
library(dplyr)
forLines <- mtcars %.%
group_by(cut(wt, breaks = 6)) %.%
summarise(mean_mpg = mean(mpg), mean_wt = mean(wt))
p +
geom_point(size=5) +
geom_boxplot(aes(group = cut(wt, breaks = 6))) +
geom_line(data=forLines,aes(x=mean_wt,y=mean_mpg))
Maybe this is what you're looking for:
library(ggplot2)
s <- seq(0, ceiling(max(mtcars$wt)), 1)
ind <- as.integer(cut(mtcars$wt, s))
myfun <- function(i)
c(y = mean(i), ymin = mean(i) - var(i), ymax = mean(i) + var(i))
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
stat_summary(fun.data = myfun, aes(group = ind, x = ind - .5),
colour = "red") +
stat_summary(fun.y = mean, aes(x = ind - .5), geom = "line",
colour = "red")
Is this what you want?
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point() + geom_smooth(aes(wt, mpg, group=1), method = "lm")

Resources