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.