Reputation: 87
In a ggplot graph I want to set limits to the y on the first axis but I have problem with the secondary axis vizualisation.
Here is what the code to generate the graph (inspired from this post):
mydata <- data.frame(group = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"),
count = c(140, 90, 25, 10, 10, 9, 8, 7, 7, 7, 6)) %>%
mutate(cumsum=cumsum(count)) %>%
mutate(cumperc=cumsum / sum(count) *100)
mydata$group <- factor(mydata$group, levels = mydata$group)
scaleRight <- tail(mydata$cumperc, n=1)/head(mydata$count, n=1)
ggplot(mydata, aes(x=group)) +
geom_bar(aes(y=count), fill='deepskyblue4', stat="identity") +
geom_path(aes(y=cumperc/scaleRight, group=1),colour="orange", size=0.9) +
geom_point(aes(y=cumperc/scaleRight, group=1),colour="orange") +
scale_y_continuous(expand = c(0, 0), sec.axis = sec_axis(~.*scaleRight, name = "Cumulative percent (%)",
breaks=seq(0,100,10))) +
coord_cartesian(ylim = c(0, 650)) +
theme_classic() +
theme(axis.text.x = element_text(angle=90, vjust=0.6),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold")) +
labs( y="Number of person", x="group")
My problem is that the second axis is squished.
Any suggestion ?
Another thing, I would like to add an horizontal line base on the secondary axis (at 90%).
I tried with geom_hline
but it take the first axis into account.
Here is the graph I want to obtain (done in excel)
Upvotes: 0
Views: 6830
Reputation: 3996
You just needed to adjust the scale adjustment in scaleRight
to use the full y axis extent (up to 700 in your example) instead of just the range of data for the bars.
I've added the variable ymax
to make it easy to change the range of data we want to extend our axis over.
The only other adjustment I made was to include expand = expansion(mult = c(0,.05))
to replace expand=c(0,0)
to add a 5% padding to the top of the axis as previously the top of the line was being cropped.
library(tidyverse)
mydata <- data.frame(group = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"),
count = c(140, 90, 25, 10, 10, 9, 8, 7, 7, 7, 6)) %>%
mutate(cumsum=cumsum(count)) %>%
mutate(cumperc=cumsum / sum(count) *100)
mydata$group <- factor(mydata$group, levels = mydata$group)
ymax <- 700
scaleRight <- max(mydata$cumperc)/ymax
ggplot(mydata, aes(x=group)) +
geom_bar(aes(y=count), fill='deepskyblue4', stat="identity") +
geom_path(aes(y=cumperc/scaleRight, group=1),colour="orange", size=0.9) +
geom_point(aes(y=cumperc/scaleRight, group=1),colour="orange") +
geom_hline(aes(yintercept=90/scaleRight), linetype='dashed')+
scale_y_continuous(expand = expansion(mult = c(0,.05)),
sec.axis = sec_axis(~.*scaleRight, name = "Cumulative percent (%)",
breaks=seq(0,100,10))) +
coord_cartesian(ylim = c(0, ymax)) +
theme_classic() +
theme(axis.text.x = element_text(angle=90, vjust=0.6),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold")) +
labs( y="Number of person", x="group")
Created on 2020-04-30 by the reprex package (v0.3.0)
Upvotes: 2