Re: GGSAVE does not save a good version of my graph
CALUM POLWART <[email protected]> Sat, 25 Jul 2026 05:56:08 +0100
| Newsgroups | gmane.comp.lang.r.general |
|---|---|
| Message-ID | <CA+etgPmtdBmTa7vSXN9=Y_bjjc6zdpWsJSeMMWWGkS6TUMEAtg@mail.gmail.com> |
I'm late to the party, and as others have said - we can't see your Word
document as attachments aren't allowed.
That means the easiest way for us to test something is to reproduce an
equivalent graph with data we can access (mtcars, penguins etc). I often
find building a demo case with as much trimmed out actually helps me find
the issue (or at least that it's not reproducible and so I find the big in
my code). Not sure that would apply here.
Studio's little render window and ggsave never match in my experience.
Different image sizes being the cause. But that might not be what you
experiencing. But you may want to check out this SO page if it is -
https://stackoverflow.com/questions/77188670/ggplot2-fix-absolute-size-of-g=
gplot-on-onscreen-device-rstudio-plots-pane
Tiff, png and jpg are all raster formats. I'm dubious if a journal wants
that. Double check their requirements. Do they offer a vector format - EPS,
SVG, PDF? Your ggplot is lines - which are vectors. Vectors scale
better. If a journal says they want tiff I think they are expecting
photographs etc rather than graphs... There is often a further section for
vectors.
If I recall svg needs the svglite package installed and ggplot throws an
error if it's not found (just run install.packages("svglite") once )
Without the difference reproducible to me I can't answer your Q. But I just
thought I'd comment on some of your code:
current_time <- Sys.time()
current_time
# Convert colons to dashes, remove spaces
formatted_time <- gsub(":", "-", format(current_time, "%Y-%m-%d_%H-%M-%S"))
formatted_time
I'm not sure you need the gsub at all.
format(current_time, "%Y-%m-%d_%H-%M-%S")
That piece of code is rendering the date/time without colons anyway
And I would have either wrapped it up like this-
format(
Sys.time(),
"%Y-%m-%d_%H-%M-%S"
)
Or in the modern pipe world like this -
Sys.time() |>
format ("%Y-%m-%d_%H-%M-%S")
Obviously neither of those capture the result to an object for reuse so
actually
Sys.time() |>
format ("%Y-%m-%d_%H-%M-%S") -> formatted_time
Your code of course works fine, just had a lot of redundancy.
>
>
>
>
> On Thu, Jul 23, 2026 at 3:45=E2=80=AFPM Sorkin, John <[email protected]=
nd.edu>
> wrote:
>
> > I am running R in RStudio. I am creating a graph, which I see in the pl=
ot
> > window, and I use ggsave() to save a copy of the graph in a file.
> >
> > When I use look at the graph is RStudio's Plots window, the graph looks
> > fine. When I click on the Plot window's export button, copy the plot to
> the
> > clipboard and paste the graph into MS word, the graph looks fine. Please
> > see first figure on attached MS word document.
> >
> > When I copy and paste the .tiff file saved by ggsave, the graph looks
> bad.
> > Second figure on the attached MS word document. I think the graphs is
> being
> > exported improperly by ggsave. I would appreciate any suggestions for
> > improving the quality of the graph produced by ggsave.
> >
> > Please see code below and attached MS word document
> >
> >
> >
> >
> > # Code to produce graph
> > ## Plot
> > zz <- ggplot(
> > df.long,
> > aes(
> > x =3D Date,
> > y =3D NumPeopleExposed,
> > color =3D factor(criticalvalue),
> > linetype =3D factor(criticalvalue),
> > group =3D factor(criticalvalue)
> > )
> > ) +
> > geom_point(size =3D 3) +
> > geom_line(linewidth =3D 1.5) +
> >
> > scale_linetype_manual(
> > values =3D c(
> > "solid",
> > "dashed",
> > "dotted",
> > "dotdash",
> > "longdash",
> > "twodash"
> > )
> > ) +
> >
> > labs(
> > title =3D species,
> > x =3D "Date",
> > y =3D "Number of People Exposed to Toxic Concentration",
> > color =3D "Critical Value",
> > linetype =3D "Critical Value"
> > ) +
> >
> > ## Show every date on x-axis
> > scale_x_date(
> > breaks =3D sort(unique(df.long$Date)),
> > date_labels =3D "%Y-%m-%d"
> > ) +
> >
> > theme_bw() +
> >
> > theme(
> > axis.text.x =3D element_text(
> > angle =3D 45,
> > hjust =3D 1,
> > vjust =3D 1
> > )
> > )
> > print(zz)
> >
> > Code to save the graph
> > current_time <- Sys.time()
> > current_time
> > # Convert colons to dashes, remove spaces
> > formatted_time <- gsub(":", "-", format(current_time,
> > "%Y-%m-%d_%H-%M-%S"))
> > formatted_time
> >
> > #species=3D"JDS"
> > # Save graph
> > mypath <- file.path("C:","Users","JSorkin","OneDrive - University of
> > Maryland School of Medicine","HalemMilton","PaperAndAbstract")
> > mypath
> >
> > myfilename <- paste0(species2,formatted_time,".tiff")
> > myfilename
> >
> > ggsave(plot=3Dzz,
> > path=3Dmypath,
> > filename=3Dmyfilename,
> > device=3D"tiff",
> > width=3D4,height=3D3,
> > units=3D"in",
> > dpi=3D600)
> >
> > cat("Ending (12) Plot Fraction Exposed\n")
> > }
> >
> >
> >
> >
> > John David Sorkin M.D., Ph.D.
> > Professor of Medicine, Univer
> <https://www.google.com/maps/search/or+of+Medicine,+Univer?entry=3Dgmail&=
source=3Dg>sity
> of Maryland School of Medicine;
> > Associate Director for Biostatistics and Informatics, Baltimore VA
> Medical
> > Center Geriatrics Research, Education, and Clinical Center;
> > Former PI Biostatistics and Informatics Core, University of Maryland
> > School of Medicine Claude D. Pepper Older Americans Independence Center;
> > Senior Statistician University of Maryland Center for Vascular Research;
> >
> > Division of Gerontology, Geriatrics and Palliative Medicine,
> > 10 North Greene Street
> > GRECC (BT/18/GR)
> > Baltimore, MD 21201-1524
> > Cell phone 443-418-5382
> >
> >
> > ______________________________________________
> > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > https://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]