StirlingCodingClub / studyGroup

Gather together a group to skill-share, co-work, and create community
http://StirlingCodingClub.github.io/studyGroup/
Other
2 stars 1 forks source link

Default dims for saving ggplots and recommended workflow ggplot #34

Open SamPaplauskas opened 4 years ago

SamPaplauskas commented 4 years ago

Issue one: Default dims for saving ggplots

Interested in how people save their ggplots for reproducible figures.

There are a variety of methods for saving plots including some which do not require dimensions specifications, such as my go to method which is saving plots directly from RStudio plotspace, and others which allow you to specify dimensions such as using the command line e.g. ggsave(plot = p, width = 3, height = 3, dpi = 300, filename = "not squished axis.pdf")

I would like to know what people have used from their own experience to save plots and do so in a way that they can be easily reproduced with a similar looking / sized image every time you save.

Issue two: Recommended workflow ggplot

For long plotting code, is there a neat workflow that can be recommended from users own experience? This question comes to mind when choosing between chunks of code e.g. ggplot(....) + ... + ...

versus an alternative

Plot <- ggplot(..) plot + ...

jejoenje commented 4 years ago

Does writing output to "devices" not work with ggplot()? I.e. for a base R plot i would just do something like this:

dat = rnorm(1000, 10, 15)
tiff("saved_plot.tiff")
plot(dat)
dev.off()

Anything between the tiff() and dev.off() statements will get printed to the file specified. There's loads of options that you can specify in the graphics devices, like figure width, height, resolution, etc. If you don't want a TIFF as output, you can also use jpg() or png(). I would have thought the same would apply to ggplot() output? But then, there's lots of stuff about ggplot() I don't understand... :)

SamPaplauskas commented 4 years ago

Yes it is as easy as that BUT...I am not in the habit of specifying the dimensions of my plots as I create them because you are always changing and updating plot stuff (and I always resize my figures in word many (a million) times before I think I have a 'final' correct size with associated dimensions)...therefore -> I was wondering if there is a workaround people use to avoid this frustrating scenario XD

BW, Sam

On Thu, 17 Oct 2019 at 14:46, Jeroen Minderman notifications@github.com wrote:

Does writing output to "devices" not work with ggplot()? I.e. for a base R plot i would just do something like this:

dat = rnorm(1000, 10, 15) tiff("saved_plot.tiff") plot(dat) dev.off()

Anything between the tiff() and dev.off() statements will get printed to the file specified. There's loads of options that you can specify in the graphics devices, like figure width, height, resolution, etc. If you don't want a TIFF as output, you can also use jpg() or png(). I would have thought the same would apply to ggplot() output? But then, there's lots of stuff about ggplot() I don't understand... :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/StirlingCodingClub/studyGroup/issues/34?email_source=notifications&email_token=AKNAA2VL3Q6HEQ2NN3WEFODQPBUBRA5CNFSM4JBZHL3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBQE2IA#issuecomment-543182112, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNAA2VI4UJBNWOAHXQBSR3QPBUBRANCNFSM4JBZHL3A .

-- Sam Paplauskas

PhD Research Student

Biological and Environmental Sciences | University of Stirling | Stirling | FK9 4LA | UK

bradduthie commented 4 years ago

Just a shameless plug for coding club next week -- I'll be showing how to write manuscripts in Rmarkdown instead of Word (which, once you get the hang of it, will make the problem of figure size pretty much go away).

SamPaplauskas commented 4 years ago

I will have to check that out...

but then I lose all of the good things about writing in word haha

Best wishes, Sam

On Thu, 17 Oct 2019 at 15:19, Brad Duthie notifications@github.com wrote:

Just a shameless plug for coding club next week -- I'll be showing how to write manuscripts in Rmarkdown instead of Word (which, once you get the hang of it, will make the problem of figure size pretty much go away).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/StirlingCodingClub/studyGroup/issues/34?email_source=notifications&email_token=AKNAA2QMDQ23KG3F4E3SVTLQPBX5PA5CNFSM4JBZHL3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBQINBA#issuecomment-543196804, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNAA2VXS4VR47JHTASGED3QPBX5PANCNFSM4JBZHL3A .

-- Sam Paplauskas

PhD Research Student

Biological and Environmental Sciences | University of Stirling | Stirling | FK9 4LA | UK

jejoenje commented 4 years ago

Just a shameless plug for coding club next week -- I'll be showing how to write manuscripts in Rmarkdown instead of Word (which, once you get the hang of it, will make the problem of figure size pretty much go away).

Before I read @bradduthie's reply here I almost typed "rmarkdown?" in reply, but changed my mind as I figured that would be a bit too terse... :)

On a serious note, @SamPaplauskas I am now not sure if I understand your question - on the one hand you are saying you don't want to specify dimensions, but then you are frustrated by the fact that it is not consistent? If you're after an automated way of "updating" figures in a document without the need to do any cutting/pasting, I fully agree with @bradduthie that Rmarkdown (or indeed markdown) is the best bet.

bradduthie commented 4 years ago

Ah, but you can still keep all of your favourite parts about Word, if you like! Rmarkdown can produce documents in PDF, HTML, or DOCX, so if at any point you decide that you really need to do something in Word, you can switch over with the push of a button.

mattnuttall00 commented 4 years ago

Hi Sam,

I tend to just use ggsave and work out the dimensions that I want. Once I have the dimensions that I like, you then use that code to produce nice plots that all have the same dimensions. Apologies if I have misunderstood your question, but surely by avoiding using the dimensions options you are precluding the production of uniform, reproducible plots? It can be a faff at the start to mess around with the dimensions until you get exactly what you want, but once you have them nailed, then it's super quick and easy and the plots all get spat out the same.

There is a way of saving (or perhaps copying) the plot in such a way that when you paste it into word/powerpoint etc the plot behaves like a plot produced in Word - i.e. you can just click on the axis labels, titles, axis ticks etc and edit it like you would a Word plot. But I can't for the life of me remember how to do it.....

For your second question, I prefer the first option i.e.

ggplot(...)+ geom_point()+ geom_line()+ geom_ribbon()+ ......

I don't really see the point in assigning the base plot to an object, and then adding different layers to that object (second option). But perhaps I am missing something? I suppose the end result is the same and the code is not hugely different. Perhaps it's just personal preference...

Cheers Matt

SamPaplauskas commented 4 years ago

Thanks for the advice all...

For clarity @jejoenje ...was just hoping to get around the the fact that RStudio plotspace doesnt display my figures with certain specified dims, so you only see the final plot after exporting - so you can see how this quickly becomes a bit of a faff to move from one workspace to another just to see if your plots are any good. Although you may not get exact copies by using Rstudio plotspace to do a direct save of the plot, there are benefits to saving plots from the Rstudio plotspace because you can ressize manually with an updating live figure. So I am just looking at how to avoid this tradeoff (between avoiding use of mutliple workspaces, the ability to optimise dims, and getting the dims reproduced).

I like your point about word / pp and also agree that the uses of "object plotting" are limited @mattnuttall00, but consider the case where you want to establish a template to use for multiple plots...

BW, Sam

On Thu, 17 Oct 2019 at 17:21, Matt Nuttall notifications@github.com wrote:

Hi Sam,

I tend to just use ggsave and work out the dimensions that I want. Once I have the dimensions that I like, you then use that code to produce nice plots that all have the same dimensions. Apologies if I have misunderstood your question, but surely by avoiding using the dimensions options you are precluding the production of uniform, reproducible plots? It can be a faff at the start to mess around with the dimensions until you get exactly what you want, but once you have them nailed, then it's super quick and easy and the plots all get spat out the same.

There is a way of saving (or perhaps copying) the plot in such a way that when you paste it into word/powerpoint etc the plot behaves like a plot produced in Word - i.e. you can just click on the axis labels, titles, axis ticks etc and edit it like you would a Word plot. But I can't for the life of me remember how to do it.....

For your second question, I prefer the first option i.e.

ggplot(...)+ geom_point()+ geom_line()+ geom_ribbon()+ ......

I don't really see the point in assigning the base plot to an object, and then adding different layers to that object (second option). But perhaps I am missing something? I suppose the end result is the same and the code is not hugely different. Perhaps it's just personal preference...

Cheers Matt

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/StirlingCodingClub/studyGroup/issues/34?email_source=notifications&email_token=AKNAA2WJE6QDZKUMRI3HP33QPCGHPA5CNFSM4JBZHL3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBQWDGA#issuecomment-543252888, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKNAA2WJX4PEASFY6E5OV63QPCGHPANCNFSM4JBZHL3A .

-- Sam Paplauskas

PhD Research Student

Biological and Environmental Sciences | University of Stirling | Stirling | FK9 4LA | UK

mattnuttall00 commented 4 years ago

@SamPaplauskas I just stumbled across the function for exporting a plot into MS Office whereby the elements of the plot are editable as a plot made in Excel would be. The package is export and the function is graph2ppt, graph2doc etc...