用R中的for循环简化绘图(ggplot)

时间:2015-02-17 11:13:19

标签: r ggplot2

我试图将几个计算变量读入数据帧的不同列,这是不可能的,因为所有col都必须具有相同的长度。所以它创建了列表(Data_Overall)和peu a peu创建我的ggplot层。 我无法在这里使用for循环或者粘贴函数,因此我的代码看起来如下:

 Data_Overall
    as.data.frame(Data_Overall[[1]])
    ggplot()  +geom_point(data=data.frame(Data_Overall[[1]]),aes(1,as.numeric(Data_Overall[[1]])))+
   geom_point(data=data.frame(Data_Overall[[2]]),aes(2,as.numeric(Data_Overall[[2]])))+
  geom_point(data=data.frame(Data_Overall[[3]]),aes(3,as.numeric(Data_Overall[[3]])))+
  geom_point(data=data.frame(Data_Overall[[4]]),aes(4,as.numeric(Data_Overall[[4]])))+
  geom_point(data=data.frame(Data_Overall[[5]]),aes(5,as.numeric(Data_Overall[[5]])))+
  geom_point(data=data.frame(Data_Overall[[6]]),aes(6,as.numeric(Data_Overall[[6]])))+
  geom_boxplot(data=data.frame(Data_Overall[[1]]),aes(1,as.numeric(Data_Overall[[1]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[2]]),aes(2,as.numeric(Data_Overall[[2]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[3]]),aes(3,as.numeric(Data_Overall[[3]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[4]]),aes(4,as.numeric(Data_Overall[[4]]),alpha=0.2))+
   geom_boxplot(data=data.frame(Data_Overall[[5]]),aes(5,as.numeric(Data_Overall[[5]]),alpha=0.2))+
              geom_boxplot(data=data.frame(Data_Overall[[6]]),aes(6,as.numeric(Data_Overall[[6]]),alpha=0.2))

数据

Data_Overall<-list()
Data_Overall[[1]]<-c("90","80","90","90","80","70","70","100","100","50","99.9","70","50","80","30","50","50","90","90","50","60","85","50","10",   "50",   "30",   "50",   "30",   "95",   "50",   "50",   "50",   "20",   "50",   "100",  "60")
Data_Overall[[2]]<-c("80","100","70")
Data_Overall[[3]]<-c("100","50","99.9","70","50","80","30","50","50","90","90","50")
Data_Overall[[4]]<-c("80","100","70")
Data_Overall[[5]]<-c("100","50","99.9","70","50","80","50","50","90","90","30","50","50","90","90","50")
Data_Overall[[6]]<-c("50","88","70","76")

2 个答案:

答案 0 :(得分:0)

好的,这是Data_Overall的值列表,知道它应该可以运行了。问题基本上是罗马提到的,我有一些循环和读取不同大小的数据,这些数据不适用于矩阵或data.frame - 因此我选择了列表()。

Data_Overall<-list()
Data_Overall[[1]]<-c("90","80","90","90","80","70","70","100","100","50","99.9","70","50","80","30","50","50","90","90","50","60","85","50","10"   "50"   "30"   "50"   "30"   "95"   "50"   "50"   "50"   "20"   "50"   "100"  "60")
Data_Overall[[2]]<-c("80","100","70")
Data_Overall[[3]]<-c("100","50","99.9","70","50","80","30","50","50","90","90","50")
Data_Overall[[4]]<-c("80","100","70")
Data_Overall[[5]]<-c("100","50","99.9","70","50","80","50","50","90","90","30","50","50","90","90","50")
Data_Overall[[6]]<-c("50","88","70","76")

ggplot() + 
geom_point(data=data.frame(Data_Overall[[1]]),aes(1,as.numeric(Data_Overall[[1]]))) + 

  geom_point(data=data.frame(Data_Overall[[2]]),aes(2,as.numeric(Data_Overall[[2]]))) +

geom_point(data=data.frame(Data_Overall[[3]]),aes(3,as.numeric(Data_Overall[[3]])))+

geom_point(data=data.frame(Data_Overall[[4]]),aes(4,as.numeric(Data_Overall[[4]])))+
  geom_point(data=data.frame(Data_Overall[[5]]),aes(5,as.numeric(Data_Overall[[5]])))+
  geom_point(data=data.frame(Data_Overall[[6]]),aes(6,as.numeric(Data_Overall[[6]])))+
  geom_boxplot(data=data.frame(Data_Overall[[1]]),aes(1,as.numeric(Data_Overall[[1]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[2]]),aes(2,as.numeric(Data_Overall[[2]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[3]]),aes(3,as.numeric(Data_Overall[[3]]),alpha=0.2))+
  geom_boxplot(data=data.frame(Data_Overall[[4]]),aes(4,as.numeric(Data_Overall[[4]]),alpha=0.2))+
   geom_boxplot(data=data.frame(Data_Overall[[5]]),aes(5,as.numeric(Data_Overall[[5]]),alpha=0.2))+
              geom_boxplot(data=data.frame(Data_Overall[[6]]),aes(6,as.numeric(Data_Overall[[6]]),alpha=0.2))

答案 1 :(得分:0)

OP已在列表Data_Overall中提供数据。每个列表元素是特定变量的计算数值的向量。向量确实有不同的长度。

由于ggplot2更喜欢以长格式提供数据,因此需要将向量列表转换为包含以下列的数据框:VariableValue。 (这遵循建议in this comment)。

准备数据

library(data.table)   # CRAN version 1.10.4 used here

# convert each list element into a data.table,
# combine resulting list of data.tables into one large data.table
# thereby creating an id column named Variable
DT <- rbindlist(lapply(Data_Overall, data.table), idcol = "Variable")
# rename the Value column
setnames(DT, "V1", "Value")
# convert Value from character to numeric
DT[, Value := as.numeric(Value)]
# turn Variable into factor to avoid continuous scale when plotting
DT[, Variable := factor(Variable)]

DT
#    Variable Value
# 1:        1    90
# 2:        1    80
# 3:        1    90
# 4:        1    90
# 5:        1    80
# 6:        1    70
# ...
#68:        5    90
#69:        5    90
#70:        5    50
#71:        6    50
#72:        6    88
#73:        6    70
#74:        6    76
#    Variable Value

创建情节

library(ggplot2)
ggplot(DT, aes(Variable, Value, group = Variable)) +
  geom_boxplot() +
  geom_point()

enter image description here

箱图绘制在第一层,点位于顶部。因此,无需设置alpha = 0.2

数据

Data_Overall <- list(c("90", "80", "90", "90", "80", "70", "70", "100", "100", 
"50", "99.9", "70", "50", "80", "30", "50", "50", "90", "90", 
"50", "60", "85", "50", "10", "50", "30", "50", "30", "95", "50", 
"50", "50", "20", "50", "100", "60"), c("80", "100", "70"), c("100", 
"50", "99.9", "70", "50", "80", "30", "50", "50", "90", "90", 
"50"), c("80", "100", "70"), c("100", "50", "99.9", "70", "50", 
"80", "50", "50", "90", "90", "30", "50", "50", "90", "90", "50"
), c("50", "88", "70", "76"))