# Ggplot2 does not accept the function "theme"

**URL:** <https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [June 15, 2020, 9:59pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474 "2020-06-15T21:59:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas\_vilches](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_vilches/32/7391_2.png) [@thomas\_vilches](https://discourse.julialang.org/u/thomas_vilches)\
**Post date:** [June 15, 2020, 9:59pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/1 "2020-06-15T21:59:45Z")

</div>

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:

```nohighlight

@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))
```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [June 15, 2020, 11:32pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/2 "2020-06-15T23:32:56Z")

</div>

Wrap your R code if its a complex R expression.

```julia
retval = R"""
   r code here.
"""
```

---

<div class="post-metadata">

**Author:** ![thomas\_vilches](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_vilches/32/7391_2.png) [@thomas\_vilches](https://discourse.julialang.org/u/thomas_vilches)\
**Post date:** [June 16, 2020, 1:13am UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/3 "2020-06-16T01:13:54Z")

</div>

Thanks man! Worked!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 16, 2020, 6:12am UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/4 "2020-06-16T06:12:51Z")

</div>

Also maybe have a look at [`Gadfly.jl`](http://gadflyjl.org/stable/), which is the pure-Julia plotting library that is “based on ggplot2” and the Grammar of Graphics.

---

<div class="post-metadata">

**Author:** ![paula\_calvo](https://avatars.discourse-cdn.com/v4/letter/p/c4cdca/32.png) [@paula\_calvo](https://discourse.julialang.org/u/paula_calvo)\
**Post date:** [April 17, 2021, 5:54pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/5 "2021-04-17T17:54:50Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [March 7, 2022, 8:18pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/6 "2022-03-07T20:18:48Z")

</div>

> [@paula\_calvo](#):
>
> R"r code

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()).

---

<div class="post-metadata">

**Author:** ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)\
**Post date:** [May 15, 2022, 5:59pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/7 "2022-05-15T17:59:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)\
**Post date:** [May 15, 2022, 6:04pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/8 "2022-05-15T18:04:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [May 15, 2022, 7:29pm UTC](https://discourse.julialang.org/t/ggplot2-does-not-accept-the-function-theme/41474/9 "2022-05-15T19:29:16Z")

</div>

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

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

```
