R: plot() 在 as.data.frame() 之后使用散点图中的线
R: plot() uses lines in a scatterplot after as.data.frame()
我想使用带有 2 个变量的 table 创建一个简单的散点图。
Table 看起来像这样:
> freqs
Var1 Freq
1 1 200
2 2 50
3 3 20
我使用 freqs <- as.data.frame(table(data$V2))
计算另一个 table 中数字的频率得到它。
我现在做的是:
plot(freqs, log="xy", main="Frequency of frequencies",
xlab="frequencies", ylab="frequency of frequencies")
问题是,我得到的图是线条而不是点,我不知道为什么。
对于另一个列表 plot()
表现不同并使用点。
它看起来像这样:
我知道绘图取决于它获取的数据类型。
那么我生成freqs
的方式有问题吗?
编辑:
这里是请求的数据:link
步骤是:
data <- read.csv(file="out-kant.txt",head=FALSE,sep="\t")
freqs <- as.data.frame(table(data$V2))
plot(freqs,log="xy",main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
您的某个变量的类型似乎未设置为整数。当 x 和 y 都是整数时,您会得到一个散点图。例如,当您 运行 这段代码时,您将得到一个散点图,因为它会自动将两个变量设置为整数:
freqs <- read.table(header=TRUE, text='Var1 freq
1 200
2 50
3 20')
plot(freqs, log="xy", main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
检查你的变量是什么类型:
typeof(freqs$freq)
typeof(freqs$Var1)
然后,如果它不是一个整数,修复它:
freqs$freq <- as.integer(freqs$freq)
freqs$Var1 <- as.integer(freqs$Var1)
编辑:所以当我 运行:
时,我设法重现了你的问题
freqs$Var1 <- as.factor(freqs$Var1)
plot(freqs, log="xy", main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
也许您的 Var1 变量被指定为一个因素。尝试 运行宁:
freqs$Var1 <- as.numeric(freqs$Var1)
EDIT2:使用上面的代码在主要问题的编辑中提供的数据上使 freqs$Var1 成为数字,这解决了问题。
我想使用带有 2 个变量的 table 创建一个简单的散点图。 Table 看起来像这样:
> freqs
Var1 Freq
1 1 200
2 2 50
3 3 20
我使用 freqs <- as.data.frame(table(data$V2))
计算另一个 table 中数字的频率得到它。
我现在做的是:
plot(freqs, log="xy", main="Frequency of frequencies",
xlab="frequencies", ylab="frequency of frequencies")
问题是,我得到的图是线条而不是点,我不知道为什么。
对于另一个列表 plot()
表现不同并使用点。
它看起来像这样:
我知道绘图取决于它获取的数据类型。
那么我生成freqs
的方式有问题吗?
编辑:
这里是请求的数据:link
步骤是:
data <- read.csv(file="out-kant.txt",head=FALSE,sep="\t")
freqs <- as.data.frame(table(data$V2))
plot(freqs,log="xy",main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
您的某个变量的类型似乎未设置为整数。当 x 和 y 都是整数时,您会得到一个散点图。例如,当您 运行 这段代码时,您将得到一个散点图,因为它会自动将两个变量设置为整数:
freqs <- read.table(header=TRUE, text='Var1 freq
1 200
2 50
3 20')
plot(freqs, log="xy", main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
检查你的变量是什么类型:
typeof(freqs$freq)
typeof(freqs$Var1)
然后,如果它不是一个整数,修复它:
freqs$freq <- as.integer(freqs$freq)
freqs$Var1 <- as.integer(freqs$Var1)
编辑:所以当我 运行:
时,我设法重现了你的问题freqs$Var1 <- as.factor(freqs$Var1)
plot(freqs, log="xy", main="Frequency of frequencies", xlab="frequencies", ylab="frequency of frequencies")
也许您的 Var1 变量被指定为一个因素。尝试 运行宁:
freqs$Var1 <- as.numeric(freqs$Var1)
EDIT2:使用上面的代码在主要问题的编辑中提供的数据上使 freqs$Var1 成为数字,这解决了问题。