У меня есть датафрейм df
со многими столбцами ...
Я бы хотел график подмножества столбцов, где c
- это список столбцов, которые я хотел бы построить.
В настоящее время я делаю следующее
df <-structure(list(Image.Name = structure(1:5, .Label = c("D1C1", "D2C2", "D4C1", "D5C3", "D6C2"), class = "factor"), Experiment = structure(1:5, .Label = c("020718 perfusion EPC_BC_HCT115_Day 5", "020718 perfusion EPC_BC_HCT115_Day 6", "020718 perfusion EPC_BC_HCT115_Day 7", "020718 perfusion EPC_BC_HCT115_Day 8", "020718 perfusion EPC_BC_HCT115_Day 9"), class = "factor"), Type = structure(c(2L, 1L, 1L, 2L, 1L), .Label = c("VMO", "VMT"), class = "factor"), Date = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "18-Apr-18", class = "factor"), Time = structure(1:5, .Label = c("12:42:02 PM", "12:42:29 PM", "12:42:53 PM", "12:43:44 PM", "12:44:23 PM"), class = "factor"), Low.Threshold = c(10L, 10L, 10L, 10L, 10L), High.Threshold = c(255L, 255L, 255L, 255L, 255L), Vessel.Thickness = c(7L, 7L, 7L, 7L, 7L), Small.Particles = c(0L, 0L, 0L, 0L, 0L), Fill.Holes = c(0L, 0L, 0L, 0L, 0L), Scaling.factor = c(0.001333333, 0.001333333, 0.001333333, 0.001333333, 0.001333333), X = c(NA, NA, NA, NA, NA), Explant.area = c(1.465629333, 1.093447111, 1.014612444, 1.166950222, 1.262710222), Vessels.area = c(0.255562667, 0.185208889, 0.195792, 0.153907556, 0.227996444), Vessels.percentage.area = c(17.43706003, 16.93807474, 19.29722044, 13.18887067, 18.05611774), Total.Number.of.Junctions = c(56L, 32L, 39L, 18L, 46L), Junctions.density = c(38.20884225, 29.26524719, 38.43832215, 15.42482246, 36.42957758), Total.Vessels.Length = c(12.19494843, 9.545333135, 10.2007416, 7.686755647, 11.94211976), Average.Vessels.Length = c(0.182014156, 0.153956986, 0.188902622, 0.08938088, 0.183724919), Total.Number.of.End.Points = c(187L, 153L, 145L, 188L, 167L), Average.Lacunarity = c(0.722820111, 0.919723402, 0.86403871, 1.115896082, 0.821753818)), .Names = c("Image.Name", "Experiment", "Type", "Date", "Time", "Low.Threshold", "High.Threshold", "Vessel.Thickness", "Small.Particles", "Fill.Holes", "Scaling.factor", "X", "Explant.area", "Vessels.area", "Vessels.percentage.area", "Total.Number.of.Junctions", "Junctions.density", "Total.Vessels.Length", "Average.Vessels.Length", "Total.Number.of.End.Points", "Average.Lacunarity"), row.names = c(NA, -5L), class = "data.frame")
doBarPlot <- function(x) {
p <- ggplot(x, aes_string(x="Type", y=colnames(x), fill="Type") ) +
stat_summary(fun.y = "mean", geom = "bar", na.rm = TRUE) +
stat_summary(fun.data = "mean_cl_normal", geom = "errorbar", width=0.5, na.rm = TRUE) +
ggtitle("VMO vs. VMT") +
theme(plot.title = element_text(hjust = 0.5) )
print(p)
ggsave(sprintf("plots/%s_bars.pdf", colnames(x) ) )
return(p)
}
c = c('Total.Vessels.Length', 'Total.Number.of.Junctions', 'Total.Number.of.End.Points', 'Average.Lacunarity')
p[c] <- lapply(df[c], doBarPlot)
Однако это приводит к следующей ошибке:
Error: ggplot2 doesn't know how to deal with data of class numeric
Отладка показывает, что x
внутри doBarPlot имеет тип numeric
, а не data.frame
, поэтому ggplot
ошибки. Однако test <- df2[c]
возвращает переменную типа data.frame
.
Почему x
a numeric
?
Какой лучший способ применить doBarPlot
без обращения к циклу?