-4

I tried to read several calculated variables into different columns of a data frame which is not possible due to the fact that all cols have to have the same length. So it created the list (Data_Overall) and peu a peu create layers of my ggplot. I was unable to employ a for-loop here, or the paste-function, so that my code looks the following:

 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

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")
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • 3
    Welcome to Stackoverflow. Please provide a reproducible example like this: http://adv-r.had.co.nz/Reproducibility.html – lukeA Feb 17 '15 at 11:22
  • The way you are using ggplot is abusing its functionality. Consider coercing your data into a single data.frame with a logical layout (variable names). If they're off different length, padding with NAs would be a clear solution. A reproducible example will go a long way. Next to lukeA's link, see also [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Feb 18 '15 at 08:33

2 Answers2

0

Okay here is the list of values for Data_Overall, know it should be ready to run. The problem is essentially what Roman referred to that I had some loops and read data of different sizes which were not applicable to a matrix or data.frame - thus I chose the list().

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))
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • This is not answer but is providing the required data. It should have been edited into the Q (which I did). So this posting could be deleted. – Uwe May 22 '17 at 14:40
0

The OP has supplied data in a list Data_Overall. Each list element is a vector of calculated numeric values of a particular variable. The vectors do have varying lengths.

As ggplot2 prefers data to be supplied in long format anyway, the list of vectors needs to be converted into a data.frame with columns: Variableand Value. (This follows the suggestion in this comment).

Prepare data

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

Create plot

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

enter image description here

The boxplots are plotted in the first layer, and the points on top. So, there is no need to set alpha = 0.2.

Data

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"))
Uwe
  • 41,420
  • 11
  • 90
  • 134