Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

add a smoothed line to barplot with ggplot

I was wondering whether it is possible to add a geom_smooth (or any trend-line) to a geom_col barplot in ggplot2. Here is the code I’m using:

library(dplyr)
library(readxl)
library(ggplot2)
library(RColorBrewer)

mapQ_prop <- read_excel("/path/to.file.xlsm", 16)

mapQ_prop <- mapQ_prop %>% arrange(value)
mapQ_prop$sample <- as.vector(mapQ_prop$sample)
mapQ_prop$sample = factor(mapQ_prop$sample,mapQ_prop$sample)

plot_mapQ <- 
  ggplot(data=mapQ_prop, aes(x=sample, y=value, fill=brewer.pal(9, "Blues")[5])) + geom_col(color="black", alpha=.75) + theme_bw() + 
  
  theme(legend.title=element_text(face='italic'), legend.position='bottom', legend.direction='horizontal') +
  
  scale_fill_manual(values=brewer.pal(9, "Blues")[5], labels="value") +
  
  guides(
    fill=guide_legend(title="proportion over Q20", title.position="top", title.hjust=.5)
  ) +
  
  xlab("")

plot_mapQ + theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1, size=6)) +
  scale_y_continuous(expand=c(0,0), limits=c(0,1))

and a dput of the data to recreate the dataset:

structure(list(sample = structure(1:279, levels = c("HGDP00749", 
"HGDP01172", "HGDP00936", "HGDP00533", "TZ-11", "AV-21", "HGDP01286", 
"HGDP01036", "HGDP00982", "mixe0007", "HGDP01076", "IHW9118", 
"HGDP00546", "HGDP01308", "IHW9193", "HGDP00775", "HGDP01015", 
"HGDP01153", "HGDP00783", "HGDP00125", "Peru60", "HGDP01344", 
"HGDP00124", "Jordan445", "TGBS21", "Bu16", "NA17385", "HGDP00887", 
"HGDP01320", "I3", "Y4", "HG02494", "NA00726", "HGDP01163", "HGDP01228", 
"HGDP00543", "HGDP00555", "IraqiJew4291", "YemeniteJew5433", 
"NA17386", "HGDP01198", "HGDP01032", "HGDP01179", "CHI-034", 
"HGDP01098", "HGDP01335", "HGDP00541", "HGDP00956", "HGDP01250", 
"M13", "HGDP01044", "HGDP01306", "HGDP01191", "tdj409_shugnan", 
"ND15865", "KD4", "SAH41", "CHI-007", "DNK07", "HGDP00550", "K4", 
"NA15728", "Bu5", "HGDP01333", "HGDP00987", "NA13607", "HGDP00737", 
"M4", "B11", "Sir19", "NA13604", "NA15763", "Nesk_22", "NorthOssetia5", 
"HGDP01188", "IraqiJew1771", "Bishkek28440", "HGDP01350", "HGDP00058", 
"Y8", "HGDP00545", "ML2", "NA15761", "HGDP00090", "K1", "I1", 
"HG00360", "NA11200", "HGDP01355", "Igor21", "R6", "HGDP01168", 
"Dus22", "HGDP00660", "Dus16", "ML3", "Esk29", "HGDP01079", "HGDP00476", 
"ND19394", "HGDP01297", "HGDP00706", "Sir40", "YemeniteJew4695", 
"R3", "HGDP00852", "NA13616", "HGDP00725", "HG00174", "HGDP01223", 
"HGDP00449", "HGDP01401", "HGDP01246", "Nlk3", "SAH31", "altai363p", 
"HGDP00526", "B17", "HGDP00547", "HGDP01211", "HGDP00195", "SA0722", 
"NA17374", "HGDP01240", "Ale14", "tdj430_shugnan", "HG00190", 
"NA21490", "Nlk1", "HGDP00569", "HG01503", "HGDP00216", "HGDP01095", 
"HGDP00702", "HGDP00857", "mixa0105", "Ale32", "HGDP00846", "HGDP00785", 
"HGDP01315", "HGDP00540", "HG03007", "HGDP00552", "HG00126", 
"NA17377", "Tuba19", "HGDP01314", "NA11201", "HG02724", "Nesk_25", 
"zapo0098", "HGDP01203", "Bishkek28439", "NA18940", "Est400", 
"HGDP00554", "mg31", "HGDP00798", "HGDP00722", "HGDP01018", "HG00128", 
"HG02783", "Ul5", "Kor82", "HGDP00548", "HGDP00855", "HGDP01078", 
"NOR111", "Mansi41", "DNK11", "Mansi79", "HGDP00019", "HGDP00796", 
"Utsa21", "mixe0002", "Armenian222", "HGDP01345", "HGDP01417", 
"Kayseri24424", "Kusunda02", "HGDP00656", "Kayseri23827", "Igor20", 
"mg27", "HG02574", "mixe0042", "HG02790", "HG03006", "HGDP00286", 
"NA19044", "NA21581", "SA0342", "NorthOssetia12", "HGDP01242", 
"HG03100", "HGDP00530", "HGDP00428", "HGDP01312", "HGDP00027", 
"Est375", "HGDP00208", "HGDP00549", "HGDP00328", "abh107", "zapo0099", 
"HGDP00553", "HGDP00951", "HG01846", "HGDP01402", "HGDP01253", 
"Ale20", "Ayodo_81S", "BulgarianB4", "Nlk18", "HG02464", "HGDP00932", 
"HGDP00773", "Kusunda15", "BulgarianC1", "armenia293", "HGDP00616", 
"HG01504", "DNK05", "HGDP01365", "mixa0099", "HG01600", "HGDP01274", 
"HGDP00551", "NA19023", "iran11", "ALB212", "Ale22", "HGDP00160", 
"HGDP01364", "HGDP01012", "HG02943", "HGDP01323", "abh100", "Tuba9", 
"Sam02", "HGDP01215", "Ul31", "HG03078", "Ayodo_430C", "HGDP00597", 
"lez49", "HGDP00474", "Ayodo_502C", "lez42", "ch113", "HGDP00157", 
"HGDP01338", "HGDP00650", "HGDP00556", "HGDP01199", "NA15203", 
"HGDP01047", "HGDP00903", "Sir26", "Jordan603", "NA15202", "iran17", 
"HGDP00232", "Utsa22", "HG03085", "HGDP01035", "Jordan214", "HGDP01034", 
"HGDP00991", "HGDP00713", "HGDP00928", "HGDP00338", "HGDP00457", 
"HGDP00717", "HGDP01414", "HGDP00461", "HGDP01030", "HGDP01028", 
"HGDP00915"), class = "factor"), value = c(0.568026, 0.586163, 
0.611686, 0.615131, 0.617185, 0.622274, 0.626596, 0.634903, 0.638516, 
0.642894, 0.645012, 0.646246, 0.646643, 0.651504, 0.659362, 0.66035, 
0.693463, 0.748575, 0.775585, 0.799904, 0.809495, 0.810797, 0.813196, 
0.815898, 0.828594, 0.830746, 0.831749, 0.839394, 0.851589, 0.854254, 
0.855906, 0.856021, 0.85647, 0.857044, 0.857156, 0.857976, 0.858112, 
0.858384, 0.858926, 0.860694, 0.860702, 0.860986, 0.861457, 0.861628, 
0.862806, 0.863012, 0.863729, 0.864315, 0.864371, 0.864374, 0.865234, 
0.865495, 0.86583, 0.866675, 0.866983, 0.868242, 0.869689, 0.869762, 
0.869845, 0.870519, 0.870821, 0.871134, 0.871593, 0.871753, 0.871931, 
0.873242, 0.87332, 0.873374, 0.87366, 0.87414, 0.874163, 0.874369, 
0.87446, 0.874509, 0.874528, 0.874643, 0.874838, 0.87535, 0.875595, 
0.875707, 0.876403, 0.876409, 0.876425, 0.876552, 0.876586, 0.876844, 
0.87685, 0.876926, 0.876986, 0.877308, 0.877446, 0.877482, 0.877994, 
0.878208, 0.878836, 0.878899, 0.87894, 0.879029, 0.879148, 0.879171, 
0.879554, 0.879579, 0.879708, 0.879831, 0.880295, 0.880383, 0.880435, 
0.880438, 0.880496, 0.880511, 0.880531, 0.880572, 0.880714, 0.880739, 
0.880881, 0.881043, 0.881261, 0.881282, 0.881284, 0.881339, 0.881367, 
0.881484, 0.881486, 0.88157, 0.881974, 0.881982, 0.882005, 0.882182, 
0.882322, 0.882348, 0.882413, 0.882549, 0.882566, 0.88257, 0.882615, 
0.88282, 0.88289, 0.88296, 0.882994, 0.883129, 0.883145, 0.883264, 
0.883329, 0.883363, 0.883431, 0.883586, 0.883737, 0.88375, 0.883824, 
0.884139, 0.884221, 0.884251, 0.88438, 0.88438, 0.884433, 0.884435, 
0.884652, 0.884653, 0.884655, 0.884817, 0.884844, 0.885007, 0.885098, 
0.885134, 0.885139, 0.885225, 0.885253, 0.885263, 0.885312, 0.885339, 
0.885368, 0.885419, 0.885469, 0.885578, 0.885686, 0.885717, 0.885723, 
0.885762, 0.885836, 0.885843, 0.885872, 0.88603, 0.886063, 0.886112, 
0.886188, 0.886225, 0.886261, 0.886298, 0.886304, 0.88641, 0.886415, 
0.886488, 0.88649, 0.886531, 0.886567, 0.886641, 0.886672, 0.886673, 
0.886703, 0.886772, 0.886801, 0.886898, 0.886928, 0.886931, 0.887006, 
0.887128, 0.887154, 0.887189, 0.887366, 0.887373, 0.887508, 0.887675, 
0.887703, 0.887736, 0.887782, 0.887811, 0.88784, 0.887906, 0.888032, 
0.888162, 0.888187, 0.888203, 0.888264, 0.888264, 0.888365, 0.888382, 
0.888386, 0.888428, 0.888446, 0.888484, 0.888519, 0.888545, 0.888584, 
0.888627, 0.888649, 0.888734, 0.888886, 0.888915, 0.888999, 0.889029, 
0.889035, 0.889068, 0.889124, 0.889333, 0.889496, 0.889727, 0.889898, 
0.890081, 0.890192, 0.890197, 0.89028, 0.890416, 0.890417, 0.890432, 
0.890526, 0.890692, 0.890903, 0.890929, 0.890992, 0.891027, 0.891045, 
0.891088, 0.891198, 0.891241, 0.891266, 0.891418, 0.891555, 0.891697, 
0.891863, 0.891935, 0.892073, 0.892172, 0.892176, 0.892227, 0.892872, 
0.892909, 0.892915, 0.893008, 0.893234)), row.names = c(NA, -279L
), class = c("tbl_df", "tbl", "data.frame"))

Now, one thing might be at the root of this issue is not having numerical data on the x-axis; if so, it is still feasible somehow?
Also, I added the three lines before the actual plot as looking up a bit it appears to be one solution to sort the categorical values on the x-axis in ascending order based on their value on the y-axis. If anyone has any idea, any help is much appreciated, thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To plot categories in a specific order, it’s typical to use factors like you have here. If we use as.numeric(some_factor), we’ll get the ordering, which we could use for geom_smooth. The span parameter of stats::loess controls the degree of smoothing, which I’ve set manually here to follow this data more closely.

... 
geom_smooth(aes(x = as.numeric(sample)), method = "loess", span = 0.1) +
...

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading