【问题标题】:plotting lines with dataframes of different lengths用不同长度的数据框绘制线
【发布时间】:2015-08-12 07:48:20
【问题描述】:

我想为x-axis 上的col1x-axis 上的col1 上的col2 上的col2 绘制不同长度的三个数据帧df1df2df3

 # Data
 set.seed(123)
 df1<-data.frame(col1=sample(LETTERS[1:10], 10, replace=FALSE), col2=sample(c(1:26),10))
 > df1
    col1 col2
 1     C   25
 2     H   12
 3     D   17
 4     G   14
 5     F    3
 6     A   19
 7     J    5
 8     I    1
 9     B    6
 10    E   24

 df2 <- data.frame(col1=sample(LETTERS[1:10], 5, replace=FALSE), col2=sample(c(1:26),5))
> df2
   col1 col2
 1    I   19
 2    G   14
 3    F   15
 4    J    7
 5    D    4

df3 <- data.frame(col1=sample(LETTERS[1:10], 8, replace=FALSE), col2=sample(c(1:26),8))
 > df3
   col1 col2
 1    J    9
 2    I    6
 3    F    4
 4    H   10
 5    A   23
 6    C    8
 7    D   24
 8    G    3

 # plotting
 lab.min <- min(c(df2$col2, df1$col2, df3$col2), na.rm = T)
 lab.max <- max(c(df2$col2, df1$col2, df3$col2), na.rm = T)
 plot(df1$col1, df1$col2, type='o', pch=0, las=2, ylim=c(lab.min, lab.max))
 lines(df2$col1, df2$col2, type='o', pch=2)
 lines(df3$col1, df3$col2, type='o', pch=8)
 # add a legend 
 legend(0, 20, c('df1','df2','df3'), cex=0.8, pch=c(0,2,8), title="df")
 abline(h=5)

从图中,我可以观察到这些行没有从所有数据框中获取相同的 col1 值。所有三个数据框在col1 中都有值J,但该图仅显示来自df1 的值。您能否建议如何克服这个问题?

【问题讨论】:

  • 你能详细说明你想要的输出是什么吗?我不清楚你所说的“线条没有采用 col1 值”是什么意思。 df1 的表示是错误的,还是 df2 和 df3 的表示错误?

标签: r plot dataframe


【解决方案1】:

问题在于col1是一个因子,所以在绘制的时候,因子的整数表示就作为x值。要解决此问题,请确保所有 col1 变量具有相同的因子水平,因此具有相同的基础整数。

set.seed(123)
df1<-data.frame(col1=sample(LETTERS[1:10], 10, replace=FALSE), col2=sample(c(1:26),10))
df2 <- data.frame(col1=sample(LETTERS[1:10], 5, replace=FALSE), col2=sample(c(1:26),5))
df3 <- data.frame(col1=sample(LETTERS[1:10], 8, replace=FALSE), col2=sample(c(1:26),8))

## Put the data.frames in a list, and convert col1 to have same levels
dfs <- lapply(mget(paste0("df", 1:3)), function(x)
    transform(x, col1=factor(col1, levels=LETTERS[1:10])))

## Put data.frames back into global environment
## Note: I only do this to reuse your code, but it would be better
## to keep them in a list
list2env(dfs, .GlobalEnv)

## plotting
lab.min <- min(c(df2$col2, df1$col2, df3$col2), na.rm = T)
lab.max <- max(c(df2$col2, df1$col2, df3$col2), na.rm = T)
plot(df1$col1, df1$col2, type='o', pch=0, las=2, ylim=c(lab.min, lab.max))
lines(df2$col1, df2$col2, type='o', pch=2)
lines(df3$col1, df3$col2, type='o', pch=8)
                                        # add a legend 
legend(0, 20, c('df1','df2','df3'), cex=0.8, pch=c(0,2,8), title="df")
abline(h=5)

这也可以通过在您最初创建数据时明确设置因子水平来解决。

【讨论】:

  • 我已将所有 dfs 的 col1 的级别明确设置为 levels(df1$col1) &lt;- LETTERS[1:10]。不幸的是,我的情节保持不变。如何确定绘图/线在绘图时采用的值?
  • @Prradep 但这不起作用 - 它会使你的所有因素从 1 开始,按照我在上面的代码中所做的事情来修复这些因素。您可以通过 as.integer(yourFactor) 查看整数
  • 感谢您的建议。我真正的 dfs 不是这样的,所以我必须找出你的方法的等价物。我设法使用df1$col1 &lt;- factor(df1$col1, levels = LETTERS[1:10]) 纠正了我之前的方法,谢谢,我将根据我的需要遵循它们。您是否建议,如何使这些线条看起来不错,我的意思是在绘制线条时重新排列要考虑的点(A,B,C,D ...)?
  • @Prradep 如果您使用sortorderlines 之前的数据进行排序,则应按顺序连接点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2012-07-29
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 2021-02-10
  • 2021-08-17
相关资源
最近更新 更多