我如何将离散因子水平视为连续水平? [英] How do I get discrete factor levels to be treated as continuous?

查看:69
本文介绍了我如何将离散因子水平视为连续水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中的列最初是任意标记的。稍后,我想将这些级别更改为数值。下面的脚本说明了该问题。

I have a data frame with columns initially labeled arbitrarily. Later on, I want to change these levels to numerical values. The following script illustrates the problem.

library(ggplot2)
library(reshape2)

m <- 10
n <- 6

nam <- list(c(),letters[1:n])
var <- as.data.frame(matrix(sort(rnorm(m*n)),m,n,F,nam))
dtf <- data.frame(t=seq(m)*0.1, var)
mdf <- melt(dtf, id=c('t'))

xs <- c(0.25,0.5,1.0,2.0,4.0,8.0)
levels(mdf$variable) <- xs

g <- ggplot(mdf,aes(variable,value,group=variable,colour=t))
g +
    geom_point() +
    #scale_x_continuous() +
    opts()

将生成此图。

可变数量在图上均匀分布,即使从数字上讲不是这样。我如何才能正确地获得x轴上的间距?

The 'variable' quantities are evenly spaced on the plot, even though numerically this is not true. How can I get the spacing on the x-axis correct?

推荐答案

我认为您可以通过将变量转换为数值:

I think you can do this simply by transforming the variable to numeric:

mdf$variable <- as.numeric(as.character(mdf$variable))

g <- ggplot(mdf,aes(variable,value,group=variable,colour=t))
g +
    geom_point() +
    #scale_x_continuous() +
    opts()

这篇关于我如何将离散因子水平视为连续水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆