I have a line chart with metrics for different years and months. I want to add a median line for each month.
library(dplyr)
library(lubridate)
library(ggplot2)
set.seed(52)
data <- tibble(
date = seq.Date(from = as.Date("2017-01-01"), to = date("2020-12-31"), by = "month")
) %>%
mutate(
metric = rnorm(n(), mean = 5, sd = 2),
month = month(date, label = TRUE),
year = as.factor(year(date))
)
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line()
I tried using geom_hline() to do this but it only produces a line for the total median.
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
geom_hline(aes(yintercept = median(metric), group = month))
Created on 2021-11-18 by the reprex package (v2.0.1)
What is the easiest way to create a median line for each month? Like this:
Is this what you are looking for...?
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
geom_tile(data = function(df) df %>% group_by(month) %>%
summarise(metric = median(metric)),
aes(x = month, y = metric), height = 0.05, inherit.aes = FALSE)
Alternatively, thanks to #user20650's comment below, this also works...
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
stat_summary(aes(x = month, y = metric),
fun = median,
geom = "tile",
height = 0.05,
inherit.aes = FALSE)
You could use ave.
I didn't get it if you need a median line for each year or through the months, so I will show you both.
Median for each year
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
geom_line(aes(y = ave(metric, year, FUN = median)))
Median through months
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
geom_line(aes(y = ave(metric, month, FUN = median), colour = "median"), size = 2)
EDIT:
since you are interested in a horizontal line, here you have an alternative solution:
ggplot(data, aes(x = month, y = metric, color = year, group = year)) +
geom_line() +
stat_summary(aes(x = month, y=metric), fun = median, geom = "point", shape = "-", size = 15, inherit.aes = FALSE)
Not sure if this is what you're looking for, but you can simply create a new variable in your dataset with a median metric. Your first group observations by month, then calculate a median metric and then ungroup the data.
data <- data %>% group_by(month) %>% mutate(median_metric=median(metric)) %>% ungroup()
Once you do it, you just specify yintercept to vary by median_metric
ggplot(data ) +
geom_line(aes(x = month, y = metric, color = year, group = year))+
geom_hline(aes(yintercept = median_metric))
Related
I want to shade the area between the grouped lines. I've tried different approaches but I don't manage to do it.
df <- data.frame(year = rep(c(1950:1955), each = 2),
gender = factor(rep(c("male", "female"), times = 6)),
value = c(40526812, 37450509, 43027405,
40135682, 45801088, 43130369,
48579427, 46077640, 50948574,
48493786, 53052094, 50537984))
df |>
ggplot(aes(year, value, group = gender)) +
geom_line()
Thanks in advance!
You could define the boundaries of the area by using a pivot_wider to create the ymax and ymin of the area in geom_ribbon so you don't need to subset per argument. So it will be easier to reproduce. Here an example:
df <- data.frame(year = rep(c(1950:1955), each = 2),
gender = factor(rep(c("male", "female"), times = 6)),
value = c(40526812, 37450509, 43027405,
40135682, 45801088, 43130369,
48579427, 46077640, 50948574,
48493786, 53052094, 50537984))
library(ggplot2)
library(dplyr)
library(tidyr)
area <- df %>%
pivot_wider(names_from = gender, values_from = value) %>%
mutate(
ymax = pmax(male, female),
ymin = pmin(male, female)
)
ggplot(data = df, mapping = aes(year, y = value, group = gender)) +
geom_line() +
geom_ribbon(data = area, mapping = aes(year, ymin = ymin, ymax = ymax), alpha = 0.4, inherit.aes = FALSE )
Created on 2022-07-22 by the reprex package (v2.0.1)
You can use geom_ribon which allows to shade areas within axis values.
A stronger alpha will play on the shader intensity.
ggplot(data=df, aes(year, value, group = gender)) +
geom_line() +
geom_ribbon(data=subset(df, 1950 <= year & year <= 1955),
aes(ymin=rep(subset(df, gender=='female')$value, each=2),
ymax=rep(subset(df, gender=='male')$value, each=2)), fill="blue", alpha=0.5)
I am using ggplot to visualize the gapminder data set. Can someone help me get the legend to show round points with their sizes respective to the population of the country?
df1<-gapminder[!(gapminder$country=="Kuwait"),]
blackline <- df1 %>%
group_by(continent, year) %>%
summarise(average = weighted.mean(gdpPercap))
p <- ggplot(data = df1, mapping = aes(x = year, y = gdpPercap)) +
scale_x_continuous(breaks = seq(1960, 2000, by = 20)) +
theme_bw() +
labs(x = "Life Expectancy",
y = "GDP Per Capita",
color = "Continent",
size = "Population (100K)") +
# colored dots
geom_point(df1, mapping = aes(col = continent, size = pop/100000)) +
# colored lines
geom_line(data = df1, aes(color = continent, group = country)) +
facet_grid(cols = vars(continent)) +
# weighted average black line
geom_line(data = blackline, aes(x = year, y = average, size = 1)) +
geom_point(data = blackline, aes(x = year, y = average, size = 1000))
p
This could be achieved by setting show.legend=FALSE in the geoms for the blackline:
library(gapminder)
library(ggplot2)
library(dplyr)
df1<-gapminder[!(gapminder$country=="Kuwait"),]
blackline <- df1 %>%
group_by(continent, year) %>%
summarise(average = weighted.mean(gdpPercap))
#> `summarise()` regrouping output by 'continent' (override with `.groups` argument)
p <- ggplot(data = df1, mapping = aes(x = year, y = gdpPercap)) +
scale_x_continuous(breaks = seq(1960, 2000, by = 20)) +
theme_bw() +
labs(x = "Life Expectancy",
y = "GDP Per Capita",
color = "Continent",
size = "Population (100K)") +
# colored dots
geom_point(aes(col = continent, size = pop/100000)) +
# colored lines
geom_line(aes(color = continent, group = country)) +
facet_grid(cols = vars(continent)) +
# weighted average black line
geom_line(data = blackline, aes(x = year, y = average, size = 1), show.legend = FALSE) +
geom_point(data = blackline, aes(x = year, y = average, size = 1000), show.legend = FALSE)
p
I would like to draw a line (or making points) on top of my stacked bar_plots. As I have no real data points I can refer to (only the spereated values and not the sum of them) I don't know how I can add such line. The Code produce this plot:
I want to add this black line(my real data are not linear):
library(tidyverse)
##Create some fake data
data3 <- tibble(
year = 1991:2020,
One = c(31:60),
Two = c(21:50),
Three = c(11:40)
)
##Gather the variables to create a long dataset
new_data3 <- data3 %>%
gather(model, value, -year)
##plot the data
ggplot(new_data3, aes(x = year, y = value, fill=model)) +
geom_bar(stat = "identity",position = "stack")
You can use stat_summary and sum for the summary function:
ggplot(new_data3, aes(year, value)) +
geom_col(aes(fill = model)) +
stat_summary(geom = "line", fun.y = sum, group = 1, size = 2)
Result:
You could get sum by year and plot it with new geom_line
library(dplyr)
library(ggplot2)
newdata4 <- new_data3 %>%
group_by(year) %>%
summarise(total = sum(value))
ggplot(new_data3, aes(x = year, y = value, fill=model)) +
geom_bar(stat = "identity",position = "stack") +
geom_line(aes(year, total, fill = ""), data = newdata4, size = 2)
I'm trying to plot a basic bar chart per group.
As values are pretty big, I want to show for each bar (i.e. group) the % of each group within the bar.
I managed to show percentage of the total, but this is not what I'm expecting : in each bar, I would like that the sum of % equal 100%.
Is there an easy way to do it without changing the dataframe ?
(DF <- data.frame( year = rep(2015:2017, each = 4),
Grp = c("Grp1", "Grp2", "Grp3", "Grp4"),
Value = trunc(rnorm(12, 2000000, 100000))) )
ggplot(DF) +
geom_bar(aes(x = year, y = Value, fill = Grp),
stat = "identity",
position = position_stack()) +
geom_text(aes(x = year, y = Value, group = Grp,
label = percent(Value/sum(Value))) ,
position = position_stack(vjust = .5))
You can create a new variable for percentile by year:
library(dplyr)
library(ggplot2)
library(scales)
DF <- DF %>% group_by(year) %>% mutate(ValuePer=(Value/sum(Value))) %>% ungroup()
ggplot(DF, aes(year, ValuePer, fill = Grp)) +
geom_bar(stat = "identity", position = "fill") +
geom_text(aes(label = percent(ValuePer)),
position = position_fill())+
scale_y_continuous(labels = percent_format())
Use position = "fill" to turn scale into proportions and scale_y_continuous(labels = percent_format()) to turn this scale into percent.
DF <- data.frame( year = rep(2015:2017, each = 4),
Grp = c("Grp1", "Grp2", "Grp3", "Grp4"),
Value = trunc(rnorm(12, 2000000, 100000)))
library(ggplot2)
library(scales)
ggplot(DF, aes(year, Value, fill = Grp)) +
geom_bar(stat = "identity", position = "fill") +
geom_text(aes(label = percent(Value / sum(Value))),
position = position_fill()) +
scale_y_continuous(labels = percent_format())
OK gathering all your tricks, I finally get this :
I need to adjust my DF, what I wanted to avoid, but it remains simple so it works
library(dplyr)
library(ggplot2)
library(scales)
DF <- DF %>% group_by(year) %>% mutate(ValuePer=(Value/sum(Value))) %>% ungroup()
ggplot(DF, aes(year, Value, fill = Grp)) +
geom_bar(stat = "identity", position = "stack") +
geom_text(aes(label = percent(ValuePer)),
position = position_stack()) +
scale_y_continuous(labels = unit_format("M", 1e-6) )
I would use a single geom_text for each bar while filtering data by year (bar) using dplyr. Check if is that what you need:
(DF <- data.frame( year = rep(2015:2017, each = 4),
Grp = c("Grp1", "Grp2", "Grp3", "Grp4"),
Value = trunc(rnorm(12, 2000000, 100000))) )
library(dplyr)
ggplot(DF) +
geom_bar(aes(x = year, y = Value, fill = Grp),
stat = "identity",
position = position_stack()) +
geom_text(data = DF %>% filter(year == 2015),
aes(x = year, y = Value,
label = scales::percent(Value/sum(Value))) ,
position = position_stack(vjust = .5)) +
geom_text(data = DF %>% filter(year == 2016),
aes(x = year, y = Value,
label = scales::percent(Value/sum(Value))) ,
position = position_stack(vjust = .5)) +
geom_text(data = DF %>% filter(year == 2017),
aes(x = year, y = Value,
label = scales::percent(Value/sum(Value))) ,
position = position_stack(vjust = .5))
Argument group is not necessary here. There may be more elegant solutions but that is the one I could think about. Tell me if this is the output you were waiting for:
Maybe creating a new column doing the right computation. I could not figure out how the computation could be done right inside aes(), the way you did you just computed the overall %, the Value should be grouped by year instead.
At least you got yourself the actually value by the Y axis and the Year grouped % inside bars. I would advise changing this labels by stacking something like this:
scale_y_continuous(breaks = seq(0,8*10^6,10^6),
labels = c(0, paste(seq(1,8,1),'M')))
Resulting this:
You can adapt to your context.
How do I draw the sum value of each class (in my case: a=450, b=150, c=290, d=90) above the stacked bar in ggplot2? Here is my code:
#Data
hp=read.csv(textConnection(
"class,year,amount
a,99,100
a,100,200
a,101,150
b,100,50
b,101,100
c,102,70
c,102,80
c,103,90
c,104,50
d,102,90"))
hp$year=as.factor(hp$year)
#Plotting
p=ggplot(data=hp)
p+geom_bar(binwidth=0.5,stat="identity")+
aes(x=reorder(class,-value,sum),y=value,label=value,fill=year)+
theme()
You can do this by creating a dataset of per-class totals (this can be done multiple ways but I prefer dplyr):
library(dplyr)
totals <- hp %>%
group_by(class) %>%
summarize(total = sum(value))
Then adding a geom_text layer to your plot, using totals as the dataset:
p + geom_bar(binwidth = 0.5, stat="identity") +
aes(x = reorder(class, -value, sum), y = value, label = value, fill = year) +
theme() +
geom_text(aes(class, total, label = total, fill = NULL), data = totals)
You can make the text higher or lower than the top of the bars using the vjust argument, or just by adding some value to total:
p + geom_bar(binwidth = 0.5, stat = "identity") +
aes(x = reorder(class, -value, sum), y = value, label = value, fill = year) +
theme() +
geom_text(aes(class, total + 20, label = total, fill = NULL), data = totals)
You can use the built-in summary functionality of ggplot2 directly:
ggplot(hp, aes(reorder(class, -amount, sum), amount, fill = year)) +
geom_col() +
geom_text(
aes(label = after_stat(y), group = class),
stat = 'summary', fun = sum, vjust = -1
)