简体   繁体   English

如何使用 geom_jitter 图制作散点图可重现?

[英]How to make scatterplot with geom_jitter plot reproducible?

I am using the Australian AIDS Survival Data.我正在使用澳大利亚艾滋病生存数据。 This time to create scatterplots.这次要创建散点图。

To show the genders in survival of different Reported transmission category (T.categ), I plot the chart in this way:为了显示不同报告传播类别(T.categ)的存活率,我以这种方式绘制图表:

data <- read.csv("https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/MASS/Aids2.csv")

data %>%
  ggplot() +
  geom_jitter(aes(T.categ, sex, colour = status))

It shows a chart.它显示了一个图表。 But each time I run the code, it seems to produce a different chart.但是每次运行代码时,它似乎都会生成不同的图表。 Here are 2 of them putting together.这是其中的 2 个组合。

在此处输入图片说明

Anything wrong with the codes?代码有什么问题吗? Is it normal (each run a different chart)?正常吗(每个运行不同的图表)?

if you use geom_point instead of geom_jitter , you can add position = position_jitter() , which accepts the seed argument:如果您使用geom_point而不是geom_jitter ,您可以添加position = position_jitter() ,它接受种子参数:

library(ggplot2)
p <- ggplot(mtcars, aes(as.factor(cyl), disp)) 

p + geom_point(position = position_jitter(seed = 42))


p + geom_point(position = position_jitter(seed = 1))

And back to "42"然后回到“42”


p + geom_point(position = position_jitter(seed = 42))

Created on 2020-07-02 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 7 月 2 日创建

Try setting the seed when plotting:绘图时尝试设置种子:

set.seed(1); data %>%
  ggplot() +
  geom_jitter(aes(T.categ, sex, colour = status))

From the manual ?geom_jitter :从手册?geom_jitter

It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.它为每个点的位置添加了少量随机变化,是处理较小数据集离散性引起的过度绘图的一种有用方法。

To have that "random variation" reproducible, we need to set set.seed when plotting.为了使“随机变化”可重现,我们需要在绘图时设置set.seed

如果我想做一些随机的,但可重复排列等的东西,我使用样本来设置种子: my.seed = sample(1:10000,1) set.seed(my.seed)然后我可以用它来写一个文件名之类的。

save(my_plot,paste0('plot',my.seed,'.rda')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM