Create individual plot and save as pdf

Hi … i used to use this code in r studio to generate individual plots for each id and save them as combined pdf for the offline evaluation. can someone help me to translate code to julia.

pdf("path\\indplots.pdf")
for(i in unique(df$ID))
{
  print((ggplot(subset(df,df$ID == i),aes(x=TAD,y=DV))+
           geom_point()+
           xlab("TAD (hrs)")+
           ylab("Plasma concentration (mg/L)")+
           ggtitle(paste(as.character(i)))+
           theme(panel.grid.minor=element_blank(),
                 panel.grid.major=element_blank(),
                 panel.background = element_blank(),
                 plot.title=element_text(hjust=0.5),
                 legend.position="right",
                 legend.key=(element_rect(colour='white',fill='white')),
                 plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"),
                 legend.text=element_text(colour="black",size=10),
                 axis.text.x=element_text(colour="black",size=10),
                 axis.text.y=element_text(colour="black",size=10),
                 axis.title=element_text(colour="black",size=14),
                 axis.line.x = element_line( colour="black",size=1,linetype ="solid"),
                 axis.line.y = element_line( colour="black",size=1,linetype ="solid"))))
}
dev.off()

here i am focusing more on saving the individual plots as combined pdf ,than the codes to customise plot.
Thanks.

1 Like

Maybe this?

2 Likes

That would be a GR solution:

using GR
beginprint("plots.pdf")
for i in 1:10 plot(randn(10,4)) end
endprint()
4 Likes
julia> using GR

julia> using Plots

julia> beginprint("plots.pdf")

julia> for i in 1:10 plot(randn(10,4)) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> endprint()

may i get some help, my plots stopped working here !.

Plots.jl doesn’t support it natively. But you can trick Plots:

using Plots

Plots.GR.beginprint("plots.pdf")
gr(show=true)

for i in 1:10 plot(randn(10)) end

Plots.GR.endprint()

Not very pretty, but works.

3 Likes

I encountered the following error using your code (GR version: 0.63.1):

SystemError: opening file "C:\\Users\\ytian\\AppData\\Local\\Temp\\jl_SbuQV4fEbT.svg": No such file or directory

Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:168
  [2] #systemerror#62
    @ .\error.jl:167 [inlined]
  [3] systemerror
    @ .\error.jl:167 [inlined]
  [4] open(fname::String; lock::Bool, read::Bool, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing)
    @ Base .\iostream.jl:293
  [5] open(fname::String, mode::String; lock::Bool)
    @ Base .\iostream.jl:355
  [6] open
    @ .\iostream.jl:355 [inlined]
  [7] _readfile(path::String)
    @ GR C:\Users\ytian\.julia\packages\GR\KPElO\src\GR.jl:3710
  [8] show()
    @ GR C:\Users\ytian\.julia\packages\GR\KPElO\src\GR.jl:3768
  [9] plot_data(flag::Bool)
    @ GR.jlgr C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1553
 [10] plot_data
    @ C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1198 [inlined]
 [11] plot(args::Matrix{Float64}; kv::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ GR.jlgr C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1763
 [12] plot
    @ C:\Users\ytian\.julia\packages\GR\KPElO\src\jlgr.jl:1755 [inlined]
 [13] top-level scope
    @ .\In[2]:4
 [14] eval
    @ .\boot.jl:360 [inlined]
 [15] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base .\loading.jl:1116

still same error

julia> using Plots

julia> Plots.GR.beginprint("plots.pdf")

julia> gr(show=true)
Plots.GRBackend()

julia> for i in 1:10 plot(randn(10)) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> Plots.GR.endprint()

Please use the following:

for i in 1:10 GR.plot(randn(10)) end

No, that was not the idea. You probably forgot to restart Julia and β€œused” both GR and Plots, which cannot work.

Thank you, it works now.
my purpose is to get a separate for each indiviadul . but this code gives me a plot od all values in single page . please rectify the code
this is the code i have used

df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
Plots.GR.beginprint("plots.pdf")
gr(show=true)
for i in df.id GR.plot(df.a,df.b) end
Plots.GR.endprint()

i want df.a vs df.b plot for each id in a separate pages of pdf file.
Thank you

using DataFrames
using Plots
gr(show=true)
df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
Plots.GR.beginprint("plots.pdf")
for i in df.id plot(df.a,df.b .* i) end
Plots.GR.endprint()

The result is one PDF with 5 pages.

2 Likes

i copied the excat code , not undersating why plots are not working

julia> using DataFrames

julia> using Plots

julia> gr(show=true)
Plots.GRBackend()

julia> df = DataFrame(id = 1:5, a = 10:14, b = 5:9)
5Γ—3 DataFrame
 Row β”‚ id     a      b     
     β”‚ Int64  Int64  Int64 
─────┼─────────────────────
   1 β”‚     1     10      5
   2 β”‚     2     11      6
   3 β”‚     3     12      7
   4 β”‚     4     13      8
   5 β”‚     5     14      9

julia> Plots.GR.beginprint("plots.pdf")

julia> for i in df.id plot(df.a,df.b .* i) end
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope
   @ ./none:1

julia> Plots.GR.endprint()

Are you in a notebook? Did you really restart the Julia kernel? Or, do you β€œauto-load” packages in your startup files?

im on atom. i have comletely closed and open the julia . still the same result

this worked after changing the plot to Plots.plot.
but all the 5 pages have same plot.
plot i want is ,
10 on x , 5 on y for id 1 in first page,
11 on x , 6 on y for id 2 in second page
.
.
14 on x , 9 on y for id 5 in last page.

just plots for individual id’s.

is there a way to get that. Thank you

Sorry - can’t help furthermore as I cannot reproduce the behaviour on any platform.

1 Like

thank you very much for your support.

can you say , how you got plots.
each id was printed in separate page ?

Could you help figure out the problem in this post:

Thanks!