【问题标题】:Plot grouped point data using only base R code仅使用基本 R 代码绘制分组点数据
【发布时间】:2019-06-19 15:48:09
【问题描述】:

我正在做一个作业,其中我只被允许使用基础 R,没有包。

这是一些示例数据:

set.seed(1)
variables <- paste0('V_', seq(1,16,1))
data <- data.frame(t(rbind(variables, rnorm(16,0,1),rnorm(16,0,1), rnorm(16,0,1))))
colnames(data) <- c('variables','OLS', 'IV', '2SLS')}''

我知道如何在 ggplot2 上执行此操作,但在 base R 上不知道。 我想以每种类型的模型都用颜色编码的方式用点绘制每个变量的值。在我的 x 轴上,我们将拥有从 V_1V_16 的所有因子(如果轴上的所有标签都显示出来,那就太好了)。

有什么建议吗? 谢谢!

【问题讨论】:

    标签: r plot graph


    【解决方案1】:
    set.seed(1)
    variables <- paste0('V_', seq(1,16,1))
    data <- data.frame(t(rbind(variables, rnorm(16,0,1),rnorm(16,0,1), rnorm(16,0,1))))
    colnames(data) <- c('variables','OLS', 'IV', '2SLS')
    
    
    attach(data)
    #> The following object is masked _by_ .GlobalEnv:
    #>     variables
    
    variables <- factor(variables, 
                        levels = variables[order(as.numeric(gsub("V_","", variables)))])
    
    plot.default(variables,as.double(OLS),type='p',xaxt='n', ylab="value", cex=1, col="red")
    points(x=variables, y=as.double(IV), col="blue")
    points(x=variables, y=as.double(`2SLS`), col="green")
    axis(side = 1, at = as.numeric(variables), labels = variables)
    
    detach(data)
    

    reprex package (v0.3.0) 于 2019 年 6 月 19 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2013-11-24
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多