Changing the size of line drawn with stat_function

Changing the size of line drawn with stat_function



I'm trying to plot a series of functions with stat_function in ggplot2 on R. The function remains the same throughout, but some of the parameters change according to values in a data frame. I used a for loop to plot each different form of the function.


stat_function



I want to use different sizes for the lines drawn, but no matter how I change the aes parameter, the size of the lines drawn by stat_function remains constant. I can change the size of other lines in the plot just fine.


aes


stat_function



The code


test_fun <- function(x,d,fstart,fend,h)
mp=mean(c(fstart, fend))
a=h/((mp-fstart)*(mp-fend))
a*(x-fstart)*(x-fend)

plot <- ggplot() + xlim(min(gtf.tx$start), max(gtf.tx$end))+ ylim(-pheight/2,pheight/2)
for(i in 1:nrow(rmats_to_plot))
weight=rmats_to_plot[i,5]*100
start=rmats_to_plot[i,1]
end=rmats_to_plot[i,2]
flip=rmats_to_plot[i,6]
height=rmats_to_plot[i,8]
plot <- plot+stat_function(mapping = aes(size=1),xlim = c(start,end),fun = test_fun,args = list(fstart=start,fend=end, d=flip,h=height))


plot <- plot + geom_hline(yintercept = 0,size=7,color='white')+
geom_hline(yintercept=0,alpha=.9, size=1)+geom_segment(data = gtf.tx,aes(x=start,xend=end,y=0, yend=0),size=7)+theme_minimal()



and the data


start end sample_1 sample_2 total_weight flip size height
31.98 32.71 20.5 39 0.1662 -1 0.73 20
33.36 49.86 16.5 27.5 0.1229 -1 16.5 40
12.13 29.21 20.5 39 0.1662 -1 17.08 60
12.13 32.71 28.5 34 0.1746 1 20.58 -20
51.17 79.79 16.5 40 0.1578 1 28.62 -40
12.13 49.86 21.5 46 0.1885 1 37.73 -60
33.36 79.79 4 2.5 0.0182 -1 46.43 80
12.13 79.79 0 2 0.0056 1 67.66 -80



any help would be appreciated






You should add "width" argument to your code. Please refer to the below article for more details. https://stackoverflow.com/questions/12985091/print-data-frame-with-columns-center-aligned

– Prasan Karunarathna
Sep 10 '18 at 21:47




1 Answer
1



Since you want size to be a specific value, you should put it outside the aes.


size


aes



By putting size=1 in the aes, you're setting up a scale which you need to define with a scale_size_* function. It's the same thing as if you put size='dog'


size=1


aes


scale_size_*


size='dog'


ggplot(mtcars) + geom_point(aes(x=mpg, y=disp, size = 1))



enter image description here


ggplot(mtcars) + geom_point(aes(x=mpg, y=disp, size = 9))



enter image description here


ggplot(mtcars) + geom_point(aes(x=mpg, y=disp), size = 9)



enter image description here






Thank you! that worked

– Vinay Swamy
Sep 11 '18 at 14:59



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế