I have one income variable. I want to make a combination plot of a histogram and cumulative distribution in one plot with two y-axes. I got this code,
income<- bi_tr%>%
ggplot(aes(x=`12 Income`,na.rm = TRUE))+ #this fill comment goes to define legend
geom_histogram(binwidth=50)+ #setting default color for aes in histogram
theme_classic()+
geom_line(stat = "ecdf")+
geom_point(stat="ecdf",size=2)+
scale_y_continuous(sec.axis = sec_axis(trans = ~./max(bi_tr$`12 Income`),
name = "Cumulative distribution (%)"))+
labs(x="Income (USD/month)",y="Frequency")+
theme(text = element_text(size = 16, family = "serif"))+
xlim(0,500)
Then after I run income
it returns this plot
Personally, I have found some similar references with built-in function in R (without ggplot) for this case. But, somehow I want to stick with ggplot instead, hoping I could cope with the same syntax pattern for more cases afterwards. Then, I found trans=~./max(data)
line that works for ggplot. Then I tucked with this result.
Many thanks