Julia 与 R 的等效 ggplot 代码是什么?

Jad*_*Jad 7 ggplot2 julia

我想在 Julia 中绘制一个复杂的图表。下面的代码是使用 ggplot 的 Julia 版本。

using CairoMakie, DataFrames, Effects, GLM, StatsModels, StableRNGs, RCall
@rlibrary ggplot2

rng = StableRNG(42)
growthdata = DataFrame(; age=[13:20; 13:20],
                       sex=repeat(["male", "female"], inner=8),
                       weight=[range(100, 155; length=8); range(100, 125; length=8)] .+ randn(rng, 16))

mod_uncentered = lm(@formula(weight ~ 1 + sex * age), growthdata)

refgrid = copy(growthdata)
filter!(refgrid) do row
    return mod(row.age, 2) == (row.sex == "male")
end
effects!(refgrid, mod_uncentered)

refgrid[!, :lower] = @. refgrid.weight - 1.96 * refgrid.err
refgrid[!, :upper] = @. refgrid.weight + 1.96 * refgrid.err

df= refgrid

ggplot(df, aes(x=:age, y=:weight, group = :sex, shape= :sex, linetype=:sex)) + 
  geom_point(position=position_dodge(width=0.15)) +
  geom_ribbon(aes(ymin=:lower, ymax=:upper), fill="gray", alpha=0.5)+
  geom_line(position=position_dodge(width=0.15)) + 
  ylab("Weight")+ xlab("Age")+
  theme_classic()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

不过,我想稍微修改一下这张图。例如,我想更改 y 轴的比例、功能区的颜色、添加一些误差线,以及更改图例的文本大小等。由于我是 Julia 的新手,因此我没有成功找到这些修改的等效语言代码。有人可以帮我将 ggplot 下面的 R 代码翻译成 Julia 的语言吗?

t1= filter(df, sex=="male") %>% slice_max(df$weight) 


ggplot(df, aes(age, weight, group = sex, shape= sex, linetype=sex,fill=sex, colour=sex)) + 
  geom_line(position=position_dodge(width=0.15)) +
  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_ribbon(aes(ymin = lower, ymax = upper, fill = sex, colour = sex), alpha = 0.2) +
  geom_text(data = t1, aes(age, weight, label = round(weight, 1)), hjust = -0.25, size=7,show_guide  = FALSE) +
  scale_y_continuous(limits = c(70, 150), breaks = seq(80, 140, by = 20))+
  theme_classic()+
  scale_colour_manual(values = c("orange", "blue")) +
  guides(color = guide_legend(override.aes = list(linetype = c('dotted', 'dashed'))),
         linetype = "none")+
  xlab("Age")+ ylab("Average marginal effects") + ggtitle("Title") +
  theme( 
    axis.title.y = element_text(color="Black", size=28, face="bold", hjust = 0.9),
    axis.text.y = element_text(face="bold", color="black", size=16),
    plot.title = element_text(hjust = 0.5, color="Black", size=28, face="bold"),
    legend.title = element_text(color = "Black", size = 13),
    legend.text = element_text(color = "Black", size = 16),
    legend.position="bottom",
    axis.text.x = element_text(face="bold", color="black", size=11),
    strip.text = element_text(face= "bold", size=15)
  ) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bat*_*aBe 4

正如我之前评论的,您可以使用 R 字符串来运行 R 代码。需要明确的是,这与您帖子的方法不同,您将包装许多 R 对象的许多 Julia 对象拼凑在一起,这是 RCall 将 Julia 数据帧转换为 R 数据帧,然后运行 ​​R 代码。

运行 R 脚本可能看起来不太符合儒略标准,但代码重用却很符合儒略标准。此外,无论哪种方式,您仍然使用 R 库和活动 R 会话,并且减少创建包装器对象以及在 Julia 和 R 之间切换的频率甚至可能会带来轻微的性能优势。

## import libraries for Julia and R; still good to do at top

using CairoMakie, DataFrames, Effects, GLM, StatsModels, StableRNGs, RCall
R"""
library(ggplot2)
library(dplyr)
"""

## your Julia code without the @rlibrary or ggplot lines

rng = StableRNG(42)
growthdata = DataFrame(; age=[13:20; 13:20],
                       sex=repeat(["male", "female"], inner=8),
                       weight=[range(100, 155; length=8); range(100, 125; length=8)] .+ randn(rng, 16))

mod_uncentered = lm(@formula(weight ~ 1 + sex * age), growthdata)

refgrid = copy(growthdata)
filter!(refgrid) do row
    return mod(row.age, 2) == (row.sex == "male")
end
effects!(refgrid, mod_uncentered)

refgrid[!, :lower] = @. refgrid.weight - 1.96 * refgrid.err
refgrid[!, :upper] = @. refgrid.weight + 1.96 * refgrid.err

df= refgrid

## convert Julia's df and run your R code in R-string
## - note that $df is interpolation of Julia's df into R-string,
##   not R's $ operator like in rdf$weight
## - call the R dataframe rdf because df is already an R function

R"""
rdf <- $df
t1= filter(rdf, sex=="male") %>% slice_max(rdf$weight) 

ggplot(rdf, aes(age, weight, group = sex, shape= sex, linetype=sex,fill=sex, colour=sex)) + 
  geom_line(position=position_dodge(width=0.15)) +
  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_ribbon(aes(ymin = lower, ymax = upper, fill = sex, colour = sex), alpha = 0.2) +
  geom_text(data = t1, aes(age, weight, label = round(weight, 1)), hjust = -0.25, size=7,show_guide  = FALSE) +
  scale_y_continuous(limits = c(70, 150), breaks = seq(80, 140, by = 20))+
  theme_classic()+
  scale_colour_manual(values = c("orange", "blue")) +
  guides(color = guide_legend(override.aes = list(linetype = c('dotted', 'dashed'))),
         linetype = "none")+
  xlab("Age")+ ylab("Average marginal effects") + ggtitle("Title") +
  theme( 
    axis.title.y = element_text(color="Black", size=28, face="bold", hjust = 0.9),
    axis.text.y = element_text(face="bold", color="black", size=16),
    plot.title = element_text(hjust = 0.5, color="Black", size=28, face="bold"),
    legend.title = element_text(color = "Black", size = 13),
    legend.text = element_text(color = "Black", size = 16),
    legend.position="bottom",
    axis.text.x = element_text(face="bold", color="black", size=11),
    strip.text = element_text(face= "bold", size=15)
  ) 
"""
Run Code Online (Sandbox Code Playgroud)

结果与您帖子的 R 代码相同: RCall 的 R 字符串中的 OP 的 R 代码得出此图