首页 > 解决方案 > 尝试在 R ggplot 中绘制模拟数据会在绘图左侧产生一个奇怪的灰色块,我该怎么办?

问题描述

我正在尝试为我的论文的统计数据模拟一些数据,我觉得我在这方面做得很好,我对 R 没有那么丰富的经验,所以它可能比必要的时间长得多。我正在尝试将这些数据绘制在散点图中,但它正在努力解决它,我真的不知道如何解释它,但是图左侧有一个巨大的灰色块并且数据没有按顺序排列( 105 天 => 14 天 => 21 天)。Base R 处理得很好,但 ggplot 只是疯了。我该怎么办?

奇怪的事情出现了,最高的 x 值是第一个,然后是最低的:

dualex <- cbind.data.frame(
      rbind(cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 20, sd = 2), 4), rep('C', 182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 19.8, sd = 2),4), rep('T1',182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 18.5, sd = 2),4), rep('T2',182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 18, sd = 2),4), rep('T3', 182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 17.4, sd = 2),4), rep('T4', 182))),
      rbind(cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 0.35, sd = 0.02),4), rep('C', 182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 0.34, sd = 0.02),4), rep('T1',182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 0.32, sd = 0.02),4), rep('T2',182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 0.28, sd = 0.02),4), rep('T3',182)),
      cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
            round(rnorm(n = 182, mean = 0.23, sd = 0.02),4), rep('T4',182))),
      rbind(cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
                  round(rnorm(n = 182, mean = 0.085, sd = 0.004),4), rep('C', 182)),
            cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
                  round(rnorm(n = 182, mean = 0.083, sd = 0.004),4), rep('T1',182)),
            cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
                  round(rnorm(n = 182, mean = 0.078, sd = 0.004),4), rep('T2',182)),
            cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
                  round(rnorm(n = 182, mean = 0.28, sd = 0.02),4), rep('T3',182)),
            cbind(rep(c(14,21,28,35,42,49,56,63,70,77,84,91,98,105), each = 13),
                  round(rnorm(n = 182, mean = 0.070, sd = 0.004),4), rep('T4',182))))

dualex[,c(3,4,6,7)] <- NULL
colnames(dualex) <- c('Day', 'Chl', 'Flav', 'Anth', 'Treatment')
  
#ggplot going crazy
ggplot(data = dualex, mapping = aes(x = Day, y = Chl)) + geom_point()

#base R takes it fine
plot(dualex$Day, dualex$Chl)

标签: rggplot2plot

解决方案


试试这个,你的问题是因素变量:

library(tidyverse)
#Code
dualex %>% mutate(across(Chl:Anth,~as.numeric(as.character(.)))) %>%
  ggplot(aes(x = Day, y = Chl)) + geom_point()

输出:

在此处输入图像描述


推荐阅读