Ggplot2 does not accept the function "theme"

I am trying to use ggplot2 inside julia, because I am used to its usage and I think it makes nice plots.

It works when making a basic plot, or using a “fixed theme” (such as theme_bw). however, when using “theme()” in orther to change tick size, font… it returns “UndefVarError: theme not defined”. (it was complaining about the dots for axis.text.x, I replaced by “_”.

Does anyone have any idea?

I thank you all in advance
Here is the code:


@rlibrary ggplot2
@rlibrary scales
@rlibrary rcartocolor
@rlibrary cowplot

mp=ggplot(data1,aes(x=:Qp,y=:Ep))+geom_point(color = color_v,size = 3)+theme(axis_text_x = element_text(colour="black", size = 10))

Wrap your R code if its a complex R expression.

retval = R"""
   r code here.
"""
4 Likes

Thanks man! Worked!

Also maybe have a look at Gadfly.jl, which is the pure-Julia plotting library that is “based on ggplot2” and the Grammar of Graphics.

2 Likes

Hi! I am having the same problem that Thomas had but I am not being able to solve it.

using RCall
@rlibrary ggplot2
@rlibrary gridExtra
@rlibrary scales

using DataFrames
df = DataFrame(Marital_Status = [“Marriage”, “Cohabitation”, “Marriage”, “Cohabitation”,
“Marriage”, “Cohabitation”, “Marriage”, “Cohabitation”],
Source = [“Model”, “Model”, “Data”, “Data”, “Model”, “Model”, “Data”, “Data”],
Education = [“Low Educated”, “Low Educated”, “Low Educated”, “Low Educated”,
“High Educated”, “High Educated”, “High Educated”, “High Educated”],
w = [0.3, 0.4, 0.5, 0.7, 0.1, 0.2, 0.5, 0.6])

a = ggplot(df, aes(x=:Source, y=:w, fill=:Marital_Status)) +
geom_bar(colour=“black”, stat=“identity”,
position=position_dodge(),
size=.3) + # Thinner lines
ylim(0, 1) +
scale_fill_hue(name=“Marital Status”) + # Set legend title
xlab("") + ylab(“Labor Force Participation”) + # Set axis labels
facet_wrap(R"~Education", ncol=1, scales = “free_y”) +
ggtitle(“Female Labor Force Participation”)

The codes works until here but when I add the theme function it does not work:

  • R""“theme(strip.background = element_rect(colour=“black”,
    fill=“white”))”""

I tried to wrap it in R""" r code here “”" as suggested, but still the error message says: could not find function “theme” . What I am doing wrong? I also tried R"r code", and:

r = R “”" theme(strip.background = element_rect(colour=“black”,
fill=“white”)) “”"

and adding that to my ggplot. Could you tell me what I am missing? I am just starting with Julia and ggplot, so sorry if this was answered.
Thanks!!!

I’m having the same problem with theme() in ggplot using RCall the work around I’ve found is to use the function theme_update(). This will change the theme for all subsequent plots so if you only want the changes made to a single plot you will have to go back and restore the default theme with theme_set(theme_gray()).

I have also run into the same issue. This looks like a namespace issue in which Plots.theme() is masking theme() from ggplot2. My workaround was to delete the “using Plots” dependency for this particular piece of code.

I am not sure whether one can specify the namespace for the R code theme() function, so that one can mix and match julia Plots and ggplot2 plots via RCall. Perhaps others can comment on this.

In my experience, ggplot2 is much more polished and user-friendly than any Julia plotting packages at this time. Using ggplot2 within julia with RCall is like the best of both worlds (fast code + ggplot2 plotting).

Hope to revise my opinion in the future, it would be great if julia plotting packages can catch up in terms of overall design and quality to ggplot2, but IMO they are not close yet.

There are several options for working with Plots.jl and ggplot2:

By default @rlibrary ggplot2 does not make ggplot2 symbol available. If you additionally @rimport ggplot2, then it’s possible to use ggplot2.theme.

Another option is to use R"""ggplot2::theme""" or do R"library(ggplot2)", which will load ggplot2 in the R side (not in Julia).

Yet another option could be to do

using RCall
@rlibrary ggplot2
theme # Since theme is used before Plots is loaded it will stay as ggplot2::theme
using Plots