在 ggplot2 中绘制线图时出错,我认为它以某种方式没有正确分组我的组
Error in plotting line plot in ggplot2, I think it somehow is not grouping my groups properly
我只是想用两个条件制作一个简单的线图:标准和偏差。
csv 中的数据最初看起来像这样:
是的,时间应该是负数。时间在这里是一个变量,从 -100 毫秒(事件发生前 100 毫秒)到 1500 毫秒(事件发生后 1500 毫秒)。
本质上,我想做的是绘制标准条件和异常条件下的值(我稍后将其称为振幅)如何随时间变化。看起来像这样的东西:
不幸的是,我得到的是:
这是我的代码:
# Libraries
library(ggplot2)
# Plotting
ggplot(data=PupilERP, aes(x=Pt, y=Amplitude, group=Condition)) +
geom_line() + scale_y_continuous(breaks = seq(-5,15,1)) + scale_x_continuous(breaks = seq(-100,1500,100))
编辑:正如评论中所要求的,这是我通过第 8 行后的示例数据- 出现在该行之后 colnames(PupilERP) <- c("Pt","Deviant","Standard")
另外,还有人要了dput输出。此时 Tt 太长了,无法为您提供数据(在 colnames 行之后),甚至最多只有 20 个点,所以在我完成所有重塑之后,这里是实际的 dput 输出。
structure(list(Pt = c(13L, 110L, 109L, 108L, 107L, 106L, 105L,
104L, 103L, 102L, 101L, 99L, 98L, 97L, 96L, 95L, 94L, 93L, 92L,
91L), Condition = c("Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant"), Amplitude = c(0.0089,
-0.0066, -0.0076, 0.0105, 0.0514, 0.111, 0.178, 0.2396, 0.2851,
0.306, 0.2999, 0.2708, 0.2277, 0.1796, 0.1318, 0.085, 0.0399,
0.0012, -0.0264, -0.0413)), row.names = c(NA, 20L), class = "data.frame")
问题可能出现在您的数据处理中。您对 Pt
列的 as.integer
调用创建了错误的数字。这是因为在您对其进行转置之后,Pt
变量已成为一个因素,因此例如“-11”被解释为 15 级的因素(例如) - 这可能在您的数据中导致重复点(您会注意到图表中没有负数)。
要解决此问题,请在调用 as.integer
之前将 Pt
强制转换为字符向量。我使用虚拟数据执行以下操作(您的问题无法从上面的 dput 部分重现):
library(ggplot2)
library(tidyr)
# dummy data
df <- read.csv("test.csv")
df <- t(df)
df <- as.data.frame(df)
df <- df[-1,]
colnames(df) <- c("Pt","Deviant","Standard")
df$Pt <- as.integer(as.character(df$Pt)) # the key change - will read neg. numbers
df <- gather(df, Condition, Amplitude, Deviant:Standard)
df$Amplitude <- as.numeric(df$Amplitude)
ggplot(df, aes(Pt, Amplitude, colour = Condition)) + geom_line()
希望能帮助解决一些问题。
我只是想用两个条件制作一个简单的线图:标准和偏差。
csv 中的数据最初看起来像这样:
是的,时间应该是负数。时间在这里是一个变量,从 -100 毫秒(事件发生前 100 毫秒)到 1500 毫秒(事件发生后 1500 毫秒)。
本质上,我想做的是绘制标准条件和异常条件下的值(我稍后将其称为振幅)如何随时间变化。看起来像这样的东西:
不幸的是,我得到的是:
这是我的代码:
# Libraries
library(ggplot2)
# Plotting
ggplot(data=PupilERP, aes(x=Pt, y=Amplitude, group=Condition)) +
geom_line() + scale_y_continuous(breaks = seq(-5,15,1)) + scale_x_continuous(breaks = seq(-100,1500,100))
编辑:正如评论中所要求的,这是我通过第 8 行后的示例数据- 出现在该行之后 colnames(PupilERP) <- c("Pt","Deviant","Standard")
另外,还有人要了dput输出。此时 Tt 太长了,无法为您提供数据(在 colnames 行之后),甚至最多只有 20 个点,所以在我完成所有重塑之后,这里是实际的 dput 输出。
structure(list(Pt = c(13L, 110L, 109L, 108L, 107L, 106L, 105L,
104L, 103L, 102L, 101L, 99L, 98L, 97L, 96L, 95L, 94L, 93L, 92L,
91L), Condition = c("Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant", "Deviant", "Deviant",
"Deviant", "Deviant", "Deviant", "Deviant"), Amplitude = c(0.0089,
-0.0066, -0.0076, 0.0105, 0.0514, 0.111, 0.178, 0.2396, 0.2851,
0.306, 0.2999, 0.2708, 0.2277, 0.1796, 0.1318, 0.085, 0.0399,
0.0012, -0.0264, -0.0413)), row.names = c(NA, 20L), class = "data.frame")
问题可能出现在您的数据处理中。您对 Pt
列的 as.integer
调用创建了错误的数字。这是因为在您对其进行转置之后,Pt
变量已成为一个因素,因此例如“-11”被解释为 15 级的因素(例如) - 这可能在您的数据中导致重复点(您会注意到图表中没有负数)。
要解决此问题,请在调用 as.integer
之前将 Pt
强制转换为字符向量。我使用虚拟数据执行以下操作(您的问题无法从上面的 dput 部分重现):
library(ggplot2)
library(tidyr)
# dummy data
df <- read.csv("test.csv")
df <- t(df)
df <- as.data.frame(df)
df <- df[-1,]
colnames(df) <- c("Pt","Deviant","Standard")
df$Pt <- as.integer(as.character(df$Pt)) # the key change - will read neg. numbers
df <- gather(df, Condition, Amplitude, Deviant:Standard)
df$Amplitude <- as.numeric(df$Amplitude)
ggplot(df, aes(Pt, Amplitude, colour = Condition)) + geom_line()
希望能帮助解决一些问题。