1

I call the ggplot package in Julia as shown in this website: https://avt.im/blog/2018/03/23/R-packages-ggplot-in-julia. I use the package as shown in that website and everything works fine.

Now i plot the average marginal effects in Julia using the Effects package. I want to plot it using ggplot here is the data i have:

df = effects(design, m1)

enter image description here

Here is my ggplot code and the error:

ggplot(df, aes(unemploy, workhours, group = sex, shape= sex, linetype=sex)) + 
  geom_point(position=position_dodge(width=0.15)) +
  geom_errorbar(aes(ymin = lower, ymax = upper),width = 0.1,
                linetype = "solid",position=position_dodge(width=0.15))+
  geom_line(position=position_dodge(width=0.15))  

UndefVarError: sex not defined

Stacktrace:
 [1] top-level scope
   @ In[131]:1
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

I tried this code before in R on the same dataframe and it worked fine, so the problem i guess is that ggplot is not reading the data as it should. Could someone help me to get around this problem?

Jack
  • 813
  • 4
  • 17

1 Answers1

1

Your link actually explains it in this small snippet:

ggplot(d, aes(x=:x,y=:y)) + geom_line()

Note that the aes function uses Julia symbols like :x to refer to data frame columns.

In your line aes(unemploy, workhours, group = sex, shape= sex, linetype=sex), sex is treated like any other variable in Julia, specifically that Julia tries to look for the existing variable sex in the code and the object it references. It was not found in your code, hence the UndefVarError.

Non-standard evaluation grants R's aes the capability to see group=sex and treat sex as a name instead of trying to evaluate it. This slightly resembles how Julia's macros work on unevaluated expressions prior to the compile phase, but R has a very different style.

BatWannaBe
  • 4,330
  • 1
  • 14
  • 23
  • Thanks for the answer, i am sorry but i am new to Julia and it is still a bit complicated. I am not sure that i understood you fully regarding the `group=sex`. When i remove them, the code works fine now, but i still need to include them and i am not sure that i understood how to do so according to Julia's code language. I tried `group =:sex`, but that did not work. – Jack Nov 12 '21 at 13:18
  • 1
    I didn't say it outright and perhaps I should have. Make this edit to the `aes` call: `aes(unemploy, workhours, group = :sex, shape= :sex, linetype=:sex)` – BatWannaBe Nov 12 '21 at 13:20
  • It worked perfectly, thanks! last time to bother you with this, but i am trying to change the scale in this graph by including this code as i usually do in R: `scale_y_continuous(limits = c(0, 40), breaks = seq(0, 40, by = 2))`, i get the following error: `UndefVarError: c not defined `. I tried to tweak by puting the double dots `:` operator, but it does not work. – Jack Nov 12 '21 at 13:31
  • 1
    When people have another question, I would normally suggest that they do it in a separate post instead of attaching it to a previous question. But I have a different suggestion: do some reading on how to code in Julia. I'm sure there are even some articles just for translating from R to Julia, start there. Your error there is because `c` isn't a combined vector function in Julia. And why would it be? Julia and R are different languages, so you shouldn't expect Julia's compiler to understand R code or for R and Julia to be designed the same way. – BatWannaBe Nov 12 '21 at 13:37
  • 1
    I will also link this bit in the Julia docs that lists differences to R. I wouldn't suggest starting there because it's not organized with examples as Julia tutorials, but it does point out how the languages "constructs vectors" among other common habit traps (docs.julialang.org/en/v1/manual/noteworthy-differences/…) – BatWannaBe Nov 12 '21 at 14:03