首页 > 解决方案 > 如何在 R 中使用列索引作为 x 轴

问题描述

我有一个包含 7 列和 100 个观察值的数据框 我将观察结果分为两组

我正在研究的问题是: b) 构建两个时间图,将平均血铅水平叠加在每次琥珀酸和安慰剂组的血铅水平上。

到目前为止,这是我的代码:

library(tidyverse)
library(haven)
library(dplyr)
library(plyr)
library(foreign)
library(ggplot2)

tlc = read_dta(file = 'tlc.dta')
head(tlc)
## a)
placebo = subset(tlc, tlc$trt==0)
succimer = subset(tlc, tlc$trt==1)
summary(placebo[, 3:6])
summary(succimer[, 3:6])
placebo_mean=colMeans(placebo[ ,3:6])
placebo_std=apply(placebo[ ,3:6],2,sd)
placebo_var=placebo_std^2
succimer_mean=colMeans(succimer[ ,3:6])
succimer_std=apply(succimer[ ,3:6],2,sd)
succimer_var=succimer_std^2

## b)


## c)
placebo_cor=cor(placebo[ , 3:6]) %>% round(digits = 3)
succimer_cor=cor(succimer[ , 3:6]) %>% round(digits = 3)
placebo_cov=cov(placebo[ , 3:6]) %>% round(digits = 3)
succimer_cov=cov(succimer[ , 3:6]) %>% round(digits = 3)

所以目的是通过使用值作为 y 轴,将 y0、y1、y4、y6 列(代表第 0 周、第 1 周、第 4 周、第 6 周)作为 x 轴来绘制所有观察结果,然后绘制每组的平均值叠加在剧情上。我打算用不同的颜色来区分两组,所以最终的绘图在每个 x 坐标上会有很多点,并且有两条短线表示每个组在每个 x 坐标上的均值。

我的问题是如何在 R 中使用列索引作为 x 轴?使用或不使用 ggplot。我知道这个问题可能太初级了,但是给我这个初学者带来了很多麻烦。

以下是我的数据:

dput(tlc) 结构(list(id = 结构(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100), format.stata = "%9.0g"), trt = structure(c(0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1), format.stata = "%9.0g", class = "haven_labelled",labels = c(Placebo = 0, Succimer = 1)), y0 = structure(c(30.7999992370605, 26.5, 25.7999992370605, 24.7000007629395, 20.3999996185303, 20.3999996185303, 28.6000003814697, 33.7000007629395, 19.7000007629395, 31.1000003814697, 19.7999992370605, 24.7999992370605, 21.3999996185303, 27.8999996185303, 21.1000003814697, 20.6000003814697, 24, 37.5999984741211, 35.2999992370605, 28.6000003814697, 31.8999996185303, 29.6000003814697, 21.5, 26.2000007629395, 21.7999992370605, 23, 22.2000007629395, 20.5, 25, 33.2999992370605, 26, 19.7000007629395, 27.8999996185303, 24.7000007629395, 28.7999992370605, 29.6000003814697, 32, 21.7999992370605, 24.3999996185303, 33.7000007629395, 24.8999996185303, 19.7999992370605, 26.7000007629395, 26.7999992370605, 20.2000007629395, 35.4000015258789, 25.2999992370605, 20.2000007629395, 24.5, 20.2999992370605, 20.3999996185303,24.1000003814697, 27.1000003814697, 34.7000007629395, 28.5, 26.6000003814697, 24.5, 20.5, 25.2000007629395, 34.7000007629395, 30.2999992370605, 26.6000003814697, 20.7000007629395, 27.7000007629395, 24.2999992370605, 36.5999984741211, 28.8999996185303, 34, 32.5999984741211, 29.2000007629395, 26.3999996185303, 21.7999992370605, 27.2000007629395, 22.3999996185303, 32.5, 24.8999996185303, 24.6000003814697, 23.1000003814697, 21.1000003814697, 25.7999992370605, 30, 22.1000003814697, 20, 38.0999984741211, 28.8999996185303, 25.1000003814697, 19.7999992370605, 22.1000003814697, 23.5, 29.1000003814697, 30.2999992370605, 25.3999996185303, 30.6000003814697, 22.3999996185303, 31.2000007629395, 31.3999996185303, 41.0999984741211, 29.3999996185303, 21.8999996185303, 20.7000007629395 ), format .stata = "%9.0g"), y1 = 结构(c(26.8999996185303, 14.8000001907349, 23, 24.5, 2.79999995231628, 5.40000009536743, 20.7999992370605, 31.6000003814697, 14.8999996185303, 31.2000007629395, 17.5, 23.1000003814697, 26.2999992370605, 6.30000019073486, 20.2999992370605, 23.8999996185303, 16.7000007629395, 33.7000007629395, 25.5, 15.8000001907349, 27.8999996185303, 15.8000001907349, 6.5, 26.7999992370605, 12, 4.19999980926514, 11.5, 21.1000003814697, 3.90000009536743, 26.2000007629395, 21.3999996185303, 13.1999998092651, 21.6000003814697, 21.2000007629395, 26.3999996185303, 17.5, 30.2000007629395, 19.2999992370605, 16.3999996185303, 14.8999996185303, 20.8999996185303, 18.8999996185303, 6.40000009536743, 20.3999996185303, 10.6000003814697, 30.3999996185303, 23.8999996185303, 17.5, 10, 21, 17.2000007629395, 20.1000003814697、14.8999996185303、39、32.5999984741211、22.3999996185303、5.09999990463257、17.5、25。1000003814697, 39.5, 29.3999996185303, 25.2999992370605, 19.2999992370605, 4, 24.2999992370605, 23.2999992370605, 28.8999996185303, 10.6999998092651, 19, 9.19999980926514, 15.3000001907349, 10.6000003814697, 28.5, 22, 25.1000003814697, 23.6000003814697, 25, 20.8999996185303, 5.59999990463257, 21.8999996185303, 27.6000003814697, 21, 22.7000007629395, 40.7999992370605, 12.5, 28.1000003814697, 11.6000003814697, 21.1000003814697, 7.90000009536743, 16.7999992370605, 3.5, 24.2999992370605, 28.2000007629395, 7.09999990463257, 10.8000001907349, 3.90000009536743, 15.1000003814697, 22.1000003814697, 7.59999990463257, 8.10000038146973), format.stata = "%9.0g"), y4 = structure( c(25.7999992370605, 19.5, 19.1000003814697, 22, 3.20000004768372, 4.5, 19.2000007629395, 28.5, 15.3000001907349, 29.2000007629395, 20.5, 24.6000003814697, 19.5, 18.5, 18.3999996185303, 19, 21.7000007629395, 34.4000015258789, 26.2999992370605, 22.8999996185303, 27.2999992370605, 23.7000007629395, 7.09999990463257, 25.2999992370605, 16.7999992370605, 4, 9.5, 17.3999996185303, 12.8000001907349, 34, 21, 14.6000003814697, 23.6000003814697, 22.8999996185303, 23.7999992370605, 21, 30.2000007629395, 16.3999996185303, 11.6000003814697, 14.5, 22.2000007629395, 18.8999996185303, 5.09999990463257, 19.2999992370605, 9, 26.5, 22.2000007629395, 17.3999996185303, 15.6000003814697, 16.7000007629395, 15.8999996185303, 17.8999996185303, 18.1000003814697, 28.7999992370605, 27.5, 21.7999992370605, 8.19999980926514, 19.6000003814697, 23.3999996185303, 38.5999984741211, 33.0999984741211, 25.1000003814697, 21.8999996185303, 4.19999980926514, 18.3999996185303、40.4000015258789、32.7999992370605、12.6000003814697、16.2999992370605、8。30000019073486, 24.6000003814697, 14.3999996185303, 35, 19.1000003814697, 27.7999992370605, 21.2000007629395, 21.7000007629395, 21.7000007629395, 7.30000019073486, 23.6000003814697, 24, 8.60000038146973, 21.2000007629395, 38, 16.7000007629395, 27.5, 13, 21.5, 12.3999996185303, 15.1000003814697, 3, 22.7000007629395, 27, 17.2000007629395, 19.7999992370605, 7, 10.8999996185303, 25.2999992370605, 10.8000001907349, 25.7000007629395), format.stata = "%9.0g"), y6 = structure(c(23.7999992370605, 21, 23.2000007629395, 22.5, 9.39999961853027, 11.8999996185303, 18.3999996185303, 25.1000003814697, 14.6999998092651, 30.1000003814697, 27.5, 30.8999996185303, 19, 16.2999992370605, 20.7999992370605, 17, 20.2999992370605, 31.3999996185303, 30.2999992370605, 25.8999996185303, 34.2000007629395, 23.3999996185303, 16, 24.7999992370605, 19.2000007629395, 16.2000007629395, 14.5, 21.1000003814697, 12.6999998092651, 28.2000007629395, 22.3999996185303, 11.6000003814697, 27.7000007629395, 21.8999996185303, 22, 24.2000007629395, 27.5, 17.6000003814697, 16.6000003814697, 63.9000015258789, 19.7999992370605, 15.5, 15.1000003814697, 23.7999992370605, 16, 28.1000003814697, 27.2000007629395, 18.6000003814697, 15.1999998092651, 13.5, 17.7000007629395, 18.7000007629395, 21.2999992370605, 34.7000007629395, 22.7999992370605, 21, 23.6000003814697, 18.3999996185303, 22.2000007629395, 43.2999992370605, 28.3999996185303, 27.8999996185303, 21.7999992370605, 11.6999998092651, 27.7999992370605, 39.2999992370605, 31.7999992370605, 21.2000007629395, 18.6000003814697, 18.3999996185303, 32.4000015258789, 18.7000007629395, 30.5, 18.7000007629395, 27.2999992370605, 21.1000003814697、23.8999996185303、19.8999996185303、12。3000001907349, 24.7999992370605, 23.7000007629395, 24.6000003814697, 20.5, 32.7000007629395, 22.2000007629395, 24.7999992370605, 23.1000003814697, 20.6000003814697, 18.8999996185303, 18.7999992370605, 11.5, 20.1000003814697, 25.5, 18.7000007629395, 22.2000007629395, 17.7999992370605, 27.1000003814697, 4.09999990463257, 13, 12.3000001907349), format.stata = " %9.0g")), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))名称= c(NA,-100L),类= c(“tbl_df”,“tbl”,“data.frame”))名称= c(NA,-100L),类= c(“tbl_df”,“tbl”,“data.frame”))

我也试过这个:

p=ggplot(tlc, aes(x=colnames(tlc[,3:6],do.NULL=TRUE)),
     y=value)
p=p+geom_point()

运行代码时没有发现错误,但是当我调用“p”来绘制它时,R 确实报告了一个错误(美学必须是长度 1 或与数据相同(100):x)。

标签: r

解决方案


我没有你的数据,但听起来你想要这样的东西:

在此处输入图像描述

这是我的制作方法:

library(tidyverse)
# Setting up some fake data: 100 observations and 7 variables
set.seed(123)
some_data <- data.frame(y0 = rnorm(100),
                        y1 = runif(100),
                        y2 = rexp(100, 2),
                        y3 = rnorm(100, 2, 1),
                        y4 = rexp(100),
                        y5 = rnorm(100, 2,2),
                        y6 = runif(100, -5, 5))
# pivoting the data to longer format:
long_data <- some_data %>%
  pivot_longer(cols = everything(),
           names_to = "variable")
# building the base plot
p <- ggplot(long_data, aes(x = variable, y = value))
# adding the points - use position_jitter to give it some width if you want
p <- p + geom_point(position = position_jitter(width = 0.2))
# adding the bars at mean - play around with width, color, and size
p <- p + stat_summary(geom = "errorbar", 
                      fun = mean,
                      width = 0.4, 
                      aes(ymax = ..y.., ymin = ..y..), 
                      color = "orange", 
                      size = 1.5)
p # show plot

推荐阅读